c4-config 0.5.0

Load a folder of config files (jsonc/yaml/json/toml/ini/env/csv/xlsx/ods) into one deep-merged value
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
//! The loader: scan sources, pick parsers by extension, deep-merge with
//! per-leaf provenance. `load()` is `trace()` minus the labels — one
//! code path for both.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::de::DeserializeOwned;

use crate::trace::{SourceRef, TracedValue};
use crate::{Error, Format, FormatKind, Options, Order, Result, Source, Value, de, format};

/// Loads and merges the config sources named by its [`Options`].
#[derive(Debug, Clone, Default)]
pub struct Loader {
    pub options: Options,
}

impl Loader {
    /// Everything — sources included — comes in through [`Options`];
    /// there is no separate builder step.
    pub fn new(options: Options) -> Self {
        Self { options }
    }

    /// Load and merge all sources, deserializing into `T`.
    ///
    /// [`Value`] implements `Deserialize`, so `T = Value` gives dynamic
    /// access and any other serde type gives typed config.
    pub fn load<T: DeserializeOwned>(&self) -> Result<T> {
        let value = untrace(self.trace()?);
        T::deserialize(de::ValueDeserializer::new(value))
            .map_err(|e| Error::Deserialize(e.to_string()))
    }

    /// Like [`Loader::load`], but keeps per-leaf provenance — see
    /// [`TracedValue`]. This powers the CLI `--trace` mode; source labels
    /// are a debug/testing aid, not config data.
    ///
    /// ```
    /// use c4::{CustomFormat, Loader, TracedValue};
    ///
    /// # fn main() -> Result<(), c4::Error> {
    /// // a custom-format string source keeps this example runnable
    /// // under every feature combination (`Loader::default()` would
    /// // read ./config instead)
    /// let kv = CustomFormat::new("kv", ["kv"], |text, path, options| {
    ///     let rows = text
    ///         .lines()
    ///         .map(|line| line.split('=').map(str::to_owned).collect())
    ///         .collect();
    ///     c4::parse_table(rows, &c4::TableLayout::Kv, path, options)
    /// });
    ///
    /// let traced = Loader::new(c4::Options {
    ///     sources: vec![(kv, "port=8080").into()],
    ///     ..c4::Options::default()
    /// })
    /// .trace()?;
    /// let TracedValue::Object(root) = &traced else { unreachable!() };
    /// let TracedValue::Leaf { value, source } = &root["port"] else { unreachable!() };
    /// assert_eq!(value.as_u64(), Some(8080));
    /// assert_eq!(source, &c4::SourceRef::String(0)); // "string:0" in traces
    /// # Ok(())
    /// # }
    /// ```
    pub fn trace(&self) -> Result<TracedValue> {
        let extensions = extension_map(&self.options);
        let mut root = TracedValue::Object(BTreeMap::new());

        for (index, source) in self.options.sources.iter().enumerate() {
            match source {
                // one path source: a folder whose files merge, or a single
                // file — detected here (same rule as `c4::load`)
                Source::Path(path) if path.is_dir() => {
                    self.load_folder(path, &extensions, &mut root)?;
                }
                Source::Path(path) if path.is_file() => {
                    let claim = claimed_format(path, &extensions).ok_or_else(|| Error::Parse {
                        path: path.clone(),
                        message: "no active format claims this file's extension".into(),
                    })?;
                    let value = self.parse_file(claim, path)?;
                    if !matches!(value, Value::Null) {
                        self.merge(&mut root, value, &SourceRef::File(path.clone()));
                    }
                }
                Source::Path(path) => return Err(Error::NotFound(path.clone())),
                Source::Table {
                    path,
                    format,
                    sheet,
                    layout,
                } => {
                    // one file of a table format, read under an explicit
                    // layout (and, for spreadsheets, an explicit sheet)
                    if !path.is_file() {
                        return Err(Error::Parse {
                            path: path.clone(),
                            message: "table source is not a file (table sources read \
                                      exactly one file; for in-code text use a \
                                      (format, text) string source)"
                                .into(),
                        });
                    }
                    let value = format::parse_table_file(
                        *format,
                        path,
                        sheet.as_deref(),
                        layout,
                        &self.options,
                    )?;
                    if !matches!(value, Value::Null) {
                        self.merge(&mut root, value, &SourceRef::File(path.clone()));
                    }
                }
                Source::Value(result) => {
                    let mut value = result.clone().map_err(|message| Error::Parse {
                        path: PathBuf::from(format!("value:{index}")),
                        message,
                    })?;
                    format::strip_commented_data_keys(&mut value, &self.options);
                    if !matches!(value, Value::Null) {
                        self.merge(&mut root, value, &SourceRef::Value(index));
                    }
                }
                Source::String { format, content } => {
                    let label = PathBuf::from(format!("string:{index}"));
                    let value = match format {
                        // `format::parse` filters commented data keys
                        // itself; a custom parser's result is filtered here
                        FormatKind::Builtin(format) => format::parse(
                            *format,
                            content,
                            &crate::TableLayout::Kv,
                            &label,
                            &self.options,
                        )?,
                        FormatKind::Custom(custom) => {
                            let mut value = (custom.parser)(content, &label, &self.options)?;
                            format::strip_commented_data_keys(&mut value, &self.options);
                            value
                        }
                    };
                    if !matches!(value, Value::Null) {
                        self.merge(&mut root, value, &SourceRef::String(index));
                    }
                }
            }
        }
        Ok(root)
    }

