use std::path::Path;
pub fn get_missing_files(input_paths: &[std::path::PathBuf]) -> Vec<std::path::PathBuf> {
let mut missing_files = Vec::new();
for input_path in input_paths {
if !input_path.is_file() {
missing_files.push(input_path.clone());
}
}
missing_files
}
pub fn get_format_by_file_extension(input_path: &Path) -> Option<String> {
input_path
.extension()
.and_then(|e| e.to_str())
.and_then(|e| match e {
"json" => Some("json".to_string()),
"yaml" | "yml" => Some("yaml".to_string()),
"toml" => Some("toml".to_string()),
_ => None,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_missing_files() {
let dir = tempfile::tempdir().unwrap();
let existing = dir.path().join("exists.txt");
std::fs::write(&existing, b"data").unwrap();
let nonexistent = dir.path().join("nope.txt");
let input = [existing.clone(), nonexistent.clone()];
let missing = get_missing_files(&input);
assert_eq!(missing, vec![nonexistent]);
}
#[test]
fn test_get_missing_files_all_exist() {
let dir = tempfile::tempdir().unwrap();
let a = dir.path().join("a.txt");
let b = dir.path().join("b.txt");
std::fs::write(&a, b"x").unwrap();
std::fs::write(&b, b"x").unwrap();
let input = [a, b];
let missing = get_missing_files(&input);
assert!(missing.is_empty());
}
#[test]
fn test_get_format_by_file_extension() {
let cases = [
("foo.json", "json"),
("bar.yaml", "yaml"),
("bar.yml", "yaml"),
("baz.toml", "toml"),
];
for (path, expected) in cases {
let result = get_format_by_file_extension(Path::new(path));
assert_eq!(result.as_deref(), Some(expected), "path: {path}");
}
assert_eq!(get_format_by_file_extension(Path::new("noext")), None);
assert_eq!(get_format_by_file_extension(Path::new("bad.ext")), None);
}
}