Skip to content

Commit 9a336e6

Browse files
committed
Merge branch 'main' into evalir/new-pecorino-timestamp
2 parents 69c47c9 + 9b9249c commit 9a336e6

File tree

8 files changed

+288
-22
lines changed

8 files changed

+288
-22
lines changed

Cargo.toml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "init4-bin-base"
44
description = "Internal utilities for binaries produced by the init4 team"
55
keywords = ["init4", "bin", "base"]
66

7-
version = "0.9.0"
7+
version = "0.12.0"
88
edition = "2021"
99
rust-version = "1.81"
1010
authors = ["init4", "James Prestwich"]
@@ -16,8 +16,11 @@ repository = "https://github.com/init4tech/bin-base"
1616
init4-from-env-derive = "0.1.0"
1717

1818
# Signet
19-
signet-constants = { version = "0.8.0" }
20-
signet-tx-cache = { version = "0.8.0", optional = true }
19+
signet-constants = { version = "0.10.0" }
20+
signet-tx-cache = { version = "0.10.0", optional = true }
21+
22+
# alloy
23+
alloy = { version = "1.0.25", optional = true, default-features = false, features = ["std", "signer-local", "consensus", "network"] }
2124

2225
# Tracing
2326
tracing = "0.1.40"
@@ -45,7 +48,6 @@ tokio = { version = "1.36.0", optional = true }
4548

4649
# Other
4750
thiserror = "2.0.11"
48-
alloy = { version = "1.0.11", optional = true, default-features = false, features = ["std", "signer-aws", "signer-local", "consensus", "network"] }
4951
serde = { version = "1", features = ["derive"] }
5052
async-trait = { version = "0.1.80", optional = true }
5153
eyre = { version = "0.6.12", optional = true }
@@ -56,6 +58,7 @@ tower = { version = "0.5.2", optional = true }
5658
aws-config = { version = "1.1.7", optional = true }
5759
aws-sdk-kms = { version = "1.15.0", optional = true }
5860
reqwest = { version = "0.12.15", optional = true }
61+
rustls = { version = "0.23.31", optional = true }
5962

6063
[dev-dependencies]
6164
ajj = "0.3.1"
@@ -66,9 +69,11 @@ signal-hook = "0.3.17"
6669
tokio = { version = "1.43.0", features = ["macros"] }
6770

6871
[features]
69-
default = ["alloy"]
70-
alloy = ["dep:alloy", "dep:async-trait", "dep:aws-config", "dep:aws-sdk-kms"]
72+
default = ["alloy", "rustls"]
73+
alloy = ["dep:alloy"]
74+
aws = ["alloy", "alloy?/signer-aws", "dep:async-trait", "dep:aws-config", "dep:aws-sdk-kms"]
7175
perms = ["dep:oauth2", "dep:tokio", "dep:reqwest", "dep:signet-tx-cache", "dep:eyre", "dep:axum", "dep:tower"]
76+
rustls = ["dep:rustls", "rustls/aws-lc-rs"]
7277

7378
[[example]]
7479
name = "oauth"
@@ -79,3 +84,13 @@ required-features = ["perms"]
7984
name = "tx_cache"
8085
path = "examples/tx_cache.rs"
8186
required-features = ["perms"]
87+
88+
89+
# [patch.crates-io]
90+
# signet-bundle = { path = "../sdk/crates/bundle"}
91+
# signet-constants = { path = "../sdk/crates/constants"}
92+
# signet-evm = { path = "../sdk/crates/evm"}
93+
# signet-extract = { path = "../sdk/crates/extract"}
94+
# signet-tx-cache = { path = "../sdk/crates/tx-cache"}
95+
# signet-types = { path = "../sdk/crates/types"}
96+
# signet-zenith = { path = "../sdk/crates/zenith"}

from-env-derive/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "init4-from-env-derive"
33

44
description = "A derive macro for `init4_bin_base::FromEnv`"
5-
version = "0.1.1"
5+
version = "0.1.2"
66
edition = "2021"
77
rust-version = "1.81"
88
authors = ["init4", "James Prestwich"]
@@ -20,4 +20,4 @@ syn = { version = "2.0.100", features = ["full", "parsing"] }
2020
proc-macro = true
2121

