tork-core 0.1.0

Core runtime for the Tork web framework: HTTP server, routing, dependency injection, responses, and errors, built on Hyper and Tokio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Typed application configuration.
//!
//! [`SettingsLoader`] merges configuration from several sources into one typed,
//! validated value, loaded once at startup. The sources, from lowest to highest
//! precedence, are: struct defaults, a base config file, environment-specific
//! files, a `.env` file, environment variables, a secrets directory, and explicit
//! overrides. [`SecretString`] holds sensitive values without exposing them in
//! logs.
//!
//! The `#[settings]` macro generates a struct that derives `Deserialize` and
//! `garde::Validate` and a `load()` method built on this loader, but the loader is
//! usable directly with any `DeserializeOwned + garde::Validate` type.

use std::marker::PhantomData;
use std::path::{Path, PathBuf};

use figment::providers::{Env, Format, Serialized, Toml};
use figment::Figment;
use garde::Validate;
use serde::de::DeserializeOwned;
use serde_json::{Map, Value};

use crate::error::{Error, Result};

/// Placeholder rendered as a masked secret value.
const REDACTED: &str = "********";
/// Default environment name when none is set.
const DEFAULT_ENVIRONMENT: &str = "development";
/// Separator marking a nesting level in an environment variable or secret name.
const NESTING_SEPARATOR: &str = "__";
/// Token replaced with the resolved environment name in a file path.
const ENV_PLACEHOLDER: &str = "{env}";

/// A string whose value is kept out of logs and debug output.
///
/// `Debug` and `Display` render a fixed mask; the value is only readable through
/// [`SecretString::expose`]. It deserializes from a plain string, so it can stand
/// in for any secret configuration field. It is intentionally not `Serialize`, so
/// a configuration carrying secrets cannot be written back out by accident.
#[derive(Clone, serde::Deserialize)]
pub struct SecretString(String);

impl SecretString {
    /// Wraps a value as a secret.
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    /// Returns the underlying value. This is the only way to read a secret, so
    /// call sites that expose it are easy to audit.
    pub fn expose(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Debug for SecretString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("SecretString").field(&REDACTED).finish()
    }
}

impl std::fmt::Display for SecretString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(REDACTED)
    }
}

impl From<String> for SecretString {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for SecretString {
    fn from(value: &str) -> Self {
        Self(value.to_owned())
    }
}

/// Loads and validates a typed configuration from layered sources.
///
/// Build it with the source methods, then call [`SettingsLoader::load`]. Each
/// source overrides the ones before it; see the module documentation for the full
/// precedence order.
pub struct SettingsLoader<T> {
    env_file: Option<PathBuf>,
    prefix: Option<String>,
    config_file: Option<String>,
    files: Vec<String>,
    secrets_dir: Option<PathBuf>,
    overrides: Map<String, Value>,
    _marker: PhantomData<fn() -> T>,
}

impl<T> Default for SettingsLoader<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> SettingsLoader<T> {
    /// Creates a loader with no sources configured.
    pub fn new() -> Self {
        Self {
            env_file: None,
            prefix: None,
            config_file: None,
            files: Vec::new(),
            secrets_dir: None,
            overrides: Map::new(),
            _marker: PhantomData,
        }
    }

    /// Loads variables from this `.env` file before reading the environment.
    /// Existing environment variables are not overwritten.
    pub fn env_file(mut self, path: impl Into<PathBuf>) -> Self {
        self.env_file = Some(path.into());
        self
    }

    /// Reads environment variables that start with `prefix` followed by `_`.
    /// Nested fields use `__` between levels (for example `APP_SERVER__PORT`).
    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = Some(prefix.into());
        self
    }

    /// Reads a base configuration file (TOML). A missing file is ignored.
    pub fn config_file(mut self, path: impl Into<String>) -> Self {
        self.config_file = Some(path.into());
        self
    }

    /// Appends an environment-specific file (TOML). The `{env}` token in the path
    /// is replaced with the resolved environment name. A missing file is ignored.
    pub fn file(mut self, path: impl Into<String>) -> Self {
        self.files.push(path.into());
        self
    }

    /// Reads one secret per file from a directory: the file name is the key (with
    /// `__` marking nesting) and the trimmed contents are the value.
    pub fn secrets_dir(mut self, dir: impl Into<PathBuf>) -> Self {
        self.secrets_dir = Some(dir.into());
        self
    }

    /// Sets the highest-priority override for a key, overriding every source.
    /// The key uses `.` between nesting levels (for example `server.port`).
    pub fn override_value(mut self, key: impl AsRef<str>, value: impl serde::Serialize) -> Self {
        if let Ok(value) = serde_json::to_value(value) {
            let parts: Vec<&str> = key.as_ref().split('.').collect();
            insert_nested(&mut self.overrides, &parts, value);
        }
        self
    }

