Skip to content

Commit 4452c31

Browse files
committed
extract config env_vars helpers into separate crate
1 parent 995c6e8 commit 4452c31

File tree

5 files changed

+63
-41
lines changed

5 files changed

+63
-41
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ exclude = [
2020
".rustwide-docker",
2121
]
2222

23+
[workspace.dependencies]
24+
anyhow = { version = "1.0.42", features = ["backtrace"]}
25+
tracing = "0.1.37"
26+
2327
[dependencies]
28+
docs_rs_env_vars = { path = "crates/lib/docs_rs_env_vars" }
2429
sentry = { version = "0.46.0", features = ["panic", "tracing", "tower-http", "anyhow", "backtrace"] }
2530
log = "0.4"
26-
tracing = "0.1.37"
31+
tracing = { workspace = true }
2732
tracing-subscriber = { version = "0.3.20", default-features = false, features = ["ansi", "fmt", "json", "env-filter", "tracing-log"] }
2833
tracing-log = "0.2.0"
2934
regex = "1"
@@ -38,7 +43,7 @@ slug = "0.1.1"
3843
sqlx = { version = "0.8", features = [ "runtime-tokio", "postgres", "sqlite", "chrono" ] }
3944
url = { version = "2.1.1", features = ["serde"] }
4045
docsrs-metadata = { path = "crates/lib/metadata" }
41-
anyhow = { version = "1.0.42", features = ["backtrace"]}
46+
anyhow = { workspace = true }
4247
thiserror = "2.0.3"
4348
comrak = { version = "0.49.0", default-features = false }
4449
syntect = { version = "5.0.0", default-features = false, features = ["parsing", "html", "dump-load", "regex-onig"] }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "docs_rs_env_vars"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
anyhow = { workspace = true }
8+
tracing = { workspace = true }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use anyhow::{Context as _, Result, anyhow};
2+
use std::{env::VarError, error::Error, str::FromStr};
3+
use tracing::trace;
4+
5+
pub fn env<T>(var: &str, default: T) -> Result<T>
6+
where
7+
T: FromStr,
8+
T::Err: Error + Send + Sync + 'static,
9+
{
10+
Ok(maybe_env(var)?.unwrap_or(default))
11+
}
12+
13+
pub fn require_env<T>(var: &str) -> Result<T>
14+
where
15+
T: FromStr,
16+
<T as FromStr>::Err: Error + Send + Sync + 'static,
17+
{
18+
maybe_env(var)?.with_context(|| anyhow!("configuration variable {} is missing", var))
19+
}
20+
21+
pub fn maybe_env<T>(var: &str) -> Result<Option<T>>
22+
where
23+
T: FromStr,
24+
T::Err: Error + Send + Sync + 'static,
25+
{
26+
match std::env::var(var) {
27+
Ok(content) => Ok(content
28+
.parse::<T>()
29+
.map(Some)
30+
.with_context(|| format!("failed to parse configuration variable {var}"))?),
31+
Err(VarError::NotPresent) => {
32+
trace!("optional configuration variable {} is not set", var);
33+
Ok(None)
34+
}
35+
Err(VarError::NotUnicode(_)) => Err(anyhow!("configuration variable {} is not UTF-8", var)),
36+
}
37+
}

src/config.rs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use crate::storage::StorageKind;
2-
use anyhow::{Context, Result, anyhow, bail};
2+
use anyhow::{Result, bail};
3+
use docs_rs_env_vars::{env, maybe_env, require_env};
34
use std::{
4-
env::VarError,
5-
error::Error,
65
io,
76
path::{self, Path, PathBuf},
8-
str::FromStr,
97
time::Duration,
108
};
11-
use tracing::trace;
129
use url::Url;
1310

1411
#[derive(Debug, derive_builder::Builder)]
@@ -274,37 +271,3 @@ fn ensure_absolute_path(path: PathBuf) -> io::Result<PathBuf> {
274271
Ok(path::absolute(&path)?)
275272
}
276273
}
277-
278-
fn env<T>(var: &str, default: T) -> Result<T>
279-
where
280-
T: FromStr,
281-
T::Err: Error + Send + Sync + 'static,
282-
{
283-
Ok(maybe_env(var)?.unwrap_or(default))
284-
}
285-
286-
fn require_env<T>(var: &str) -> Result<T>
287-
where
288-
T: FromStr,
289-
<T as FromStr>::Err: Error + Send + Sync + 'static,
290-
{
291-
maybe_env(var)?.with_context(|| anyhow!("configuration variable {} is missing", var))
292-
}
293-
294-
fn maybe_env<T>(var: &str) -> Result<Option<T>>
295-
where
296-
T: FromStr,
297-
T::Err: Error + Send + Sync + 'static,
298-
{
299-
match std::env::var(var) {
300-
Ok(content) => Ok(content
301-
.parse::<T>()
302-
.map(Some)
303-
.with_context(|| format!("failed to parse configuration variable {var}"))?),
304-
Err(VarError::NotPresent) => {
305-
trace!("optional configuration variable {} is not set", var);
306-
Ok(None)
307-
}
308-
Err(VarError::NotUnicode(_)) => Err(anyhow!("configuration variable {} is not UTF-8", var)),
309-
}
310-
}

0 commit comments

Comments
 (0)