Skip to main content

c4/
options.rs

1//! The plain-data configuration surface: [`Options`] and every type its
2//! fields are made of — formats and their specs (built-in and custom),
3//! folder order, and the table options.
4
5use std::path::Path;
6
7use crate::{Result, Source, Value};
8
9/// A config file format.
10///
11/// The variants always exist; whether a format can actually be parsed
12/// depends on the matching Cargo feature. Which extensions a format reads
13/// is part of [`FormatSpec`].
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum Format {
16    Json,
17    Jsonc,
18    Yaml,
19    Toml,
20    Ini,
21    Env,
22    Csv,
23    /// Excel workbooks (`xlsx`/`xlsm`/`xlsb`/`xls`) — a binary
24    /// table-shaped format; file sources only. Feature `excel`.
25    Excel,
26    /// OpenDocument spreadsheets (`ods`) — a binary table-shaped format;
27    /// file sources only. Feature `ods`.
28    Ods,
29}
30
31impl Format {
32    /// Look a format up by its string id (`"json"`, `"jsonc"`, `"yaml"`,
33    /// `"toml"`, `"ini"`, `"env"`, `"csv"`, `"excel"`, `"ods"`).
34    pub fn from_id(id: &str) -> Option<Format> {
35        Some(match id {
36            "json" => Format::Json,
37            "jsonc" => Format::Jsonc,
38            "yaml" => Format::Yaml,
39            "toml" => Format::Toml,
40            "ini" => Format::Ini,
41            "env" => Format::Env,
42            "csv" => Format::Csv,
43            "excel" => Format::Excel,
44            "ods" => Format::Ods,
45            _ => return None,
46        })
47    }
48
49    /// The string id of this format.
50    pub fn id(self) -> &'static str {
51        match self {
52            Format::Json => "json",
53            Format::Jsonc => "jsonc",
54            Format::Yaml => "yaml",
55            Format::Toml => "toml",
56            Format::Ini => "ini",
57            Format::Env => "env",
58            Format::Csv => "csv",
59            Format::Excel => "excel",
60            Format::Ods => "ods",
61        }
62    }
63
64    /// The extensions (without the dot) this format claims by default.
65    ///
66    /// Extension matching is always case-insensitive, and a hidden file
67    /// named `.X` is treated as having extension `X` (so `.env` matches the
68    /// `env` extension).
69    pub fn default_extensions(self) -> &'static [&'static str] {
70        match self {
71            Format::Json => &["json"],
72            Format::Jsonc => &["json", "jsonc"],
73            Format::Yaml => &["yml", "yaml"],
74            Format::Toml => &["toml"],
75            Format::Ini => &["ini"],
76            Format::Env => &["env"],
77            Format::Csv => &["csv"],
78            Format::Excel => &["xlsx", "xlsm", "xlsb", "xls"],
79            Format::Ods => &["ods"],
80        }
81    }
82
83    /// The default [`TableLayout`] of this format: `db` for the
84    /// spreadsheet formats (excel/ods — a sheet is a record grid unless
85    /// told otherwise), `kv` for csv (and, vacuously, for the non-table
86    /// formats, which never consult a layout).
87    pub fn default_layout(self) -> TableLayout {
88        match self {
89            Format::Excel | Format::Ods => TableLayout::Db,
90            _ => TableLayout::Kv,
91        }
92    }
93
94    /// Pair this format with custom extensions.
95    pub fn exts<I, S>(self, extensions: I) -> FormatSpec
96    where
97        I: IntoIterator<Item = S>,
98        S: Into<String>,
99    {
100        FormatSpec {
101            format: FormatKind::Builtin(self),
102            extensions: extensions.into_iter().map(Into::into).collect(),
103            layout: self.default_layout(),
104        }
105    }
106}
107
108/// What a [`FormatSpec`] entry parses with: a built-in format or a
109/// user-defined [`CustomFormat`].
110#[derive(Debug, Clone, PartialEq)]
111pub enum FormatKind {
112    Builtin(Format),
113    Custom(CustomFormat),
114}
115
116/// A format plus the extensions it reads — one entry of [`Options::formats`].
117///
118/// Accepted forms (all `.into()`):
119/// - `Format::Yaml` or `"yaml"` — default extensions
120/// - `(Format::Yaml, ["yml", "conf"])` or `("yaml", ["yml", "conf"])` —
121///   custom extensions
122/// - `(Format::Csv, ["csv"], "db")` — custom extensions **plus a
123///   [`TableLayout`]** (a layout id string, a `TableLayout`, or a
124///   [`CustomLayout`]): every file this entry claims parses under that
125///   layout, in merge mode, tree mode and single-file path sources
126///   alike. Table sources override it; string sources stay `Kv`; a
127///   layout on a non-table format panics at conversion. Without this
128///   form an entry uses the format's default layout
129///   ([`Format::default_layout`]).
130/// - a [`CustomFormat`] — user-defined parser with the extensions it was
131///   built with
132#[derive(Debug, Clone, PartialEq)]
133pub struct FormatSpec {
134    pub(crate) format: FormatKind,
135    /// Extensions without the leading dot.
136    pub(crate) extensions: Vec<String>,
137    /// The table layout for files this entry claims (table formats only).
138    pub(crate) layout: TableLayout,
139}
140
141impl From<Format> for FormatSpec {
142    fn from(format: Format) -> Self {
143        format.exts(format.default_extensions().iter().copied())
144    }
145}
146
147impl From<Format> for FormatKind {
148    fn from(format: Format) -> Self {
149        FormatKind::Builtin(format)
150    }
151}
152
153impl From<CustomFormat> for FormatKind {
154    fn from(custom: CustomFormat) -> Self {
155        FormatKind::Custom(custom)
156    }
157}
158
159impl From<&str> for FormatKind {
160    /// A built-in format id (`"jsonc"`, `"yaml"`, …). Panics on an unknown
161    /// id — this is config-time code. Lets a `(id, text)` string source
162    /// name its format by string.
163    fn from(id: &str) -> Self {
164        FormatKind::Builtin(
165            Format::from_id(id).unwrap_or_else(|| panic!("unknown format id: {id:?}")),
166        )
167    }
168}
169
170impl From<CustomFormat> for FormatSpec {
171    fn from(custom: CustomFormat) -> Self {
172        Self {
173            extensions: custom.extensions.clone(),
174            format: FormatKind::Custom(custom),
175            layout: TableLayout::Kv,
176        }
177    }
178}
179
180impl From<&str> for FormatSpec {
181    /// Panics on an unknown format id — this is config-time code.
182    fn from(id: &str) -> Self {
183        Format::from_id(id)
184            .unwrap_or_else(|| panic!("unknown format id: {id:?}"))
185            .into()
186    }
187}
188
189impl<const N: usize> From<(Format, [&str; N])> for FormatSpec {
190    fn from((format, extensions): (Format, [&str; N])) -> Self {
191        format.exts(extensions)
192    }
193}
194
195impl<const N: usize> From<(&str, [&str; N])> for FormatSpec {
196    /// Panics on an unknown format id — this is config-time code.
197    fn from((id, extensions): (&str, [&str; N])) -> Self {
198        Format::from_id(id)
199            .unwrap_or_else(|| panic!("unknown format id: {id:?}"))
200            .exts(extensions)
201    }
202}
203
204/// In a `formats` entry the third element is always a **layout** (a
205/// string must be a valid layout id — extensions have no sheet meaning).
206/// Panics on unknown ids, and on a layout for a non-table format
207/// (`(Format::Yaml, ["yml"], "db")`) — this is config-time code.
208impl<const N: usize, L: Into<TableLayout>> From<(Format, [&str; N], L)> for FormatSpec {
209    fn from((format, extensions, layout): (Format, [&str; N], L)) -> Self {
210        assert!(
211            matches!(format, Format::Csv | Format::Excel | Format::Ods),
212            "'{}' is not a table format (csv, excel, ods) — table layouts do not apply",
213            format.id()
214        );
215        FormatSpec {
216            layout: layout.into(),
217            ..format.exts(extensions)
218        }
219    }
220}
221
222/// Panics on an unknown format or layout id — this is config-time code.
223impl<const N: usize, L: Into<TableLayout>> From<(&str, [&str; N], L)> for FormatSpec {
224    fn from((id, extensions, layout): (&str, [&str; N], L)) -> Self {
225        let format = Format::from_id(id).unwrap_or_else(|| panic!("unknown format id: {id:?}"));
226        (format, extensions, layout).into()
227    }
228}
229
230/// File load order inside a folder.
231#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
232pub enum Order {
233    /// Subfolders first, then files, each group alphabetic — how directory
234    /// listings are conventionally sorted.
235    #[default]
236    FoldersFirstAlphabetic,
237    /// Pure lexicographic order, folders and files interleaved.
238    Alphabetic,
239    ReverseAlphabetic,
240}
241
242impl Order {
243    /// Look an order up by its string id. Accepts `-` or `_` between words
244    /// and a couple of short aliases:
245    /// `folders_first_alphabetic` (`folders_first`, `default`),
246    /// `alphabetic`, `reverse_alphabetic` (`reverse`).
247    pub fn from_id(id: &str) -> Option<Order> {
248        Some(match id.to_lowercase().replace('-', "_").as_str() {
249            "folders_first_alphabetic" | "folders_first" | "default" => {
250                Order::FoldersFirstAlphabetic
251            }
252            "alphabetic" => Order::Alphabetic,
253            "reverse_alphabetic" | "reverse" => Order::ReverseAlphabetic,
254            _ => return None,
255        })
256    }
257
258    /// The canonical string id of this order.
259    pub fn id(self) -> &'static str {
260        match self {
261            Order::FoldersFirstAlphabetic => "folders_first_alphabetic",
262            Order::Alphabetic => "alphabetic",
263            Order::ReverseAlphabetic => "reverse_alphabetic",
264        }
265    }
266}
267
268impl From<&str> for Order {
269    /// Panics on an unknown id — this is config-time code. See
270    /// [`Order::from_id`] for the accepted spellings.
271    fn from(id: &str) -> Self {
272        Order::from_id(id).unwrap_or_else(|| panic!("unknown order id: {id:?}"))
273    }
274}
275
276/// How the table stage interprets a row grid. Chosen per table source
277/// (`(format, path, layout)` / `(format, path, sheet, layout)` tuples),
278/// per `formats` entry (`(format, [exts], layout)`), or passed
279/// explicitly to [`parse_table`](crate::parse_table); everything else
280/// uses the format's default —
281/// [`Format::default_layout`](crate::Format::default_layout): `kv` for
282/// csv, `db` for excel/ods. Converts from a layout id string
283/// (`"kv"`/`"kvf"`, `"db"`) or a [`CustomLayout`].
284#[derive(Debug, Clone, PartialEq, Default)]
285pub enum TableLayout {
286    /// `key,value[,format]` positional rows — one config entry per row.
287    /// The default for csv.
288    #[default]
289    Kv,
290    /// A database-style grid: the first (non-blank) row holds the keys,
291    /// the second **always** the type ids (an empty cell = `auto`), every
292    /// following row is one record. Parses to an **array** of one object
293    /// per record: `[[a,b],[i8,str],[4,x],[5,y]]` →
294    /// `[{a:4,b:"x"},{a:5,b:"y"}]`. Empty cells are omitted from their
295    /// record (sparse tables give sparse objects). The default for the
296    /// spreadsheet formats (excel/ods). A grid *without* a
297    /// type row is a [`CustomLayout`] that inserts a row of `auto` cells
298    /// after the header and delegates back to `Db` (see the
299    /// `xlsx-sheets` example).
300    Db,
301    /// A user callback over the raw rows — see [`CustomLayout`].
302    Custom(CustomLayout),
303}
304
305impl TableLayout {
306    /// Look a layout up by its string id: `kv` (alias `kvf`) or `db`.
307    pub fn from_id(id: &str) -> Option<TableLayout> {
308        Some(match id.to_lowercase().as_str() {
309            "kv" | "kvf" => TableLayout::Kv,
310            "db" => TableLayout::Db,
311            _ => return None,
312        })
313    }
314
315    /// The string id of this layout (a custom layout reports its own id).
316    pub fn id(&self) -> &str {
317        match self {
318            TableLayout::Kv => "kv",
319            TableLayout::Db => "db",
320            TableLayout::Custom(custom) => &custom.id,
321        }
322    }
323}
324
325impl From<&str> for TableLayout {
326    /// Panics on an unknown id — this is config-time code. See
327    /// [`TableLayout::from_id`] for the accepted spellings.
328    fn from(id: &str) -> Self {
329        TableLayout::from_id(id).unwrap_or_else(|| {
330            panic!("unknown table layout id: {id:?} (valid: kv, db, or a CustomLayout)")
331        })
332    }
333}
334
335impl From<CustomLayout> for TableLayout {
336    fn from(custom: CustomLayout) -> Self {
337        TableLayout::Custom(custom)
338    }
339}
340
341/// The parser callback of a [`CustomLayout`]: `(rows, path, options)`.
342pub(crate) type CustomRowsParser =
343    std::sync::Arc<dyn Fn(Vec<Vec<String>>, &Path, &Options) -> Result<Value> + Send + Sync>;
344
345/// A user-defined table layout: an id and a callback that receives the
346/// lowered rows (`Vec<Vec<String>>` — csv records, or a sheet's cells as
347/// text) and returns any [`Value`]. This is the rows-level escape hatch:
348/// binary formats (spreadsheets) can't go through a [`CustomFormat`]
349/// (which parses text), but one sheet of a workbook can still get fully
350/// custom treatment via
351/// `(Format::Excel, path, sheet, CustomLayout::new(…)).into()`. Most
352/// custom layouts end by reshaping the rows and calling
353/// [`parse_table`](crate::parse_table) (the `xlsx-sheets` example
354/// transposes a column-oriented sheet this way).
355#[derive(Clone)]
356pub struct CustomLayout {
357    pub(crate) id: String,
358    /// `(rows, path, options)` — `path` labels errors. Crate-private:
359    /// build custom layouts through [`CustomLayout::new`].
360    pub(crate) parser: CustomRowsParser,
361}
362
363impl CustomLayout {
364    pub fn new<F>(id: impl Into<String>, parser: F) -> Self
365    where
366        F: Fn(Vec<Vec<String>>, &Path, &Options) -> Result<Value> + Send + Sync + 'static,
367    {
368        Self {
369            id: id.into(),
370            parser: std::sync::Arc::new(parser),
371        }
372    }
373}
374
375impl std::fmt::Debug for CustomLayout {
376    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
377        f.debug_struct("CustomLayout")
378            .field("id", &self.id)
379            .finish_non_exhaustive()
380    }
381}
382
383impl PartialEq for CustomLayout {
384    fn eq(&self, other: &Self) -> bool {
385        self.id == other.id && std::sync::Arc::ptr_eq(&self.parser, &other.parser)
386    }
387}
388
389/// The parser callback of a [`CustomFormat`]: `(text, path, options)`.
390pub(crate) type CustomParser =
391    std::sync::Arc<dyn Fn(&str, &Path, &Options) -> Result<Value> + Send + Sync>;
392
393/// A user-defined format: an id, the extensions it claims, and a parser
394/// callback. It goes into [`Options::formats`] like any built-in format
395/// (`formats: [..the defaults, md.into()]`), so the usual rule applies —
396/// the last claimer of an extension owns it, and a custom format can take
397/// over a built-in extension. Table-shaped custom formats can lower their
398/// file into rows and reuse the generic table stage via
399/// [`parse_table`](crate::parse_table).
400///
401/// A markdown pipe-table as a config format (runs as a doc test — a
402/// string source can name a custom format directly, no files needed):
403///
404/// ```
405/// use c4::{CustomFormat, Loader};
406///
407/// # fn main() -> Result<(), c4::Error> {
408/// let md = CustomFormat::new("md-table", ["md"], |text, path, options| {
409///     let rows: Vec<Vec<String>> = text
410///         .lines()
411///         .map(str::trim)
412///         .filter(|l| l.starts_with('|') && !l.contains("---"))
413///         // markdown tables always start with a header row — dropping
414///         // it here keeps the data rows positional (key,value[,format]),
415///         // exactly what parse_table expects
416///         .skip(1)
417///         .map(|l| {
418///             l.trim_matches('|')
419///                 .split('|')
420///                 .map(|cell| cell.trim().to_owned())
421///                 .collect()
422///         })
423///         .collect();
424///     c4::parse_table(rows, &c4::TableLayout::Kv, path, options)
425/// });
426///
427/// // to read `.md` files instead, push `md` into Options.formats —
428/// // custom formats claim extensions exactly like built-ins do
429/// let value: c4::Value = Loader::new(c4::Options {
430///         sources: vec![(
431///             md,
432///             "| key  | value | format |
433///              | ---- | ----- | ------ |
434///              | name | c4    | str    |
435///              | port | 8080  | u16    |",
436///         )
437///             .into()],
438///         ..c4::Options::default()
439///     })
440///     .load()?;
441/// assert_eq!(value["name"].as_str(), Some("c4"));
442/// assert_eq!(value["port"].as_u64(), Some(8080));
443/// # Ok(())
444/// # }
445/// ```
446#[derive(Clone)]
447pub struct CustomFormat {
448    pub(crate) id: String,
449    /// Extensions without the leading dot.
450    pub(crate) extensions: Vec<String>,
451    /// `(text, path, options)` — `path` labels errors. Crate-private:
452    /// build custom formats through [`CustomFormat::new`].
453    pub(crate) parser: CustomParser,
454}
455
456impl CustomFormat {
457    pub fn new<I, S, F>(id: impl Into<String>, extensions: I, parser: F) -> Self
458    where
459        I: IntoIterator<Item = S>,
460        S: Into<String>,
461        F: Fn(&str, &Path, &Options) -> Result<Value> + Send + Sync + 'static,
462    {
463        Self {
464            id: id.into(),
465            extensions: extensions.into_iter().map(Into::into).collect(),
466            parser: std::sync::Arc::new(parser),
467        }
468    }
469}
470
471impl std::fmt::Debug for CustomFormat {
472    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
473        f.debug_struct("CustomFormat")
474            .field("id", &self.id)
475            .field("extensions", &self.extensions)
476            .finish_non_exhaustive()
477    }
478}
479
480impl PartialEq for CustomFormat {
481    fn eq(&self, other: &Self) -> bool {
482        self.id == other.id
483            && self.extensions == other.extensions
484            && std::sync::Arc::ptr_eq(&self.parser, &other.parser)
485    }
486}
487
488/// All loader options.
489///
490/// Plain data on purpose: set the fields you care about and take the rest
491/// from `..Options::default()`.
492#[derive(Debug, Clone, PartialEq)]
493pub struct Options {
494    /// The ordered config sources; later sources override earlier ones.
495    /// Default: the `config` folder under the current working directory.
496    pub sources: Vec<Source>,
497    /// Which formats to read and which extensions each one claims. The
498    /// list carries **no override-order semantics** — it only builds the
499    /// extension→format mapping (if several entries claim the same
500    /// extension, the last entry wins the claim). Override order between
501    /// files is decided by filename alone; see the merge rules in
502    /// README.md.
503    pub formats: Vec<FormatSpec>,
504    /// **Merge mode only** (`tree: false`); folder sources. Scan
505    /// subdirectories too. Default `false`.
506    pub recursive: bool,
507    /// **Merge mode only**, and only meaningful with
508    /// `recursive: true`. `true` (default): subfolder files merge as if
509    /// they sat at the top level — folder names carry no meaning.
510    /// `false`: each subfolder becomes a key and its files merge under
511    /// it. (The *filename* never becomes a key in merge mode; for
512    /// folder-and-filename keys use tree mode.)
513    pub flat: bool,
514    /// **All modes; env and table formats.** Treat dotted keys as nested
515    /// paths: `a.b.c` becomes `{ "a": { "b": { "c": … } } }`.
516    /// Default `true`.
517    pub dot_key: bool,
518    /// **All modes.** Case-sensitive key merging. Default `true`; when
519    /// `false`, keys are normalized to lowercase. (File-extension
520    /// matching is always case-insensitive and unrelated to this
521    /// option.)
522    pub case_sensitive: bool,
523    /// **All modes**; folder sources. Load order inside a folder. In
524    /// merge mode it decides which file overrides which; in tree mode it
525    /// decides key collisions (`a.yml` vs a folder `a/`).
526    pub order: Order,
527    /// **Mode switch** (requires the `tree` feature). `false` (default):
528    /// merge mode — a folder's files deep-merge into one value. `true`:
529    /// tree mode — the folder's *shape* becomes the value: every
530    /// subfolder is a key, and every file is a key named after the file
531    /// (extension stripped) holding that file's parsed content —
532    /// `a/b.json = {c:1}` and `d.json = {a:123}` load as
533    /// `{a: {b: {c: 1}}, d: {a: 123}}`. Tree mode is always recursive;
534    /// `recursive` and `flat` do not apply to it.
535    pub tree: bool,
536    /// **Tree mode only.** What to do with a file that has *no*
537    /// extension. `true` (default): read it and guess the type of its
538    /// (trimmed) content with the table `auto` rules — a file containing
539    /// `1.1.1.1` becomes an IPv4 value when the `ipv4` feature is on, a
540    /// string otherwise. `false`: treat it like a file with an unknown
541    /// extension (see [`ignore_unknown_ext`](Options::ignore_unknown_ext)).
542    /// (Named `auto_files` after the table `auto` type id it reuses.)
543    pub auto_files: bool,
544    /// **Tree mode only.** What to do with a file whose extension no
545    /// active format claims: skip it (`true`, default) or fail the load
546    /// with `Error::Parse` (`false`). Merge mode always skips such
547    /// files.
548    pub ignore_unknown_ext: bool,
549    /// **Spreadsheet formats (excel/ods) only.** Skip sheets whose name
550    /// starts with `#`, `.` or `_` — draft/scratch space next to live
551    /// config. Default `true`. (A table source that names a sheet
552    /// explicitly bypasses this filter.)
553    pub ignore_sheet_prefix: bool,
554    /// **Spreadsheet formats (excel/ods) only.** Skip sheets marked
555    /// hidden in the workbook (Excel `hidden`/`veryHidden`, OpenDocument
556    /// `table:display="false"`). Default `true`. (A table source that
557    /// names a sheet explicitly bypasses this filter.)
558    pub ignore_hidden_sheets: bool,
559}
560
561impl Default for Options {
562    fn default() -> Self {
563        Self {
564            sources: vec![Source::Path("config".into())],
565            formats: enabled_formats(),
566            recursive: false,
567            flat: true,
568            dot_key: true,
569            case_sensitive: true,
570            order: Order::default(),
571            tree: false,
572            auto_files: true,
573            ignore_unknown_ext: true,
574            ignore_sheet_prefix: true,
575            ignore_hidden_sheets: true,
576        }
577    }
578}
579
580/// Every format compiled into this build with its default extensions.
581/// Later entries win contested extension claims, so `jsonc` (a JSON
582/// superset) takes `.json` over strict `json` when both are enabled.
583#[allow(clippy::vec_init_then_push)] // every push is cfg-gated on its feature
584fn enabled_formats() -> Vec<FormatSpec> {
585    let mut formats = Vec::new();
586    #[cfg(feature = "json")]
587    formats.push(Format::Json.into());
588    #[cfg(feature = "jsonc")]
589    formats.push(Format::Jsonc.into());
590    #[cfg(feature = "yaml")]
591    formats.push(Format::Yaml.into());
592    #[cfg(feature = "toml")]
593    formats.push(Format::Toml.into());
594    #[cfg(feature = "ini")]
595    formats.push(Format::Ini.into());
596    #[cfg(feature = "env")]
597    formats.push(Format::Env.into());
598    #[cfg(feature = "csv")]
599    formats.push(Format::Csv.into());
600    #[cfg(feature = "excel")]
601    formats.push(Format::Excel.into());
602    #[cfg(feature = "ods")]
603    formats.push(Format::Ods.into());
604    formats
605}