    /// Resolves the current environment name from `{PREFIX}_ENV` (or `ENV` when no
    /// prefix is set), defaulting to `development`.
    fn environment_name(&self) -> String {
        let var = match &self.prefix {
            Some(prefix) => format!("{prefix}_ENV"),
            None => "ENV".to_owned(),
        };
        std::env::var(&var).unwrap_or_else(|_| DEFAULT_ENVIRONMENT.to_owned())
    }

    /// Builds the environment-variable provider with the configured prefix.
    fn env_provider(&self) -> Env {
        match &self.prefix {
            Some(prefix) => Env::prefixed(&format!("{prefix}_")).split(NESTING_SEPARATOR),
            None => Env::raw().split(NESTING_SEPARATOR),
        }
    }
}

impl<T: DeserializeOwned + Validate<Context = ()>> SettingsLoader<T> {
    /// Merges every source, deserializes into `T`, and validates it.
    ///
    /// Returns an error with code `CONFIG_LOAD_FAILED` when a source cannot be
    /// parsed into `T`, or `CONFIG_VALIDATION_ERROR` (with field details) when the
    /// value fails validation. Used at startup, either aborts before the app runs.
    pub fn load(self) -> Result<T> {
        // `dotenvy` writes into the process environment via `set_var`, which races
        // with any concurrent environment read/write from another thread. Hold the
        // shared environment lock across the `.env` load and the environment reads
        // (env name + the figment env provider) so concurrent `load()` calls
        // serialize instead of racing. Mutating `std::env` from outside the loader
        // while it runs is still the caller's responsibility to avoid.
        let _env_guard = crate::env::env_guard();
        self.load_locked()
    }

    /// Loads the configuration assuming the caller already holds the shared
    /// environment lock (see [`load`](SettingsLoader::load)).
    ///
    /// Kept separate so callers that must hold the lock across their own
    /// environment mutations (the tests below) do not deadlock by re-locking a
    /// non-reentrant mutex inside `load`.
    fn load_locked(self) -> Result<T> {
        // Load the `.env` file into the process environment. dotenvy does not
        // overwrite existing variables, so a real environment variable still wins.
        match &self.env_file {
            Some(path) => {
                let _ = dotenvy::from_path(path);
            }
            None => {
                let _ = dotenvy::dotenv();
            }
        }

        let environment = self.environment_name();

        let mut figment = Figment::new();
        if let Some(config_file) = &self.config_file {
            figment = figment.merge(Toml::file(config_file));
        }
        for file in &self.files {
            let resolved = file.replace(ENV_PLACEHOLDER, &environment);
            figment = figment.merge(Toml::file(resolved));
        }
        figment = figment.merge(self.env_provider());
        if let Some(dir) = &self.secrets_dir {
            let secrets = read_secrets(dir);
            if !secrets.is_empty() {
                figment = figment.merge(Serialized::defaults(Value::Object(secrets)));
            }
        }
        if !self.overrides.is_empty() {
            figment = figment.merge(Serialized::defaults(Value::Object(self.overrides.clone())));
        }

        let value: T = figment.extract().map_err(|error| {
            let message = error.to_string();
            Error::internal(format!("failed to load configuration: {message}"))
                .with_code("CONFIG_LOAD_FAILED")
                .with_source(error)
        })?;

        value.validate().map_err(|report| {
            Error::from_garde_report(report).with_code("CONFIG_VALIDATION_ERROR")
        })?;

        Ok(value)
    }
}

/// Reads a secrets directory into a nested map keyed by file name.
fn read_secrets(dir: &Path) -> Map<String, Value> {
    let mut root = Map::new();
    let Ok(entries) = std::fs::read_dir(dir) else {
        return root;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
            continue;
        };
        let Ok(contents) = std::fs::read_to_string(&path) else {
            continue;
        };
        let key = name.to_lowercase();
        let parts: Vec<&str> = key.split(NESTING_SEPARATOR).collect();
        insert_nested(&mut root, &parts, Value::String(contents.trim().to_owned()));
    }
    root
}

