-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathlib.rs
More file actions
256 lines (239 loc) · 8.7 KB
/
Copy pathlib.rs
File metadata and controls
256 lines (239 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use dotenvy::{EnvLoader, EnvSequence};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use std::io;
use syn::{parse::Parse, Ident, LitBool, LitStr, Token};
struct DotenvInput {
path: Option<LitStr>,
override_: bool,
var_name: LitStr,
}
impl Parse for DotenvInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut path = None;
let mut var_name = None;
let mut override_ = None;
while !input.is_empty() {
if let Ok(ident) = input.parse::<Ident>() {
input.parse::<Token![=]>()?;
match ident.to_string().as_str() {
"path" => {
if path.is_some() {
return Err(syn::Error::new(
ident.span(),
"each attribute must be set only once",
));
}
path = Some(input.parse::<LitStr>()?);
}
"override_" => {
if override_.is_some() {
return Err(syn::Error::new(
ident.span(),
"each attribute must be set only once",
));
}
override_ = Some(input.parse::<LitBool>()?.value);
}
"var" => {
if var_name.is_some() {
return Err(syn::Error::new(
ident.span(),
"variable name must be set only once",
));
}
var_name = Some(input.parse::<LitStr>()?);
}
_ => {
return Err(syn::Error::new(
ident.span(),
format!("unkown attribute: {ident}"),
))
}
}
} else if let Ok(s) = input.parse::<LitStr>() {
if var_name.is_some() {
return Err(syn::Error::new(
s.span(),
"unexpected token in macro input (variable name must be set only once)",
));
}
var_name = Some(s);
} else {
return Err(syn::Error::new(
input.span(),
"unexpected token in macro input",
));
}
if !input.is_empty() {
input.parse::<Token![,]>()?;
}
}
let Some(var_name) = var_name else {
return Err(syn::Error::new(
input.span(),
"environment variable name not defined",
));
};
Ok(Self {
path,
override_: override_.unwrap_or(true),
var_name,
})
}
}
/// Loads an environment variable from a file at compile time.
///
/// This macro will expand to the value of the named environment variable at compile
/// time, yielding an expression of type `&'static str`. Use
/// [`dotenvy::var`] instead if you want to read the value at runtime.
///
/// If the environment variable is not defined, then a compilation error will be
/// emitted. To not emit a compile error, use the [`option_dotenv!`](option_dotenv)
/// macro instead. A compilation error will also be emitted if the environment
/// variable is not a valid Unicode string or if reading the .env file failed.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust ignore
/// # use dotenvy_macro::dotenv;
/// assert_eq!(dotenv!("VARIABLE_1"), "value");
/// ```
///
/// ```rust compile_fail
/// # use dotenvy_macro::dotenv;
/// const UNSET_VAR: &str = dotenv!("UNSET_VAR");
/// ```
///
/// Custom attributes:
///
/// ```rust ignore
/// # use dotenvy_macro::dotenv;
/// // Does not override current env with .env contents
/// const NOT_OVERRIDEN: &str = dotenv!("VARIABLE_1", override_ = false);
/// // Reads from custom file path
/// const CUSTOM_PATH: &str = dotenv!("VARIABLE_PROD", path = ".env.prod");
/// // Specifying the `var` attribute
/// const CUSTOM_PATH: &str = dotenv!(var = "VARIABLE_1");
/// ```
#[proc_macro]
pub fn dotenv(input: TokenStream) -> TokenStream {
let input = input.into();
dotenv_inner(input).into()
}
fn dotenv_inner(input: TokenStream2) -> TokenStream2 {
let args = match syn::parse2::<DotenvInput>(input) {
Ok(a) => a,
Err(e) => return e.into_compile_error(),
};
let sequence = if args.override_ {
EnvSequence::EnvThenInput
} else {
EnvSequence::InputThenEnv
};
let path = args.path.map_or_else(|| "./.env".to_owned(), |p| p.value());
let var_name = args.var_name.value();
let loader = EnvLoader::new().path(&path).sequence(sequence).load();
match loader.as_ref().map(|l| l.get(&var_name)) {
Ok(Some(v)) => quote!(#v),
Ok(None) => {
let msg = format!("environment variable `{var_name}` not set");
quote! {
compile_error!(#msg)
}
}
Err(e) => {
if let dotenvy::Error::Io(ioe, _) = e {
if ioe.kind() == io::ErrorKind::NotFound {
if let Ok(var) = std::env::var(&var_name) {
return quote!(#var);
} else {
return quote! {
compile_error!("environment variable not set and env file missing")
};
}
}
}
let msg = e.to_string();
quote! {
compile_error!(#msg)
}
}
}
}
/// Optionally loads an environment variable from a file at compile time.
///
/// If the named environment variable is present at compile time, this will expand
/// into an expression of type `Option<&'static str>` whose value is `Some` of the
/// value of the environment variable (a compilation error will be emitted if the
/// environment variable is not a valid Unicode string or if reading the .env file
/// failed). If either the environment variable or the .env file is not present,
/// then this will expand to `None`. Use [`dotenvy::var`] instead if
/// you want to read the value at runtime.
///
/// A compile time error is only emitted when using this macro if the environment
/// variable exists and is not a valid Unicode string or if reading the .env file
/// failed. To also emit a compile error if the environment variable is not present,
/// use the [`dotenv!`](dotenv) macro instead.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust no_run
/// # use dotenvy_macro::option_dotenv;
/// assert_eq!(option_dotenv!("UNSET_VAR"), None);
/// assert_eq!(option_dotenv!("SET_VAR"), Some("value"));
/// ```
///
/// Custom attributes:
///
/// ```rust no_run
/// # use dotenvy_macro::option_dotenv;
/// // Does not override current env with .env contents
/// const NOT_OVERRIDEN: Option<&str> = option_dotenv!("VARIABLE_1", override_ = false);
/// // Reads from custom file path
/// const CUSTOM_PATH: Option<&str> = option_dotenv!("VARIABLE_PROD", path = ".env.prod");
/// // Specifying the `var` attribute
/// const VAR_ATTR: Option<&str> = option_dotenv!(var = "VARIABLE_1");
/// ```
#[proc_macro]
pub fn option_dotenv(input: TokenStream) -> TokenStream {
let input = input.into();
option_dotenv_inner(input).into()
}
fn option_dotenv_inner(input: TokenStream2) -> TokenStream2 {
let args = match syn::parse2::<DotenvInput>(input) {
Ok(a) => a,
Err(e) => return e.into_compile_error(),
};
let sequence = if args.override_ {
EnvSequence::EnvThenInput
} else {
EnvSequence::InputThenEnv
};
let path = args.path.map_or_else(|| "./.env".to_owned(), |p| p.value());
let var_name = args.var_name.value();
let loader = EnvLoader::new().path(&path).sequence(sequence).load();
match loader.as_ref().map(|l| l.get(&var_name)) {
Ok(Some(v)) => quote!(Some(#v)),
Ok(None) => quote!(None::<&str>),
Err(e) => {
if let dotenvy::Error::Io(ioe, _) = e {
if ioe.kind() == io::ErrorKind::NotFound {
if let Ok(var) = std::env::var(&var_name) {
return quote!(Some(#var));
}
return quote!(None::<&str>);
}
}
let msg = e.to_string();
quote! {
compile_error!(#msg)
}
}
}
}