Skip to main content

shep_core/config/flockfile/
format.rs

1//! [`FlockFormat`]: which of the four document formats a path names.
2
3use std::path::Path;
4
5/// Input format of a Flockfile
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum FlockFormat {
8    /// `Flockfile.toml`: `[[app]]` tables
9    Toml,
10    /// `.yaml`/`.yml`
11    Yaml,
12    /// Strict JSON
13    Json,
14    /// JSON5 (comments, trailing commas)
15    Json5,
16}
17
18impl FlockFormat {
19    /// Maps a file extension to its format (`None` = unsupported, e.g. `.js`)
20    #[must_use]
21    pub fn from_path(path: &Path) -> Option<Self> {
22        match path.extension()?.to_str()? {
23            "toml" => Some(Self::Toml),
24            "yaml" | "yml" => Some(Self::Yaml),
25            "json" => Some(Self::Json),
26            "json5" => Some(Self::Json5),
27            _ => None,
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn format_from_path() {
38        use std::path::Path;
39        assert_eq!(
40            FlockFormat::from_path(Path::new("Flockfile.toml")),
41            Some(FlockFormat::Toml)
42        );
43        assert_eq!(
44            FlockFormat::from_path(Path::new("f.yml")),
45            Some(FlockFormat::Yaml)
46        );
47        assert_eq!(
48            FlockFormat::from_path(Path::new("f.json5")),
49            Some(FlockFormat::Json5)
50        );
51        assert_eq!(FlockFormat::from_path(Path::new("f.js")), None);
52    }
53}