    fn parse_file(&self, claim: Claim, path: &Path) -> Result<Value> {
        match claim {
            Claim::Builtin { format, spec } => {
                // the claiming formats entry decides the table layout
                let layout = &self.options.formats[spec].layout;
                // binary formats (spreadsheets) parse from the path
                // directly — their bytes must never go through the text
                // pipeline
                if format::is_binary(format) {
                    return format::parse_binary(format, path, layout, &self.options);
                }
                let text = std::fs::read_to_string(path).map_err(Error::Io)?;
                format::parse(format, &text, layout, path, &self.options)
            }
            Claim::Custom(index) => {
                let FormatKind::Custom(custom) = &self.options.formats[index].format else {
                    unreachable!("custom claims always index custom entries");
                };
                let text = std::fs::read_to_string(path).map_err(Error::Io)?;
                // built-in formats filter commented data keys inside
                // `format::parse`/`parse_binary`; do it for custom ones here
                let mut value = (custom.parser)(&text, path, &self.options)?;
                format::strip_commented_data_keys(&mut value, &self.options);
                Ok(value)
            }
        }
    }

    fn merge(&self, target: &mut TracedValue, value: Value, source: &SourceRef) {
        merge_traced(target, value, source, !self.options.case_sensitive);
    }

    /// Load one folder source: scan it (honoring [`Options::dir_depth`]),
    /// parse each file, and merge it in. The keying options decide the
    /// shape — [`filename_as_key`](Options::filename_as_key) wraps each
    /// file's value under a key named after the file, and
    /// [`dirname_as_key`](Options::dirname_as_key) wraps under the
    /// subfolder names leading to it. With both off this is a flat merge;
    /// with both on (the `tree` feature's default) it is a folder tree.
    /// Entries load in [`Options::order`], so key collisions deep-merge
    /// with the later entry winning.
    fn load_folder(
        &self,
        folder: &Path,
        extensions: &[(String, Claim)],
        root: &mut TracedValue,
    ) -> Result<()> {
        let mut files = Vec::new();
        walk(
            folder,
            Vec::new(),
            &self.options,
            self.options.dir_depth,
            &mut files,
        )?;
        for (file, dirs) in files {
            let name = file
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or_default()
                .to_owned();
            let value = match claimed_format(&file, extensions) {
                Some(claim) => self.parse_file(claim, &file)?,
                // extension-less / unknown-extension handling only applies
                // when a file becomes a key; otherwise the file has no key
                // to contribute under and is skipped
                None if self.options.filename_as_key
                    && file_extension(&name).is_none()
                    && self.options.auto_no_ext_files =>
                {
                    // the trimmed content goes through the table auto
                    // detection (same feature-gated guesses)
                    let text = std::fs::read_to_string(&file).map_err(Error::Io)?;
                    format::table_auto(text.trim())
                }
                None if self.options.filename_as_key && !self.options.ignore_unknown_ext => {
                    return Err(Error::Parse {
                        path: file,
                        message: "no active format claims this file's extension".into(),
                    });
                }
                None => continue, // no active format claims this extension
            };
            if matches!(value, Value::Null) {
                continue; // empty file contributes nothing
            }
            let mut value = value;
            if self.options.filename_as_key {
                value = Value::Object(BTreeMap::from([(file_stem_key(&name), value)]));
            }
            if self.options.dirname_as_key {
                for key in dirs.into_iter().rev() {
                    value = Value::Object(BTreeMap::from([(key, value)]));
                }
            }
            self.merge(root, value, &SourceRef::File(file));
        }
        Ok(())
    }
}

/// The [`filename_as_key`](Options::filename_as_key) key of a file: its
/// name without the extension. A hidden file whose whole name is its
/// extension (`.env`) keeps the full name.
fn file_stem_key(name: &str) -> String {
    let hidden = name.starts_with('.');
    let body = if hidden { &name[1..] } else { name };
    match body.rfind('.') {
        Some(i) => format!("{}{}", if hidden { "." } else { "" }, &body[..i]),
        None => name.to_owned(),
    }
}

/// What an extension resolves to: a built-in format (with the index of
/// its [`Options::formats`] entry, whose layout applies), or a custom
/// entry (by index).
#[derive(Clone, Copy)]
enum Claim {
    Builtin { format: Format, spec: usize },
    Custom(usize),
}

