Skip to content

Commit

Permalink
#2: no env prefix by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Mikhailov committed Feb 28, 2023
1 parent ff31d1e commit 6c636ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
17 changes: 12 additions & 5 deletions config-manager-proc/src/utils/top_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,26 @@ pub(crate) fn extract_clap_app(attrs: &[Attribute]) -> NormalClapAppInfo {
}

pub(crate) fn extract_env_prefix(attrs: &[Attribute]) -> Option<String> {
attrs
match attrs
.iter()
.find(|a| compare_attribute_name(a, ENV_PREFIX_KEY))
.map(|atr| match atr.parse_meta() {
{
None => Some(String::new()),
Some(attr) => match attr.parse_meta() {
Err(err) => panic!("Can't parse attribute as meta: {err}"),
Ok(meta) => match meta {
Meta::Path(_) => None,
Meta::NameValue(MetaNameValue {
lit: Lit::Str(input_name),
..
}) => input_name.value(),
_ => panic!("{ENV_PREFIX_KEY} must match #[{ENV_PREFIX_KEY} = \"...\"]"),
}) => Some(input_name.value()),
_ => panic!(
"{ENV_PREFIX_KEY} must match #[{ENV_PREFIX_KEY} = \"...\"] or \
#[{ENV_PREFIX_KEY}]"
),
},
})
},
}
}

pub(crate) fn extract_debug_cmd_input(attrs: &[Attribute]) -> Option<TokenStream> {
Expand Down
23 changes: 20 additions & 3 deletions tests/parse_method/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn env() {

#[test]
fn env_prefix() {
test_env(vec![empty_prefix, no_prefix, some_prefix])
test_env(vec![empty_prefix, no_prefix, some_prefix, binary_prefix])
}

fn empty_prefix() {
Expand Down Expand Up @@ -111,7 +111,6 @@ fn some_prefix() {
});
}

/// bin file is like config-manager/target/debug/deps/parse_method-b5e125d4f8a36dad
fn no_prefix() {
#[derive(Debug, PartialEq)]
#[config(__debug_cmd_input__())]
Expand All @@ -124,5 +123,23 @@ fn no_prefix() {

set_env("fir", 1);
set_env("second", 2);
assert!(matches!(NoPrefix::parse(), Err(Error::MissingArgument(_))));
assert_ok_and_compare(&NoPrefix {
first: 1,
second: 2,
});
}

/// bin file is like config-manager/target/debug/deps/parse_method-b5e125d4f8a36dad
fn binary_prefix() {
#[config(env_prefix, __debug_cmd_input__())]
struct BinPrefix {
#[allow(dead_code)]
#[source(env)]
first: String,
}

set_env("first", 1);

let parsed = BinPrefix::parse();
assert!(matches!(parsed, Err(Error::MissingArgument(_))));
}

0 comments on commit 6c636ed

Please sign in to comment.