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
//! Load a folder of config files into one deep-merged [`Value`].
//!
//! `README.md` is the user-facing overview and `CLAUDE.md` the complete
//! spec; the README examples are mirrored here as doc tests. Examples
//! that read the caller's working directory (`./config`, `/etc/myapp`)
//! are `no_run` — they compile but depend on the user's filesystem; the
//! [`CustomFormat`] example is fully runnable via a string source.
//!
//! Simplest form — one generic [`load`] taking the path of a folder (or
//! a single file), default options (formats: jsonc + yaml). The
//! annotated target type decides whether you get a dynamic [`Value`] or
//! your own struct:
//!
//! ```no_run
//! # fn main() -> Result<(), c4::Error> {
//! let value: c4::Value = c4::load("config")?;
//! # Ok(())
//! # }
//! ```
//!
//! Advanced — explicit sources (later overrides earlier) and options:
//!
//! ```no_run
//! use std::path::Path;
//!
//! use c4::{Format, Loader, Options, Source};
//!
//! # fn main() -> Result<(), c4::Error> {
//! let value: c4::Value = Loader::new(Options {
//! sources: vec![
//! Source::folder("./config"),
//! Source::folder(Path::new("/etc/myapp")), // any path-like type
//! Source::file("./local.yml"),
//! Source::string(Format::Jsonc, r#"{ "debug": true }"#),
//! ],
//! recursive: true,
//! formats: vec![
//! "jsonc".into(), // format id, default extensions
//! Format::Toml.into(), // enum form, default extensions
//! (Format::Yaml, ["yml", "yaml", "conf"]).into(), // custom extensions
//! ("jsonc", ["json", "jsonc"]).into(), // string id + custom extensions
//! ],
//! ..Options::default()
//! })
//! .load()?;
//! # Ok(())
//! # }
//! ```
compile_error!;
pub
pub use ;
pub use Loader;
pub use ;
pub use Source;
pub use ;
pub use Value;
use ;
use DeserializeOwned;
/// Load one path with default options — the shortest way in. An
/// existing file loads as a single file source; anything else is
/// treated as a folder (a missing path is [`Error::NotFound`]). The
/// annotated target type decides what you get back:
///
/// ```no_run
/// #[derive(serde::Deserialize)]
/// struct MyConfig {
/// host: String,
/// port: u16,
/// }
///
/// # fn main() -> Result<(), c4::Error> {
/// let value: c4::Value = c4::load("config")?; // a folder …
/// let value: c4::Value = c4::load("app.yml")?; // … or one file
/// let cfg: MyConfig = c4::load("config")?;
/// # Ok(())
/// # }
/// ```
/// The generic table stage: interpret a plain row table
/// (`[[key, value, format], …]`) with the `options.table` semantics — the
/// same stage the csv format feeds. Public so custom table-shaped formats
/// (spreadsheets, markdown tables, …) can lower their file into rows and
/// reuse it; `path` labels errors.