Skip to main content

rust_config_tree/
config_format.rs

1//! Format inference and renderer option presets.
2//!
3//! Runtime loading and template rendering both use [`ConfigFormat`] so unknown
4//! or extensionless files consistently fall back to YAML. The private option
5//! helpers keep generated templates comment-rich and visually aligned across
6//! YAML, TOML, and JSON5.
7
8use std::{ffi::OsStr, path::Path};
9
10/// File format used when loading config files or rendering templates.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ConfigFormat {
13    /// YAML format, selected for `.yaml`, `.yml`, unknown extensions, and paths
14    /// without an extension.
15    Yaml,
16    /// TOML format, selected for `.toml`.
17    Toml,
18    /// JSON5-compatible format, selected for `.json` and `.json5`.
19    Json,
20}
21
22/// Path-based format inference for config files and generated templates.
23impl ConfigFormat {
24    /// Infers the config format from a path extension.
25    ///
26    /// Unknown extensions intentionally fall back to YAML.
27    ///
28    /// # Arguments
29    ///
30    /// - `path`: Config or template path whose extension should be inspected.
31    ///
32    /// # Returns
33    ///
34    /// Returns the inferred [`ConfigFormat`].
35    ///
36    /// # Examples
37    ///
38    /// ```
39    /// use rust_config_tree::ConfigFormat;
40    ///
41    /// assert_eq!(ConfigFormat::from_path("config.toml"), ConfigFormat::Toml);
42    /// assert_eq!(ConfigFormat::from_path("config.json5"), ConfigFormat::Json);
43    /// assert_eq!(ConfigFormat::from_path("config.unknown"), ConfigFormat::Yaml);
44    /// ```
45    pub fn from_path(path: impl AsRef<Path>) -> Self {
46        match path.as_ref().extension().and_then(OsStr::to_str) {
47            Some("toml") => Self::Toml,
48            Some("json" | "json5") => Self::Json,
49            Some("yaml" | "yml") | Some(_) | None => Self::Yaml,
50        }
51    }
52}
53
54/// Builds the YAML renderer options used by default templates.
55///
56/// # Arguments
57///
58/// This function has no arguments.
59///
60/// # Returns
61///
62/// Returns YAML format options shared by generated templates.
63///
64/// # Examples
65///
66/// ```no_run
67/// let _ = ();
68/// ```
69pub(crate) fn yaml_options() -> confique::yaml::FormatOptions {
70    let mut options = confique::yaml::FormatOptions::default();
71    options.indent = 2;
72    options.general.comments = true;
73    options.general.env_keys = true;
74    options.general.nested_field_gap = 1;
75    options
76}
77
78/// Builds the TOML renderer options used by default templates.
79///
80/// # Arguments
81///
82/// This function has no arguments.
83///
84/// # Returns
85///
86/// Returns TOML format options shared by generated templates.
87///
88/// # Examples
89///
90/// ```no_run
91/// let _ = ();
92/// ```
93pub(crate) fn toml_options() -> confique::toml::FormatOptions {
94    let mut options = confique::toml::FormatOptions::default();
95    options.general.comments = true;
96    options.general.env_keys = true;
97    options.general.nested_field_gap = 1;
98    options
99}
100
101/// Builds the JSON5 renderer options used by default templates.
102///
103/// # Arguments
104///
105/// This function has no arguments.
106///
107/// # Returns
108///
109/// Returns JSON5 format options shared by generated templates.
110///
111/// # Examples
112///
113/// ```no_run
114/// let _ = ();
115/// ```
116pub(crate) fn json5_options() -> confique::json5::FormatOptions {
117    let mut options = confique::json5::FormatOptions::default();
118    options.indent = 2;
119    options.general.comments = true;
120    options.general.env_keys = true;
121    options.general.nested_field_gap = 1;
122    options
123}