diff --git a/config-derive/Cargo.toml b/config-derive/Cargo.toml index 29b98e2..cfcaa8d 100644 --- a/config-derive/Cargo.toml +++ b/config-derive/Cargo.toml @@ -35,3 +35,4 @@ yaml = [] default_trait = [] custom_fn = [] + shellexpand = [] diff --git a/config-derive/src/lib.rs b/config-derive/src/lib.rs index 401c6b5..97b41d2 100644 --- a/config-derive/src/lib.rs +++ b/config-derive/src/lib.rs @@ -282,11 +282,28 @@ fn build_env_branch(opt_struct_name: &Ident, struct_gen: &Generics) -> proc_macr },} } +#[cfg(any( + feature = "json", + feature = "yaml", + feature = "toml", + feature = "ini", + feature = "dhall" +))] +fn build_shellexpand() -> proc_macro2::TokenStream { + #[cfg(feature = "shellexpand")] + quote! { let content = ::twelf::reexports::shellexpand::env(&file_content)? } + + #[cfg(not(feature = "shellexpand"))] + quote! { let content = file_content } +} + fn build_json_branch() -> proc_macro2::TokenStream { + #[cfg(feature = "json")] + let shellexpand = build_shellexpand(); #[cfg(feature = "json")] let json_branch = quote! { ::twelf::Layer::Json(filepath) => { let file_content = std::fs::read_to_string(filepath)?; - let content = ::twelf::reexports::shellexpand::env(&file_content)?; + #shellexpand; (::twelf::reexports::serde_json::from_str(&content)?,None) }, }; #[cfg(not(feature = "json"))] @@ -295,13 +312,15 @@ fn build_json_branch() -> proc_macro2::TokenStream { } fn build_toml_branch() -> proc_macro2::TokenStream { + #[cfg(feature = "toml")] + let shellexpand = build_shellexpand(); #[cfg(feature = "toml")] let toml_branch = quote! { ::twelf::Layer::Toml(filepath) => { let file_content = std::fs::read_to_string(filepath)?; // Strip out comments (lines starting with #) let file_content = file_content.lines().filter(|line| !line.trim().starts_with("#")).collect::>().join("\n"); - let content = ::twelf::reexports::shellexpand::env(&file_content)?; + #shellexpand; (::twelf::reexports::toml::from_str(&content)?,None) }, }; #[cfg(not(feature = "toml"))] @@ -310,12 +329,14 @@ fn build_toml_branch() -> proc_macro2::TokenStream { } fn build_yaml_branch() -> proc_macro2::TokenStream { + #[cfg(feature = "yaml")] + let shellexpand = build_shellexpand(); #[cfg(feature = "yaml")] let yaml_branch = quote! { ::twelf::Layer::Yaml(filepath) => { let file_content = std::fs::read_to_string(filepath)?; // Strip out comments (lines starting with #) let file_content = file_content.lines().filter(|line| !line.trim().starts_with("#")).collect::>().join("\n"); - let content = ::twelf::reexports::shellexpand::env(&file_content)?; + #shellexpand; (::twelf::reexports::serde_yaml::from_str(&content)?,None) }, }; #[cfg(not(feature = "yaml"))] @@ -324,13 +345,15 @@ fn build_yaml_branch() -> proc_macro2::TokenStream { } fn build_dhall_branch() -> proc_macro2::TokenStream { + #[cfg(feature = "dhall")] + let shellexpand = build_shellexpand(); #[cfg(feature = "dhall")] let dhall_branch = quote! { ::twelf::Layer::Dhall(filepath) => { let file_content = std::fs::read_to_string(filepath)?; // Strip out comments (lines starting with --) let file_content = file_content.lines().filter(|line| !line.trim().starts_with("--")).collect::>().join("\n"); - let content = ::twelf::reexports::shellexpand::env(&file_content)?; + #shellexpand; (::twelf::reexports::serde_dhall::from_str(&content).parse()?,None) }, }; #[cfg(not(feature = "dhall"))] @@ -340,11 +363,12 @@ fn build_dhall_branch() -> proc_macro2::TokenStream { #[cfg(feature = "ini")] fn build_ini_branch(opt_struct_name: &Ident, struct_gen: &Generics) -> proc_macro2::TokenStream { + let shellexpand = build_shellexpand(); quote! { ::twelf::Layer::Ini(filepath) => { let file_content = std::fs::read_to_string(filepath)?; // Strip out comments (lines starting with ;) let file_content = file_content.lines().filter(|line| !line.trim().starts_with(";")).collect::>().join("\n"); - let content = ::twelf::reexports::shellexpand::env(&file_content)?; + #shellexpand; let tmp_cfg: #opt_struct_name #struct_gen = ::twelf::reexports::serde_ini::from_str(&content)?; (::twelf::reexports::serde_json::to_value(tmp_cfg)?,None) }, } diff --git a/twelf/Cargo.toml b/twelf/Cargo.toml index bb9e4a7..6a2f7ec 100644 --- a/twelf/Cargo.toml +++ b/twelf/Cargo.toml @@ -27,13 +27,14 @@ shellexpand = { version = "3.1.0", optional = true } [features] - clap = ["clap_rs", "config-derive/clap", "envy"] - default = ["env", "clap"] - dhall = ["serde_dhall", "config-derive/dhall", "shellexpand"] - env = ["envy", "config-derive/env"] - ini = ["serde_ini", "config-derive/ini", "shellexpand"] - json = ["config-derive/json", "shellexpand"] - toml = ["toml_rs", "config-derive/toml", "shellexpand"] - yaml = ["serde_yaml", "config-derive/yaml", "shellexpand"] + clap = ["clap_rs", "config-derive/clap", "envy"] + default = ["env", "clap", "shellexpand"] + shellexpand = ["dep:shellexpand", "config-derive/shellexpand"] + dhall = ["serde_dhall", "config-derive/dhall"] + env = ["envy", "config-derive/env"] + ini = ["serde_ini", "config-derive/ini"] + json = ["config-derive/json"] + toml = ["toml_rs", "config-derive/toml"] + yaml = ["serde_yaml", "config-derive/yaml"] default_trait = ["config-derive/default_trait"] - custom_fn = ["dyn-clone", "config-derive/custom_fn", "shellexpand"] + custom_fn = ["dyn-clone", "config-derive/custom_fn"] diff --git a/twelf/src/error.rs b/twelf/src/error.rs index 1480eb6..d960a90 100644 --- a/twelf/src/error.rs +++ b/twelf/src/error.rs @@ -1,3 +1,13 @@ +#[cfg(all( + any( + feature = "json", + feature = "yaml", + feature = "toml", + feature = "ini", + feature = "dhall" + ), + feature = "shellexpand" +))] use std::env::VarError; use thiserror::Error as ErrorTrait; @@ -5,12 +15,15 @@ use thiserror::Error as ErrorTrait; /// Error generated when instantiate configuration #[derive(Debug, ErrorTrait)] pub enum Error { - #[cfg(any( - feature = "json", - feature = "yaml", - feature = "toml", - feature = "ini", - feature = "dhall" + #[cfg(all( + any( + feature = "json", + feature = "yaml", + feature = "toml", + feature = "ini", + feature = "dhall" + ), + feature = "shellexpand" ))] #[error("env lookup error")] ShellExpand(#[from] shellexpand::LookupError), diff --git a/twelf/src/lib.rs b/twelf/src/lib.rs index 6643746..d940e13 100644 --- a/twelf/src/lib.rs +++ b/twelf/src/lib.rs @@ -16,12 +16,15 @@ pub mod reexports { pub use log; pub use serde; pub use serde_json; - #[cfg(any( - feature = "json", - feature = "yaml", - feature = "toml", - feature = "ini", - feature = "dhall" + #[cfg(all( + any( + feature = "json", + feature = "yaml", + feature = "toml", + feature = "ini", + feature = "dhall" + ), + feature = "shellexpand" ))] pub use shellexpand;