2020-03-12 23:20:34 +03:00
|
|
|
use crate::ast::*;
|
2021-04-22 11:41:46 +03:00
|
|
|
use crate::utils::{maybe_supported_box, SupportedBox};
|
2020-03-12 23:20:34 +03:00
|
|
|
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
|
|
|
|
use quote::quote;
|
2020-07-02 20:54:17 +03:00
|
|
|
use syn::ext::IdentExt;
|
|
|
|
use syn::parse::{Parse, ParseBuffer, ParseStream, Result};
|
|
|
|
use syn::token::{Brace, Colon, Comma, FatArrow, Lt};
|
|
|
|
use syn::{
|
|
|
|
braced, parenthesized, parse_str, Attribute, Error, Expr, Lit, Meta, MetaList, MetaNameValue,
|
|
|
|
NestedMeta, Token, Type,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn fill_env_prefix(prefix: String) -> Box<dyn Fn(Namespace) -> Namespace> {
|
2020-03-12 23:20:34 +03:00
|
|
|
Box::new(move |mut ns| {
|
|
|
|
let env_prefix = match &ns.env_prefix {
|
|
|
|
None => {
|
2020-07-02 20:54:17 +03:00
|
|
|
let env_prefix = format!("{}{}_", prefix, ns.name.clone().to_string());
|
2020-03-12 23:20:34 +03:00
|
|
|
ns.env_prefix = Some(env_prefix.clone());
|
|
|
|
env_prefix
|
|
|
|
}
|
2020-07-02 20:54:17 +03:00
|
|
|
Some(env_prefix) => env_prefix.clone(),
|
2020-03-12 23:20:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if !ns.namespaces.is_empty() {
|
2020-07-02 20:54:17 +03:00
|
|
|
ns.namespaces = ns
|
|
|
|
.namespaces
|
|
|
|
.into_iter()
|
2020-03-12 23:20:34 +03:00
|
|
|
.map(fill_env_prefix(ns.env_prefix.clone().unwrap()))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ns.variables.is_empty() {
|
2020-07-02 20:54:17 +03:00
|
|
|
ns.variables = ns
|
|
|
|
.variables
|
|
|
|
.into_iter()
|
2020-03-12 23:20:34 +03:00
|
|
|
.map(|mut var| {
|
|
|
|
if var.env_name.is_none() {
|
|
|
|
var.env_name = Some(
|
|
|
|
format!("{}{}", env_prefix.clone(), &var.name.to_string())
|
2020-07-02 20:54:17 +03:00
|
|
|
.to_uppercase(),
|
2020-03-12 23:20:34 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
var
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
ns
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_namespace_content(
|
|
|
|
input: &ParseBuffer,
|
|
|
|
variables: &mut Vec<Variable>,
|
|
|
|
namespaces: &mut Vec<Namespace>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let attributes: Vec<Attribute> = input.call(Attribute::parse_outer)?;
|
|
|
|
if input.peek2(Brace) {
|
|
|
|
let mut namespace: Namespace = input.parse()?;
|
|
|
|
|
|
|
|
for attr in attributes {
|
|
|
|
if attr.path.is_ident("env_prefix") {
|
|
|
|
namespace.env_prefix = parse_attribute(attr, "env_prefix", &namespace.env_prefix)?;
|
|
|
|
} else {
|
|
|
|
namespace.meta.push(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaces.push(namespace);
|
|
|
|
} else {
|
|
|
|
let mut variable: Variable = input.parse()?;
|
|
|
|
|
|
|
|
for attr in attributes {
|
|
|
|
if attr.path.is_ident("env_name") {
|
|
|
|
variable.env_name = parse_attribute(attr, "env_name", &variable.env_name)?;
|
|
|
|
} else {
|
2021-04-22 11:41:46 +03:00
|
|
|
match variable.supported_box {
|
2021-04-22 23:14:48 +03:00
|
|
|
Some(SupportedBox::Vec(current_sep)) if attr.path.is_ident("sep") => {
|
2021-04-22 11:41:46 +03:00
|
|
|
let sep = parse_attribute(attr, "sep", ¤t_sep)?;
|
2021-04-22 23:14:48 +03:00
|
|
|
variable.supported_box = Some(SupportedBox::Vec(sep));
|
2021-04-22 11:41:46 +03:00
|
|
|
}
|
|
|
|
_ => variable.meta.push(attr),
|
|
|
|
}
|
2020-03-12 23:20:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
variables.push(variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:54:17 +03:00
|
|
|
fn parse_attribute(
|
|
|
|
attr: Attribute,
|
|
|
|
name: &'static str,
|
|
|
|
var: &Option<String>,
|
|
|
|
) -> Result<Option<String>> {
|
2020-03-12 23:20:34 +03:00
|
|
|
if var.is_some() {
|
|
|
|
let message = format!("You cannot use {} meta twice", &name);
|
|
|
|
return Err(Error::new_spanned(attr, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
match attr.parse_meta()? {
|
2020-07-02 20:54:17 +03:00
|
|
|
Meta::NameValue(MetaNameValue {
|
|
|
|
lit: Lit::Str(lit_str),
|
|
|
|
..
|
|
|
|
}) => Ok(Some(lit_str.value())),
|
2020-03-12 23:20:34 +03:00
|
|
|
_ => {
|
|
|
|
let message = format!("expected #[{} = \"...\"]", &name);
|
|
|
|
Err(Error::new_spanned(attr, message))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for RootNamespace {
|
|
|
|
fn parse(input: ParseStream) -> Result<Self> {
|
|
|
|
let mut name: Option<Ident> = None;
|
|
|
|
let mut with_module = true;
|
|
|
|
let mut meta: Vec<Attribute> = vec![];
|
|
|
|
|
|
|
|
let attributes: Vec<Attribute> = input.call(Attribute::parse_inner)?;
|
|
|
|
for attr in attributes {
|
|
|
|
if attr.path.is_ident("config") {
|
|
|
|
match attr.parse_meta()? {
|
|
|
|
Meta::List(MetaList { nested, .. }) => {
|
2020-07-02 20:54:17 +03:00
|
|
|
let message =
|
2021-04-15 23:54:28 +03:00
|
|
|
"expected #[config(name = \"...\")] or #[config(unwrap)]".to_string();
|
2020-03-12 23:20:34 +03:00
|
|
|
match nested.first().unwrap() {
|
2020-07-02 20:54:17 +03:00
|
|
|
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
|
|
|
|
path,
|
|
|
|
lit: Lit::Str(lit_str),
|
|
|
|
..
|
|
|
|
})) => {
|
2020-03-12 23:20:34 +03:00
|
|
|
if path.is_ident("name") {
|
|
|
|
name = Some(Ident::new(&lit_str.value(), Span::call_site()));
|
|
|
|
} else {
|
2021-04-15 23:54:28 +03:00
|
|
|
return Err(Error::new_spanned(attr, message));
|
2020-03-12 23:20:34 +03:00
|
|
|
}
|
2020-07-02 20:54:17 +03:00
|
|
|
}
|
2020-03-12 23:20:34 +03:00
|
|
|
NestedMeta::Meta(Meta::Path(path)) => {
|
|
|
|
if path.is_ident("unwrap") {
|
|
|
|
name = None;
|
|
|
|
with_module = false;
|
|
|
|
} else {
|
2021-04-15 23:54:28 +03:00
|
|
|
return Err(Error::new_spanned(attr, message));
|
2020-03-12 23:20:34 +03:00
|
|
|
}
|
2020-07-02 20:54:17 +03:00
|
|
|
}
|
2020-03-12 23:20:34 +03:00
|
|
|
_ => {
|
2021-04-15 23:54:28 +03:00
|
|
|
return Err(Error::new_spanned(attr, message));
|
2020-03-12 23:20:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2021-04-15 23:54:28 +03:00
|
|
|
let message = "expected #[config(...)]".to_string();
|
|
|
|
return Err(Error::new_spanned(attr, message));
|
2020-03-12 23:20:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
meta.push(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if with_module && name.is_none() {
|
|
|
|
name = Some(Ident::new("config", Span::call_site()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut variables: Vec<Variable> = vec![];
|
|
|
|
let mut namespaces: Vec<Namespace> = vec![];
|
|
|
|
while !input.is_empty() {
|
|
|
|
parse_namespace_content(&input, &mut variables, &mut namespaces)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = String::new();
|
2020-07-02 20:54:17 +03:00
|
|
|
let namespaces = namespaces
|
|
|
|
.into_iter()
|
2021-04-15 23:54:28 +03:00
|
|
|
.map(fill_env_prefix(prefix))
|
2020-03-12 23:20:34 +03:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(RootNamespace {
|
|
|
|
name,
|
|
|
|
variables,
|
|
|
|
namespaces,
|
|
|
|
meta,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for Namespace {
|
|
|
|
fn parse(input: ParseStream) -> Result<Self> {
|
|
|
|
let name: Ident = input.parse()?;
|
|
|
|
let mut variables: Vec<Variable> = vec![];
|
|
|
|
let mut namespaces: Vec<Namespace> = vec![];
|
|
|
|
|
|
|
|
let content;
|
|
|
|
braced!(content in input);
|
|
|
|
while !content.is_empty() {
|
|
|
|
parse_namespace_content(&content, &mut variables, &mut namespaces)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.parse::<Comma>().ok();
|
|
|
|
|
|
|
|
Ok(Namespace {
|
|
|
|
name,
|
|
|
|
variables,
|
|
|
|
namespaces,
|
|
|
|
env_prefix: None,
|
|
|
|
meta: vec![],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for Variable {
|
|
|
|
fn parse(input: ParseStream) -> Result<Self> {
|
|
|
|
let is_static = input.parse::<Token![static]>().ok().is_some();
|
|
|
|
let name: Ident = input.parse()?;
|
|
|
|
|
|
|
|
let is_concat = input.peek(Lt);
|
|
|
|
let mut concat_parts = None;
|
|
|
|
let mut initial = None;
|
|
|
|
|
|
|
|
let ty: Type = if is_concat {
|
|
|
|
parse_str("String")?
|
|
|
|
} else if input.peek(Colon) {
|
|
|
|
input.parse::<Colon>()?;
|
|
|
|
input.parse()?
|
|
|
|
} else {
|
|
|
|
parse_str("&'static str")?
|
|
|
|
};
|
|
|
|
|
2021-04-22 11:41:46 +03:00
|
|
|
let supported_box = maybe_supported_box(&ty);
|
|
|
|
|
2020-03-12 23:20:34 +03:00
|
|
|
if is_concat {
|
|
|
|
input.parse::<Lt>()?;
|
|
|
|
|
|
|
|
let content;
|
|
|
|
parenthesized!(content in input);
|
|
|
|
|
|
|
|
let mut tmp_vec: Vec<TokenStream2> = vec![];
|
|
|
|
while !content.is_empty() {
|
|
|
|
if content.peek(Ident::peek_any) {
|
|
|
|
let concat_var: Variable = content.parse()?;
|
|
|
|
let name = &concat_var.name;
|
2021-04-15 23:54:28 +03:00
|
|
|
let env_name = &concat_var
|
|
|
|
.env_name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| name.to_string());
|
2020-03-12 23:20:34 +03:00
|
|
|
|
|
|
|
let get_variable = if concat_var.initial.is_some() {
|
|
|
|
let initial = concat_var.initial.as_ref().unwrap();
|
|
|
|
quote!(::itconfig::get_env_or_set_default(#env_name, #initial))
|
|
|
|
} else {
|
|
|
|
quote!(::itconfig::get_env_or_panic(#env_name))
|
|
|
|
};
|
|
|
|
|
|
|
|
tmp_vec.push(get_variable);
|
|
|
|
} else {
|
|
|
|
let part: Lit = content.parse()?;
|
|
|
|
tmp_vec.push(quote!(#part.to_string()));
|
|
|
|
}
|
2021-04-15 23:54:28 +03:00
|
|
|
|
2020-03-12 23:20:34 +03:00
|
|
|
content.parse::<Comma>().ok();
|
|
|
|
}
|
2021-04-15 23:54:28 +03:00
|
|
|
|
2020-03-12 23:20:34 +03:00
|
|
|
concat_parts = Some(tmp_vec);
|
|
|
|
} else {
|
2020-07-02 20:54:17 +03:00
|
|
|
initial = input
|
|
|
|
.parse::<FatArrow>()
|
|
|
|
.ok()
|
2020-03-12 23:20:34 +03:00
|
|
|
.and_then(|_| input.parse::<Expr>().ok());
|
|
|
|
};
|
|
|
|
|
|
|
|
input.parse::<Comma>().ok();
|
|
|
|
|
|
|
|
Ok(Variable {
|
2021-04-22 11:41:46 +03:00
|
|
|
supported_box,
|
2020-03-12 23:20:34 +03:00
|
|
|
is_static,
|
|
|
|
name,
|
|
|
|
ty,
|
|
|
|
initial,
|
|
|
|
concat_parts,
|
|
|
|
env_name: None,
|
|
|
|
meta: vec![],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|