Skip to content

Commit

Permalink
refactor: remove env_custom
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Balashov <43530070+0x009922@users.noreply.github.com>
  • Loading branch information
0x009922 committed May 8, 2024
1 parent 83fc3fc commit 400e968
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 465 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 9 additions & 45 deletions config/base/derive/src/lib.rs
Expand Up @@ -34,14 +34,11 @@ use crate::ast::Input;
/// Supported field attributes:
///
/// - `env = "<env var name>"` - read parameter from env (bound: `T: FromEnvStr`)
/// - `env_custom` - for a type that implements `CustomEnvRead`. Conflicts with `env`.
/// - `default` - fallback to default value (bound: `T: Default`)
/// - `default = "<expr>"` - fallback to a default value specified as an expression
/// - `nested` - delegates further reading (bound: `T: ReadConfig`).
/// It uses the field name as a namespace. Conflicts with others.
///
/// A bound of `T: Deserialize` is required unless `env_only` is set.
///
/// Supported field shapes (if `nested` is not specified):
///
/// - `T` - required parameter
Expand Down Expand Up @@ -84,7 +81,7 @@ mod ast {
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use syn::{parse::ParseStream, punctuated::Punctuated, Token};

use crate::{codegen, codegen::ParameterEnv};
use crate::codegen;

// TODO: `attributes(config)` rejects all unknown fields
// it would be better to emit an error "we don't support struct attrs" instead
Expand Down Expand Up @@ -192,7 +189,7 @@ mod ast {
Nested,
Parameter {
default: Option<AttrDefault>,
env: Option<ParameterEnv>,
env: Option<syn::LitStr>,
},
}

Expand Down Expand Up @@ -220,7 +217,6 @@ mod ast {
struct Accumulator {
default: Option<AttrDefault>,
env: Option<(Span, syn::LitStr)>,
env_custom: Option<()>,
nested: Option<Span>,
}

Expand All @@ -247,7 +243,6 @@ mod ast {
AttrItem::Env(span, value) => {
reject_duplicate(&mut acc.env, span, (span, value))?
}
AttrItem::EnvCustom(span) => reject_duplicate(&mut acc.env_custom, span, ())?,
AttrItem::Nested(span) => reject_duplicate(&mut acc.nested, span, span)?,
}
}
Expand All @@ -257,7 +252,6 @@ mod ast {
nested: Some(_),
default: None,
env: None,
env_custom: None,
} => Self::Nested,
Accumulator {
nested: Some(span), ..
Expand All @@ -267,27 +261,9 @@ mod ast {
"attributes conflict: `nested` cannot be set with other attributes",
))
}
Accumulator {
env: Some((span, _)),
env_custom: Some(()),
..
} => {
return Err(syn::Error::new(
span,
"attributes conflict: set either `env` or `env_custom`, not both",
))
}
Accumulator {
default,
env_custom: Some(()),
..
} => Self::Parameter {
default,
env: Some(ParameterEnv::Custom),
},
Accumulator { default, env, .. } => Self::Parameter {
default,
env: env.map(|(_, lit)| ParameterEnv::Plain(lit)),
env: env.map(|(_, lit)| lit),
},
};

