Skip to content

Commit

Permalink
twelf: Make shellexpand dependency optional (#40)
Browse files Browse the repository at this point in the history
We don't need this functionality for our use case and the transitive dependency[0]
of the MPL licensed `option-ext` crate is undesired.

[0]: `option-ext <- dirs-sys <- dirs <- shellexpand <- twelf`
  • Loading branch information
ijc authored Mar 11, 2024
1 parent 6ba43c5 commit 7339c55
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
1 change: 1 addition & 0 deletions config-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
yaml = []
default_trait = []
custom_fn = []
shellexpand = []
34 changes: 29 additions & 5 deletions config-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand All @@ -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::<Vec<_>>().join("\n");

let content = ::twelf::reexports::shellexpand::env(&file_content)?;
#shellexpand;
(::twelf::reexports::toml::from_str(&content)?,None)
}, };
#[cfg(not(feature = "toml"))]
Expand All @@ -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::<Vec<_>>().join("\n");
let content = ::twelf::reexports::shellexpand::env(&file_content)?;
#shellexpand;
(::twelf::reexports::serde_yaml::from_str(&content)?,None)
}, };
#[cfg(not(feature = "yaml"))]
Expand All @@ -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::<Vec<_>>().join("\n");

let content = ::twelf::reexports::shellexpand::env(&file_content)?;
#shellexpand;
(::twelf::reexports::serde_dhall::from_str(&content).parse()?,None)
}, };
#[cfg(not(feature = "dhall"))]
Expand All @@ -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::<Vec<_>>().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)
}, }
Expand Down
19 changes: 10 additions & 9 deletions twelf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
25 changes: 19 additions & 6 deletions twelf/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#[cfg(all(
any(
feature = "json",
feature = "yaml",
feature = "toml",
feature = "ini",
feature = "dhall"
),
feature = "shellexpand"
))]
use std::env::VarError;

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<VarError>),
Expand Down
15 changes: 9 additions & 6 deletions twelf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 7339c55

Please sign in to comment.