Skip to content

Commit 697f776

Browse files
authored
feat: map env errs (#5)
1 parent f0bdff7 commit 697f776

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
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.1.2"
7+
version = "0.1.3"
88
edition = "2021"
99
rust-version = "1.81"
1010
authors = ["init4", "James Prestwich"]

examples/ajj.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3434
tracing::info!("serving hello world");
3535
Ok::<_, ()>("Hello, world!")
3636
})
37+
.route("addNumbers", |(a, b): (u32, u32)| async move {
38+
tracing::info!("serving addNumbers");
39+
Ok::<_, ()>(a + b)
40+
})
3741
.into_axum("/rpc");
3842

3943
let listener = tokio::net::TcpListener::bind("localhost:8080")

src/utils/from_env.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ impl<Inner> FromEnvErr<Inner> {
3939
}
4040
}
4141

42+
/// Map the error to another type. This is useful for converting the error
43+
/// type to a different type, while keeping the other error information
44+
/// intact.
45+
pub fn map<New>(self, f: impl FnOnce(Inner) -> New) -> FromEnvErr<New> {
46+
match self {
47+
Self::EnvError(s, e) => FromEnvErr::EnvError(s, e),
48+
Self::Empty(s) => FromEnvErr::Empty(s),
49+
Self::ParseError(e) => FromEnvErr::ParseError(f(e)),
50+
}
51+
}
52+
4253
/// Missing env var.
4354
pub fn env_err(var: &str, e: VarError) -> Self {
4455
Self::EnvError(var.to_string(), e)

src/utils/otlp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ impl OtelConfig {
134134
///
135135
/// The env vars it checks are:
136136
/// - `OTEL_EXPORTER_OTLP_ENDPOINT` - optional. The endpoint to send traces
137-
/// to. If missing or unparsable, this function will return [`None`], and
138-
/// OTLP exporting will be disabled.
137+
/// to. If missing or unparsable, this function will return [`None`], and
138+
/// OTLP exporting will be disabled.
139139
/// - `OTEL_LEVEL` - optional. Specifies the minimum [`tracing::Level`] to
140140
/// export. Defaults to [`tracing::Level::DEBUG`].
141141
/// - `OTEL_TIMEOUT` - optional. Specifies the timeout for the exporter in

0 commit comments

Comments
 (0)