Expand All @@ -300,15 +276,14 @@ mod ast {
enum AttrItem {
Default(Span, AttrDefault),
Env(Span, syn::LitStr),
EnvCustom(Span),
Nested(Span),
}

impl syn::parse::Parse for AttrItem {
fn parse(input: ParseStream) -> syn::Result<Self> {
input.step(|cursor| {
const EXPECTED_IDENT: &str =
"unexpected token; expected `default`, `env`, `env_only`, or `nested`";
"unexpected token; expected `default`, `env`, or `nested`";

let Some((ident, cursor)) = cursor.ident() else {
Err(syn::Error::new(cursor.span(), EXPECTED_IDENT))?
Expand Down Expand Up @@ -337,9 +312,6 @@ mod ast {
};
Ok((Self::Env(ident.span(), lit), cursor))
}
"env_custom" => {
Ok((Self::EnvCustom(ident.span()), cursor))
}
other => Err(syn::Error::new(cursor.span(), EXPECTED_IDENT)),
}
})
Expand Down Expand Up @@ -485,7 +457,7 @@ mod ast {

let Attrs::Parameter {
default: Some(AttrDefault::Flag),
env: Some(ParameterEnv::Plain(var)),
env: Some(var),
} = attrs
else {
panic!("expectation failed")
Expand Down Expand Up @@ -597,10 +569,8 @@ mod codegen {
let mut read = quote! {
let #ident = __reader.read_parameter([stringify!(#ident)])
};
match env {
Some(ParameterEnv::Plain(var)) => read.extend(quote! { .env(#var) }),
Some(ParameterEnv::Custom) => read.extend(quote! { .env_custom() }),
None => {}
if let Some(var) = env {
read.extend(quote! { .env(#var) })
}
read.extend(match evaluation {
Evaluation::Required => quote! { .value_required() },
Expand Down Expand Up @@ -631,19 +601,13 @@ mod codegen {

pub enum EntryKind {
Parameter {
env: Option<ParameterEnv>,
env: Option<syn::LitStr>,
evaluation: Evaluation,
with_origin: bool,
},
Nested,
}

#[derive(Debug)]
pub enum ParameterEnv {
Plain(syn::LitStr),
Custom,
}

pub enum Evaluation {
Required,
OrElse(syn::Expr),
Expand All @@ -663,7 +627,7 @@ mod codegen {
let entry = Entry {
ident: parse_quote!(test),
kind: EntryKind::Parameter {
env: Some(ParameterEnv::Plain(parse_quote!("TEST_ENV"))),
env: Some(parse_quote!("TEST_ENV")),
evaluation: Evaluation::Required,
with_origin: false,
},
Expand Down
@@ -1,4 +1,4 @@
error: unexpected token; expected `default`, `env`, `env_only`, or `nested`
error: unexpected token; expected `default`, `env`, or `nested`
--> tests/ui_fail/invalid_attrs_commas.rs:5:14
|
5 | #[config(,,,)]
Expand Down
8 changes: 1 addition & 7 deletions config/base/derive/tests/ui_fail/invalid_attrs_conflicts.rs
Expand Up @@ -2,7 +2,7 @@ use iroha_config_base::ReadConfig;

#[derive(ReadConfig)]
struct Test {
#[config(nested, env_custom)]
#[config(nested, default)]
foo: u64,
}

Expand All @@ -12,10 +12,4 @@ struct Test2 {
foo: u64,
}

#[derive(ReadConfig)]
struct Test3 {
#[config(env = "FASDF", env_custom)]
foo: u64,
}

pub fn main() {}
@@ -1,17 +1,11 @@
error: attributes conflict: `nested` cannot be set with other attributes
--> tests/ui_fail/invalid_attrs_conflicts.rs:5:14
|
5 | #[config(nested, env_custom)]
5 | #[config(nested, default)]
| ^^^^^^

error: attributes conflict: `nested` cannot be set with other attributes
--> tests/ui_fail/invalid_attrs_conflicts.rs:11:23
|
11 | #[config(default, nested)]
| ^^^^^^

error: attributes conflict: set either `env` or `env_custom`, not both
--> tests/ui_fail/invalid_attrs_conflicts.rs:17:14
|
17 | #[config(env = "FASDF", env_custom)]
| ^^^
22 changes: 1 addition & 21 deletions config/base/derive/tests/ui_pass/happy_path.rs
@@ -1,9 +1,4 @@
use std::convert::Infallible;

use iroha_config_base::{
read::{CustomEnvFetcher, CustomEnvRead, CustomEnvReadError},
ReadConfig, WithOrigin,
};
use iroha_config_base::{ReadConfig, WithOrigin};

#[derive(ReadConfig)]
struct Test {
Expand All @@ -21,26 +16,11 @@ struct Test {
nested: Nested,
#[config(env = "TEST", default = "true")]
with_default_expr_and_env: bool,
#[config(env_custom)]
foo_bar: FooBar,
}

#[derive(ReadConfig)]
struct Nested {
foo: Option<u32>,
}

#[derive(serde::Deserialize)]
struct FooBar(u32);

impl CustomEnvRead for FooBar {
type Context = Infallible;

fn read(
_fetcher: &mut CustomEnvFetcher,
) -> Result<Option<Self>, CustomEnvReadError<Self::Context>> {
todo!();
}
}

pub fn main() {}

0 comments on commit 400e968

Please sign in to comment.