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
#![deny(missing_docs)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/dariocurr/env-settings/main/docs/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/dariocurr/env-settings/main/docs/logo.ico"
)]
//! # Env Settings Derive
use proc_macro::TokenStream;
use quote::quote;
use std::collections::HashMap;
use syn::parse;
mod utils;
/// The macro to add the `Derive` functionality
#[proc_macro_derive(EnvSettings, attributes(env_settings))]
pub fn env_settings_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree that we can manipulate
let input = parse(input).unwrap();
// Build the trait implementation
implement(&input)
}
/// Implement the logic of the derive macro
fn implement(input: &utils::input::EnvSettingsInput) -> TokenStream {
let struct_name = &input.name;
let mut new_args = Vec::new();
let mut new_impls = Vec::new();
let mut from_env_impls = Vec::new();
let mut from_env_args = Vec::new();
let mut env_variables_impls = quote! {};
let mut file_path_impls = quote! {};
if let Some(file_path) = &input.params.file_path {
if input.params.delay {
file_path_impls = quote! { env_settings_utils::load_env_file_path(#file_path)?; }
} else {
env_settings_utils::load_env_file_path(file_path).unwrap();
}
}
let case_insensitive = input.params.case_insensitive;
let env_variables = if input.params.delay {
env_variables_impls = quote! {
let env_variables = env_settings_utils::get_env_variables(#case_insensitive);
};
HashMap::new()
} else {
env_settings_utils::get_env_variables(case_insensitive)
};
let prefix = input.params.prefix.clone().unwrap_or_default();
for field in &input.fields {
match field {
utils::field::EnvSettingsField::NonParsable(non_parsable_field) => {
let name = &non_parsable_field.name;
let type_ = &non_parsable_field.type_;
let argument = quote! { #name: #type_ };
new_args.push(argument.clone());
from_env_args.push(argument);
let value = quote! {#name};
new_impls.push(value.clone());
from_env_impls.push(value);
}
utils::field::EnvSettingsField::Parsable(parsable_field) => {
let name = &parsable_field.name;
let name_label = &parsable_field.name_label;
let type_ = &parsable_field.type_;
let type_label = &parsable_field.type_label;
let optional_type = &parsable_field.optional_type;
let mut env_variable = parsable_field
.variable
.to_owned()
.unwrap_or(format!("{prefix}{name}"));
if case_insensitive {
env_variable = env_variable.to_lowercase();
}
// the variable involved must be named `value`
let (optional_value_impl, default_value_impl, new_arg_impl, parse_type) =
match optional_type {
Some(optional_type) => (
quote! { Some(value) },
quote! { None },
quote! { #name: #type_ },
optional_type,
),
None => (
quote! { value },
quote! { return Err(env_settings_utils::EnvSettingsError::NotExists(#env_variable)) },
quote! { #name: Option<#type_> },
type_,
),
};
// the variable involved must be named `value_to_parse`
let convert_err_impl = quote! {
return Err(env_settings_utils::EnvSettingsError::Convert(
#name_label,
value_to_parse.to_owned(),
#type_label,
))
};
let default_impl = match &parsable_field.default {
Some(value_to_parse) => {
quote! {
match #value_to_parse.parse::<#parse_type>() {
Ok(value) => #optional_value_impl,
Err(_) => {
let value_to_parse = #value_to_parse;
#convert_err_impl
}
}
}
}
None => default_value_impl,
};
// the variable involved must be named `value_to_parse`
let parse_impl = quote! {
match value_to_parse.parse::<#parse_type>() {
Ok(value) => #optional_value_impl,
Err(_) => #convert_err_impl
}
};
// the variable involved must be named `env_variables`
let env_value_impl = if input.params.delay {
quote! {
match env_variables.get(#env_variable) {
Some(value_to_parse) => {#parse_impl},
None => #default_impl,
}
}
} else {
match env_variables.get(&env_variable) {
Some(value_to_parse) => quote! {
let value_to_parse = #value_to_parse;
#parse_impl
},
None => default_impl,
}
};
new_impls.push(quote! {
#name: match #name {
Some(value) => #optional_value_impl,
None => #env_value_impl
}
});
new_args.push(new_arg_impl);
from_env_impls.push(quote! { #name: #env_value_impl });
}
}
}
let pre_impls = quote! {
#file_path_impls
#env_variables_impls
};
let generated_impl = quote! {
impl #struct_name {
/// Create a new instance using just the environment variables. Skipped fields must be passed.
/// If something fails, it returns an `env_settings_utils::EnvSettingsError` error
#[allow(clippy::too_many_arguments)]
pub fn new(#(#new_args),*) -> env_settings_utils::EnvSettingsResult<Self> {
#pre_impls
let instance = Self {
#(#new_impls),*
};
Ok(instance)
}
/// Create a new instance using a combination of environment variables and parameters.
/// More in detail, every field that can be initialized by the environment variables can be passed
/// as parameter wrapped in an `Option` object. Then if the parameter is `Some`, it is used,
/// otherwise the value is recoved from the environment variables. Skipped fields must be passed.
/// If something fails, it returns an `env_settings_utils::EnvSettingsError` error
#[allow(clippy::too_many_arguments)]
pub fn from_env(#(#from_env_args),*) -> env_settings_utils::EnvSettingsResult<Self> {
#pre_impls
let instance = Self {
#(#from_env_impls),*
};
Ok(instance)
}
}
};
generated_impl.into()
}