shep_core/config/flockfile/
format.rs1use std::path::Path;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum FlockFormat {
8 Toml,
10 Yaml,
12 Json,
14 Json5,
16}
17
18impl FlockFormat {
19 #[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}