feat: add support optional vec

This commit is contained in:
Dmitriy Pleshevskiy 2021-04-22 23:14:48 +03:00
parent 70b670b07c
commit c21f265eea
4 changed files with 45 additions and 12 deletions

View file

@ -138,7 +138,7 @@ impl ToTokens for Variable {
}}
} else if let Some(initial) = &self.initial {
match self.supported_box.clone() {
Some(SupportedBox::Vec { sep }) => {
Some(SupportedBox::Vec(sep)) => {
let sep = &sep.unwrap_or_else(|| String::from(","));
quote!(::itconfig::get_vec_env_or_set_default(#env_name, #sep, #initial))
}
@ -149,11 +149,15 @@ impl ToTokens for Variable {
Some(SupportedBox::Option) => {
quote!(::itconfig::maybe_get_env(#env_name))
}
Some(SupportedBox::Vec { sep }) => {
Some(SupportedBox::OptionVec(sep)) => {
let sep = &sep.unwrap_or_else(|| String::from(","));
quote!(::itconfig::maybe_get_vec_env(#env_name, #sep))
}
Some(SupportedBox::Vec(sep)) => {
let sep = &sep.unwrap_or_else(|| String::from(","));
quote!(::itconfig::get_vec_env_or_panic(#env_name, #sep))
}
_ => {
None => {
quote!(::itconfig::get_env_or_panic(#env_name))
}
}

View file

@ -75,9 +75,9 @@ fn parse_namespace_content(
variable.env_name = parse_attribute(attr, "env_name", &variable.env_name)?;
} else {
match variable.supported_box {
Some(SupportedBox::Vec { sep: current_sep }) if attr.path.is_ident("sep") => {
Some(SupportedBox::Vec(current_sep)) if attr.path.is_ident("sep") => {
let sep = parse_attribute(attr, "sep", &current_sep)?;
variable.supported_box = Some(SupportedBox::Vec { sep });
variable.supported_box = Some(SupportedBox::Vec(sep));
}
_ => variable.meta.push(attr),
}

View file

@ -1,14 +1,15 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::ToTokens;
use syn::{Path, Type};
use syn::{GenericArgument, Path, PathArguments, Type};
const OPTION_PATH_IDENTS: &[&str] = &["Option|", "std|option|Option|", "core|option|Option|"];
const VEC_PATH_IDENTS: &[&str] = &["Vec|", "std|vec|Vec|"];
#[derive(Debug, Clone)]
pub enum SupportedBox {
Vec { sep: Option<String> },
Vec(Option<String>),
Option,
OptionVec(Option<String>),
}
pub fn vec_to_token_stream_2<T>(input: &[T]) -> Vec<TokenStream2>
@ -40,11 +41,24 @@ fn is_vec_path_ident(path_ident: &str) -> bool {
pub fn maybe_supported_box(ty: &Type) -> Option<SupportedBox> {
match ty {
Type::Path(ty_path) if ty_path.qself.is_none() => {
let path_ident = path_ident(&ty_path.path);
if is_option_path_ident(&path_ident) {
let ty_path_ident = path_ident(&ty_path.path);
if is_option_path_ident(&ty_path_ident) {
match &ty_path.path.segments.iter().last().unwrap().arguments {
PathArguments::AngleBracketed(params) => match params.args.first() {
Some(GenericArgument::Type(Type::Path(inner_ty_path))) => {
let ty_path_ident = path_ident(&inner_ty_path.path);
if is_vec_path_ident(&ty_path_ident) {
Some(SupportedBox::OptionVec(None))
} else {
Some(SupportedBox::Option)
} else if is_vec_path_ident(&path_ident) {
Some(SupportedBox::Vec { sep: None })
}
}
_ => Some(SupportedBox::Option),
},
_ => Some(SupportedBox::Option),
}
} else if is_vec_path_ident(&ty_path_ident) {
Some(SupportedBox::Vec(None))
} else {
None
}

View file

@ -596,3 +596,18 @@ mod test_case_25 {
assert_eq!(config::CUSTOM_SEP_STD_VEC(), vec!["paypal", "stripe"]);
}
}
mod test_case_26 {
use std::env;
itconfig::config! {
OPTION_VEC: Option<Vec<&'static str>>,
}
#[test]
fn optional_vec() {
env::set_var("OPTION_VEC", "paypal,stripe");
assert_eq!(config::OPTION_VEC(), Some(vec!["paypal", "stripe"]));
}
}