]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/env.rs
Auto merge of #58302 - SimonSapin:tryfrom, r=alexcrichton
[rust.git] / src / libsyntax_ext / env.rs
1 // The compiler code necessary to support the env! extension.  Eventually this
2 // should all get sucked into either the compiler syntax extension plugin
3 // interface.
4 //
5
6 use syntax::ast::{self, Ident, GenericArg};
7 use syntax::ext::base::{self, *};
8 use syntax::ext::build::AstBuilder;
9 use syntax::symbol::{keywords, Symbol};
10 use syntax_pos::Span;
11 use syntax::tokenstream;
12
13 use std::env;
14
15 pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
16                               sp: Span,
17                               tts: &[tokenstream::TokenTree])
18                               -> Box<dyn base::MacResult + 'cx> {
19     let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
20         None => return DummyResult::expr(sp),
21         Some(v) => v,
22     };
23
24     let sp = sp.apply_mark(cx.current_expansion.mark);
25     let e = match env::var(&*var.as_str()) {
26         Err(..) => {
27             let lt = cx.lifetime(sp, keywords::StaticLifetime.ident());
28             cx.expr_path(cx.path_all(sp,
29                                      true,
30                                      cx.std_path(&["option", "Option", "None"]),
31                                      vec![GenericArg::Type(cx.ty_rptr(sp,
32                                                      cx.ty_ident(sp, Ident::from_str("str")),
33                                                      Some(lt),
34                                                      ast::Mutability::Immutable))],
35                                      vec![]))
36         }
37         Ok(s) => {
38             cx.expr_call_global(sp,
39                                 cx.std_path(&["option", "Option", "Some"]),
40                                 vec![cx.expr_str(sp, Symbol::intern(&s))])
41         }
42     };
43     MacEager::expr(e)
44 }
45
46 pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
47                        sp: Span,
48                        tts: &[tokenstream::TokenTree])
49                        -> Box<dyn base::MacResult + 'cx> {
50     let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
51         Some(ref exprs) if exprs.is_empty() => {
52             cx.span_err(sp, "env! takes 1 or 2 arguments");
53             return DummyResult::expr(sp);
54         }
55         None => return DummyResult::expr(sp),
56         Some(exprs) => exprs.into_iter(),
57     };
58
59     let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
60         None => return DummyResult::expr(sp),
61         Some((v, _style)) => v,
62     };
63     let msg = match exprs.next() {
64         None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
65         Some(second) => {
66             match expr_to_string(cx, second, "expected string literal") {
67                 None => return DummyResult::expr(sp),
68                 Some((s, _style)) => s,
69             }
70         }
71     };
72
73     if exprs.next().is_some() {
74         cx.span_err(sp, "env! takes 1 or 2 arguments");
75         return DummyResult::expr(sp);
76     }
77
78     let e = match env::var(&*var.as_str()) {
79         Err(_) => {
80             cx.span_err(sp, &msg.as_str());
81             return DummyResult::expr(sp);
82         }
83         Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
84     };
85     MacEager::expr(e)
86 }