/// The extension→parser mapping of [`Options::formats`]: lowercased
/// extension, last claimer wins — built-in and custom entries follow the
/// same rule.
fn extension_map(options: &Options) -> Vec<(String, Claim)> {
    let mut map: Vec<(String, Claim)> = Vec::new();
    for (index, spec) in options.formats.iter().enumerate() {
        let claim = match &spec.format {
            FormatKind::Builtin(format) => Claim::Builtin {
                format: *format,
                spec: index,
            },
            FormatKind::Custom(_) => Claim::Custom(index),
        };
        for ext in &spec.extensions {
            let ext = ext.to_lowercase();
            map.retain(|(e, _)| *e != ext);
            map.push((ext, claim));
        }
    }
    map
}

/// The extension of a file name, lowercased. A hidden file `.X` counts as
/// having extension `X` (that is how `.env` matches `env`).
fn file_extension(name: &str) -> Option<String> {
    let hidden = name.starts_with('.');
    let stem = if hidden { &name[1..] } else { name };
    match stem.rfind('.') {
        Some(i) => Some(stem[i + 1..].to_lowercase()),
        None if hidden && !stem.is_empty() => Some(stem.to_lowercase()),
        None => None,
    }
}

fn claimed_format(path: &Path, extensions: &[(String, Claim)]) -> Option<Claim> {
    let name = path.file_name()?.to_str()?;
    let ext = file_extension(name)?;
    extensions
        .iter()
        .find(|(e, _)| *e == ext)
        .map(|(_, claim)| *claim)
}

/// A directory's entries as `(name, path, is_dir)`.
fn read_entries(dir: &Path) -> Result<Vec<(String, PathBuf, bool)>> {
    let mut entries = Vec::new();
    for entry in std::fs::read_dir(dir).map_err(Error::Io)? {
        let entry = entry.map_err(Error::Io)?;
        let path = entry.path();
        let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
            continue;
        };
        entries.push((name.to_owned(), path.clone(), path.is_dir()));
    }
    Ok(entries)
}

fn sort_entries(entries: &mut [(String, PathBuf, bool)], order: Order) {
    match order {
        Order::FoldersFirstAlphabetic => entries.sort_by(|a, b| (!a.2, &a.0).cmp(&(!b.2, &b.0))),
        Order::Alphabetic => entries.sort_by(|a, b| a.0.cmp(&b.0)),
        Order::ReverseAlphabetic => entries.sort_by(|a, b| b.0.cmp(&a.0)),
    }
}

/// All files of a folder in load order, each with the subfolder names
/// leading to it. `depth` is the remaining [`Options::dir_depth`] budget:
/// `0` stops descent, `-1` recurses without limit, a positive value
/// decrements per level. Commented-out names (`#…`, `_…`) are skipped
/// here — by name, so keyed or not, a `_draft.yml` file and everything
/// under a `_wip/` folder stay out of the scan.
fn walk(
    dir: &Path,
    prefix: Vec<String>,
    options: &Options,
    depth: isize,
    out: &mut Vec<(PathBuf, Vec<String>)>,
) -> Result<()> {
    let mut entries = read_entries(dir)?;
    sort_entries(&mut entries, options.order);
    for (name, path, is_dir) in entries {
        let ignore_commented = if is_dir {
            options.ignore_commented_dirnames
        } else {
            options.ignore_commented_filenames
        };
        if ignore_commented && crate::options::is_commented(&name) {
            continue;
        }
        if is_dir {
            if depth != 0 {
                let mut prefix = prefix.clone();
                prefix.push(name);
                walk(
                    &path,
                    prefix,
                    options,
                    if depth < 0 { -1 } else { depth - 1 },
                    out,
                )?;
            }
        } else {
            out.push((path, prefix.clone()));
        }
    }
    Ok(())
}

/// Deep-merge `value` (labeled with `source`) into the traced tree:
/// objects merge recursively, everything else replaces.
fn merge_traced(target: &mut TracedValue, value: Value, source: &SourceRef, lowercase: bool) {
    match (target, value) {
        (TracedValue::Object(entries), Value::Object(incoming)) => {
            for (key, value) in incoming {
                let key = if lowercase { key.to_lowercase() } else { key };
                match entries.get_mut(&key) {
                    Some(slot) => merge_traced(slot, value, source, lowercase),
                    None => {
                        entries.insert(key, to_traced(value, source, lowercase));
                    }
                }
            }
        }
        (slot, value) => *slot = to_traced(value, source, lowercase),
    }
}

fn to_traced(value: Value, source: &SourceRef, lowercase: bool) -> TracedValue {
    match value {
        Value::Object(map) => TracedValue::Object(
            map.into_iter()
                .map(|(key, value)| {
                    let key = if lowercase { key.to_lowercase() } else { key };
                    (key, to_traced(value, source, lowercase))
                })
                .collect(),
        ),
        other => TracedValue::Leaf {
            value: other,
            source: source.clone(),
        },
    }
}

fn untrace(traced: TracedValue) -> Value {
    match traced {
        TracedValue::Leaf { value, .. } => value,
        TracedValue::Object(entries) => {
            Value::Object(entries.into_iter().map(|(k, v)| (k, untrace(v))).collect())
        }
    }
}