#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Format {
Json,
Jsonl,
Yaml,
Unknown,
}
impl Format {
pub fn from_filename(name: &str) -> Self {
use std::path::Path;
const EXT_FORMATS: &[(&str, Format)] = &[
("json", Format::Json),
("jsonl", Format::Jsonl),
("ndjson", Format::Jsonl),
("yaml", Format::Yaml),
("yml", Format::Yaml),
];
if let Some(ext) = Path::new(name).extension().and_then(|e| e.to_str())
{
for (pat, fmt) in EXT_FORMATS {
if ext.eq_ignore_ascii_case(pat) {
return *fmt;
}
}
}
Format::Unknown
}
}
#[cfg(test)]
mod tests {
use super::Format;
#[test]
#[allow(
clippy::cognitive_complexity,
reason = "Single test covers multiple assertions compactly."
)]
fn maps_common_extensions() {
assert_eq!(Format::from_filename("a.json"), Format::Json);
assert_eq!(Format::from_filename("b.yaml"), Format::Yaml);
assert_eq!(Format::from_filename("c.yml"), Format::Yaml);
assert_eq!(Format::from_filename("d.JSON"), Format::Json);
assert_eq!(Format::from_filename("e.YmL"), Format::Yaml);
assert_eq!(Format::from_filename("f.jsonl"), Format::Jsonl);
assert_eq!(Format::from_filename("g.ndjson"), Format::Jsonl);
assert_eq!(Format::from_filename("h.JSONL"), Format::Jsonl);
assert_eq!(Format::from_filename("noext"), Format::Unknown);
assert_eq!(Format::from_filename("weird.tar.gz"), Format::Unknown);
}
}