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
use std::sync::Arc;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use crate::EnvDecoder;
use super::super::ConfigLoader;
impl<T> ConfigLoader<T>
where
T: Serialize + DeserializeOwned,
{
/// Registers a built-in environment decoder for a configuration path.
///
/// This is useful for operational formats such as comma-separated lists or
/// `key=value` maps that are common in environment variables but awkward to
/// express as JSON.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), tier::ConfigError> {
/// use serde::{Deserialize, Serialize};
/// use tier::{ConfigLoader, EnvDecoder, EnvSource};
///
/// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
/// struct AppConfig {
/// no_proxy: Vec<String>,
/// }
///
/// let loaded = ConfigLoader::new(AppConfig { no_proxy: Vec::new() })
/// .env_decoder("no_proxy", EnvDecoder::Csv)
/// .env(EnvSource::from_pairs([(
/// "APP__NO_PROXY",
/// "localhost,127.0.0.1,.internal.example.com",
/// )]).prefix("APP"))
/// .load()?;
///
/// assert_eq!(
/// loaded.no_proxy,
/// vec![
/// "localhost".to_owned(),
/// "127.0.0.1".to_owned(),
/// ".internal.example.com".to_owned(),
/// ]
/// );
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn env_decoder(mut self, path: impl Into<String>, decoder: EnvDecoder) -> Self {
let path = path.into();
self.env_decoders.insert(path, decoder);
self
}
/// Registers a custom environment decoder for a configuration path.
///
/// This keeps application-specific env parsing inside `tier` without
/// requiring pre-normalization before building an [`EnvSource`](crate::EnvSource).
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), tier::ConfigError> {
/// use serde::{Deserialize, Serialize};
/// use serde_json::Value;
/// use tier::{ConfigLoader, EnvSource};
///
/// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
/// struct AppConfig {
/// no_proxy: Vec<String>,
/// }
///
/// let loaded = ConfigLoader::new(AppConfig { no_proxy: Vec::new() })
/// .env_decoder_with("no_proxy", |raw| {
/// Ok(Value::Array(
/// raw.split(';')
/// .map(str::trim)
/// .filter(|segment| !segment.is_empty())
/// .map(|segment| Value::String(segment.to_owned()))
/// .collect(),
/// ))
/// })
/// .env(EnvSource::from_pairs([("APP__NO_PROXY", "localhost;.internal")]).prefix("APP"))
/// .load()?;
///
/// assert_eq!(loaded.no_proxy, vec!["localhost", ".internal"]);
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn env_decoder_with<F>(mut self, path: impl Into<String>, decoder: F) -> Self
where
F: Fn(&str) -> Result<Value, String> + Send + Sync + 'static,
{
let path = path.into();
self.custom_env_decoders.insert(path, Arc::new(decoder));
self
}
}