Skip to content

Commit 29965db

Browse files
committed
feat: impl FromEnv for SignetConstants types
1 parent d73c20a commit 29965db

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/utils/from_env.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,41 @@ impl_for_parseable!(
613613
i128,
614614
isize,
615615
url::Url,
616-
tracing::Level,
616+
tracing::Level
617+
);
618+
619+
/// EnvItemInfo for .env variable holding chain name
620+
/// Used to implement FromEnv for SignetConstants type structs
621+
/// that can be instantiated from a single chain name
622+
static CHAIN_NAME: EnvItemInfo = EnvItemInfo {
623+
var: "CHAIN_NAME",
624+
description: "The name of the chain, e.g. `pecorino`",
625+
optional: false,
626+
};
627+
628+
macro_rules! impl_from_chain {
629+
($($t:ty),*) => {
630+
$(
631+
// first, impl FromEnvVar for the type
632+
impl_for_parseable!($t);
633+
634+
// next, impl FromEnv using FromEnvVar using the CHAIN_NAME env var
635+
impl FromEnv for $t {
636+
type Error = <$t as FromStr>::Err;
637+
638+
fn inventory() -> Vec<&'static EnvItemInfo> {
639+
vec![&CHAIN_NAME]
640+
}
641+
642+
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {
643+
Self::from_env_var(CHAIN_NAME.var)
644+
}
645+
}
646+
)*
647+
}
648+
}
649+
650+
impl_from_chain!(
617651
SignetConstants,
618652
SignetEnvironmentConstants,
619653
SignetSystemConstants,

0 commit comments

Comments
 (0)