/// Inserts `value` into `root` along a path of nested object keys.
fn insert_nested(root: &mut Map<String, Value>, parts: &[&str], value: Value) {
    match parts {
        [] => {}
        [key] => {
            root.insert((*key).to_owned(), value);
        }
        [key, rest @ ..] => {
            let entry = root
                .entry((*key).to_owned())
                .or_insert_with(|| Value::Object(Map::new()));
            if !entry.is_object() {
                *entry = Value::Object(Map::new());
            }
            if let Value::Object(map) = entry {
                insert_nested(map, rest, value);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::env::env_guard;
    use garde::Validate;
    use serde::Deserialize;
    use std::io::Write;

    #[derive(Debug, Deserialize, Validate)]
    struct Nested {
        #[garde(skip)]
        host: String,
        #[garde(range(min = 1, max = 65535))]
        port: u16,
    }

    #[derive(Debug, Deserialize, Validate)]
    struct Sample {
        #[serde(default = "default_name")]
        #[garde(skip)]
        name: String,
        #[garde(range(min = 1, max = 500))]
        items: u32,
        #[garde(dive)]
        nested: Nested,
        #[garde(skip)]
        token: SecretString,
    }

    fn default_name() -> String {
        "Awesome API".to_owned()
    }

    #[test]
    fn builder_methods_store_configuration_sources() {
        let loader = SettingsLoader::<Sample>::default()
            .env_file("config/.env.test")
            .prefix("CFGTESTZ")
            .config_file("config/base.toml")
            .file("config/{env}.toml")
            .secrets_dir("secrets")
            .override_value("nested.port", 7000u16);

        assert_eq!(
            loader.env_file.as_deref(),
            Some(Path::new("config/.env.test"))
        );
        assert_eq!(loader.prefix.as_deref(), Some("CFGTESTZ"));
        assert_eq!(loader.config_file.as_deref(), Some("config/base.toml"));
        assert_eq!(loader.files, vec!["config/{env}.toml"]);
        assert_eq!(loader.secrets_dir.as_deref(), Some(Path::new("secrets")));
        assert_eq!(loader.overrides["nested"]["port"], Value::from(7000u16));
    }

    #[test]
    fn environment_name_uses_prefix_and_default() {
        let _guard = env_guard();
        std::env::remove_var("ENV");
        std::env::remove_var("CFGTESTENV_ENV");
        assert_eq!(
            SettingsLoader::<Sample>::new().environment_name(),
            "development"
        );

        std::env::set_var("ENV", "staging");
        std::env::set_var("CFGTESTENV_ENV", "production");
        assert_eq!(
            SettingsLoader::<Sample>::new().environment_name(),
            "staging"
        );
        assert_eq!(
            SettingsLoader::<Sample>::new()
                .prefix("CFGTESTENV")
                .environment_name(),
            "production"
        );

        std::env::remove_var("ENV");
        std::env::remove_var("CFGTESTENV_ENV");
    }

    #[test]
    fn read_secrets_and_insert_nested_cover_edge_cases() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("nested")).unwrap();
        std::fs::write(dir.path().join("TOKEN"), "  shh \n").unwrap();
        std::fs::write(dir.path().join("DB__PORT"), "5432").unwrap();

        let secrets = read_secrets(dir.path());
        assert_eq!(secrets["token"], "shh");
        assert_eq!(secrets["db"]["port"], "5432");
        assert!(read_secrets(&dir.path().join("missing")).is_empty());

        let mut root = Map::new();
        insert_nested(&mut root, &[], Value::from("ignored"));
        assert!(root.is_empty());

        root.insert("db".to_owned(), Value::from("scalar"));
        insert_nested(&mut root, &["db", "host"], Value::from("localhost"));
        assert_eq!(root["db"]["host"], "localhost");
    }

    #[test]
    fn secret_string_is_masked_but_exposable() {
        let secret = SecretString::new("super-secret");
        assert_eq!(format!("{secret:?}"), "SecretString(\"********\")");
        assert_eq!(format!("{secret}"), "********");
        assert_eq!(secret.expose(), "super-secret");
    }

    #[test]
    fn defaults_apply_and_overrides_win() {
        // A unique prefix keeps this test independent of other tests' env vars.
        let value: Sample = SettingsLoader::new()
            .prefix("CFGTESTA")
            .override_value("items", 42u32)
            .override_value("nested.host", "localhost")
            .override_value("nested.port", 8080u16)
            .override_value("token", "shh")
            .load()
            .expect("load should succeed");

        assert_eq!(value.name, "Awesome API"); // serde default
        assert_eq!(value.items, 42);
        assert_eq!(value.nested.host, "localhost");
        assert_eq!(value.nested.port, 8080);
        assert_eq!(value.token.expose(), "shh");
    }

    #[test]
    fn environment_variable_overrides_and_nests() {
        let _guard = env_guard();
        // The prefix is unique to this test, so the variables do not collide.
        std::env::set_var("CFGTESTB_NAME", "From Env");
        std::env::set_var("CFGTESTB_ITEMS", "7");
        std::env::set_var("CFGTESTB_NESTED__HOST", "db.internal");
        std::env::set_var("CFGTESTB_NESTED__PORT", "5432");
        std::env::set_var("CFGTESTB_TOKEN", "envtoken");

        let value: Sample = SettingsLoader::new()
            .prefix("CFGTESTB")
            // The guard is already held above, so use the non-locking variant.
            .load_locked()
            .expect("load should succeed");

        assert_eq!(value.name, "From Env");
        assert_eq!(value.items, 7);
        assert_eq!(value.nested.host, "db.internal");
        assert_eq!(value.nested.port, 5432);
        assert_eq!(value.token.expose(), "envtoken");

        for key in ["NAME", "ITEMS", "NESTED__HOST", "NESTED__PORT", "TOKEN"] {
            std::env::remove_var(format!("CFGTESTB_{key}"));
        }
    }

    #[test]
    fn environment_variable_overrides_a_config_file() {
        let _guard = env_guard();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        let mut file = std::fs::File::create(&path).unwrap();
        writeln!(
            file,
            "name = \"From File\"\nitems = 3\ntoken = \"filetoken\"\n\n[nested]\nhost = \"file.host\"\nport = 1111"
        )
        .unwrap();

        // The environment value must win over the file value.
        std::env::set_var("CFGTESTD_ITEMS", "9");

        let value: Sample = SettingsLoader::new()
            .prefix("CFGTESTD")
            .config_file(path.to_string_lossy().into_owned())
            // The guard is already held above, so use the non-locking variant.
            .load_locked()
            .expect("load should succeed");

        assert_eq!(value.name, "From File"); // only in the file
        assert_eq!(value.items, 9); // env overrides the file
        assert_eq!(value.nested.host, "file.host");
        assert_eq!(value.nested.port, 1111);

        std::env::remove_var("CFGTESTD_ITEMS");
    }

    #[test]
    fn validation_failure_is_reported() {
        let error = SettingsLoader::<Sample>::new()
            .prefix("CFGTESTC")
            .override_value("items", 9999u32) // exceeds max of 500
            .override_value("nested.host", "localhost")
            .override_value("nested.port", 80u16)
            .override_value("token", "shh")
            .load()
            .unwrap_err();

        assert_eq!(error.code(), "CONFIG_VALIDATION_ERROR");
    }

    #[test]
    fn load_merges_env_file_environment_specific_file_and_secrets() {
        let _guard = env_guard();
        let dir = tempfile::tempdir().unwrap();
        let base = dir.path().join("base.toml");
        let env_file = dir.path().join(".env");
        let env_toml = dir.path().join("production.toml");
        let secrets = dir.path().join("secrets");
        std::fs::create_dir(&secrets).unwrap();

        let mut base_file = std::fs::File::create(&base).unwrap();
        writeln!(
            base_file,
            "name = \"Base\"\nitems = 3\ntoken = \"from-base\"\n\n[nested]\nhost = \"file.host\"\nport = 1111"
        )
        .unwrap();
        std::fs::write(&env_file, "CFGTESTE_ENV=production\nCFGTESTE_ITEMS=9\n").unwrap();
        let mut env_override = std::fs::File::create(&env_toml).unwrap();
        writeln!(
            env_override,
            "name = \"Prod\"\n[nested]\nhost = \"prod.host\""
        )
        .unwrap();
        std::fs::write(secrets.join("TOKEN"), "from-secret\n").unwrap();

        let value: Sample = SettingsLoader::new()
            .env_file(&env_file)
            .prefix("CFGTESTE")
            .config_file(base.to_string_lossy().into_owned())
            .file(dir.path().join("{env}.toml").to_string_lossy().into_owned())
            .secrets_dir(&secrets)
            // The guard is already held above, so use the non-locking variant.
            .load_locked()
            .expect("load should succeed");

        assert_eq!(value.name, "Prod");
        assert_eq!(value.items, 9);
        assert_eq!(value.nested.host, "prod.host");
        assert_eq!(value.nested.port, 1111);
        assert_eq!(value.token.expose(), "from-secret");

        std::env::remove_var("CFGTESTE_ENV");
        std::env::remove_var("CFGTESTE_ITEMS");
    }

    #[test]
    fn load_reports_configuration_parse_failures() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("broken.toml");
        std::fs::write(
            &path,
            "items = \"oops\"\ntoken = \"x\"\n[nested]\nhost = \"h\"\nport = 1",
        )
        .unwrap();

        let error = SettingsLoader::<Sample>::new()
            .config_file(path.to_string_lossy().into_owned())
            .load()
            .unwrap_err();

        assert_eq!(error.code(), "CONFIG_LOAD_FAILED");
        assert!(error.message().starts_with("failed to load configuration:"));
    }
}