2222
[dev-dependencies]
23-
init4-bin-base = "0.3"
23+
init4-bin-base = "0.9"

from-env-derive/src/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Field {
114114
return field_name.clone();
115115
}
116116

117-
let n = format!("field_{}", idx);
117+
let n = format!("field_{idx}");
118118
syn::parse_str::<Ident>(&n)
119119
.map_err(|_| syn::Error::new(self.span, "Failed to create field name"))
120120
.unwrap()
@@ -199,7 +199,7 @@ impl Field {
199199
})
200200
}
201201

202-
pub(crate) fn expand_item_from_env(&self, err_ident: &Ident, idx: usize) -> TokenStream {
202+
pub(crate) fn expand_item_from_env(&self, err_ident: &syn::Path, idx: usize) -> TokenStream {
203203
// Produces code fo the following form:
204204
// ```rust
205205
// // EITHER

from-env-derive/src/lib.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,23 @@ impl Input {
102102
}
103103
}
104104

105-
fn error_ident(&self) -> syn::Ident {
105+
fn error_ident(&self) -> syn::Path {
106+
if self.is_infallible() {
107+
return syn::parse_str("::std::convert::Infallible").unwrap();
108+
}
109+
106110
let error_name = format!("{}EnvError", self.ident);
107-
syn::parse_str::<syn::Ident>(&error_name)
111+
syn::parse_str::<syn::Path>(&error_name)
108112
.map_err(|_| {
109113
syn::Error::new(self.ident.span(), "Failed to parse error ident").to_compile_error()
110114
})
111115
.unwrap()
112116
}
113117

118+
fn is_infallible(&self) -> bool {
119+
self.error_variants().is_empty()
120+
}
121+
114122
fn error_variants(&self) -> Vec<TokenStream> {
115123
self.fields
116124
.iter()
@@ -152,6 +160,10 @@ impl Input {
152160
let error_variant_displays = self.error_variant_displays();
153161
let error_variant_sources = self.expand_variant_sources();
154162

163+
if error_variants.is_empty() {
164+
return Default::default();
165+
}
166+
155167
quote! {
156168
#[doc = "Generated error type for [`FromEnv`] for"]
157169
#[doc = #struct_name_str]
@@ -195,6 +207,7 @@ impl Input {
195207
fn expand_impl(&self) -> TokenStream {
196208
let env_item_info = self.env_item_info();
197209
let struct_name = &self.ident;
210+
198211
let error_ident = self.error_ident();
199212

200213
let item_from_envs = self.item_from_envs();
@@ -226,16 +239,25 @@ impl Input {
226239

227240
fn expand_mod(&self) -> TokenStream {
228241
// let expanded_impl = expand_impl(input);
229-
let expanded_error = self.expand_error();
230242
let expanded_impl = self.expand_impl();
231243
let crate_name = &self.crate_name;
232-
let error_ident = self.error_ident();
233244

234245
let mod_ident =
235246
syn::parse_str::<syn::Ident>(&format!("__from_env_impls_{}", self.ident)).unwrap();
236247

248+
let expanded_error = self.expand_error();
249+
250+
let use_err = if !expanded_error.is_empty() {
251+
let error_ident = self.error_ident();
252+
quote! {
253+
pub use #mod_ident::#error_ident;
254+
}
255+
} else {
256+
quote! {}
257+
};
258+
237259
quote! {
238-
pub use #mod_ident::#error_ident;
260+
#use_err
239261
mod #mod_ident {
240262
use super::*;
241263
use #crate_name::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar, EnvItemInfo};

from-env-derive/tests/macro.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
use init4_from_env_derive::FromEnv;
22

3+
// Compile check for a struct with no fallible fields (and therefore no
4+
// associated env error).
5+
#[derive(Debug, FromEnv)]
6+
pub struct InfallibleConfig {
7+
#[from_env(var = "INTERNAL", desc = "An infallible string", infallible)]
8+
pub internal: String,
9+
10+
#[from_env(var = "INTERNAL_COW", desc = "An infallible Cow<str>", infallible)]
11+
pub internal_cow: std::borrow::Cow<'static, str>,
12+
}
13+
314
#[derive(FromEnv, Debug)]
415
pub struct FromEnvTest {
516
/// This is a guy named tony
@@ -81,10 +92,10 @@ mod test {
8192
}
8293

8394
fn assert_contains(vec: &Vec<&'static EnvItemInfo>, item: &EnvItemInfo) {
84-
let item = vec.iter().find(|i| i.var == item.var).unwrap();
85-
assert_eq!(item.var, item.var);
86-
assert_eq!(item.description, item.description);
87-
assert_eq!(item.optional, item.optional);
95+
let i = vec.iter().find(|i| i.var == item.var).unwrap();
96+
assert_eq!(i.var, item.var);
97+
assert_eq!(i.description, item.description);
98+
assert_eq!(i.optional, item.optional);
8899
}
89100

90101
#[test]

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ pub mod utils {
3535
pub mod otlp;
3636

3737
#[cfg(feature = "alloy")]
38+
/// Alloy Provider configuration and instantiation
39+
pub mod provider;
40+
41+
#[cfg(feature = "aws")]
3842
/// Signer using a local private key or AWS KMS key.
3943
pub mod signer;
4044

@@ -77,5 +81,11 @@ pub mod deps {
7781
pub fn init4() -> Option<utils::otlp::OtelGuard> {
7882
let guard = utils::tracing::init_tracing();
7983
utils::metrics::init_metrics();
84+
85+
// This will install the AWS-LC-Rust TLS provider for rustls, if no other
86+
// provider has been installed yet
87+
#[cfg(feature = "rustls")]
88+
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
89+
8090
guard
8191
}

src/utils/from_env.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use signet_constants::{
33
SignetSystemConstants,
44
};
55
use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
6+
use tracing_core::metadata::ParseLevelError;
67

78
/// The `derive(FromEnv)` macro.
89
///
@@ -278,7 +279,7 @@ pub fn parse_env_if_present<T: FromStr>(env_var: &str) -> Result<T, FromEnvErr<T
278279
///
279280
pub trait FromEnv: core::fmt::Debug + Sized + 'static {
280281
/// Error type produced when loading from the environment.
281-
type Error: core::error::Error + Clone;
282+
type Error: core::error::Error + Clone + PartialEq + Eq;
282283

283284
/// Get the required environment variable names for this type.
284285
///
@@ -453,7 +454,7 @@ where
453454
/// ```
454455
pub trait FromEnvVar: core::fmt::Debug + Sized + 'static {
455456
/// Error type produced when parsing the primitive.
456-
type Error: core::error::Error;
457+
type Error: core::error::Error + Clone + PartialEq + Eq;
457458

458459
/// Load the primitive from the environment at the given variable.
459460
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>;
@@ -613,7 +614,6 @@ impl_for_parseable!(
613614
i128,
614615
isize,
615616
url::Url,
616-
tracing::Level,
617617
SignetConstants,
618618
SignetEnvironmentConstants,
619619
SignetSystemConstants,
@@ -646,6 +646,28 @@ impl FromEnvVar for bool {
646646
}
647647
}
648648

649+
/// Error type for parsing tracing levels from the environment.
650+
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
651+
#[error("failed to parse tracing level from environment variable")]
652+
pub struct LevelParseError;
653+
654+
impl From<ParseLevelError> for LevelParseError {
655+
fn from(_: ParseLevelError) -> Self {
656+
LevelParseError
657+
}
658+
}
659+
660+
impl FromEnvVar for tracing::Level {
661+
type Error = LevelParseError;
662+
663+
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
664+
let s: String = std::env::var(env_var).map_err(|e| FromEnvErr::env_err(env_var, e))?;
665+
s.parse()
666+
.map_err(Into::into)
667+
.map_err(FromEnvErr::parse_error)
668+
}
669+
}
670+
649671
#[cfg(test)]
650672
mod test {
651673
use std::{borrow::Cow, time::Duration};

0 commit comments

Comments
 (0)