1use std::path::Path;
2
3pub fn get_missing_files(input_paths: &[std::path::PathBuf]) -> Vec<std::path::PathBuf> {
6 let mut missing_files = Vec::new();
7 for input_path in input_paths {
8 if !input_path.is_file() {
9 missing_files.push(input_path.clone());
10 }
11 }
12 missing_files
13}
14
15pub fn get_format_by_file_extension(input_path: &Path) -> Option<String> {
16 input_path
17 .extension()
18 .and_then(|e| e.to_str())
19 .and_then(|e| match e {
20 "json" => Some("json".to_string()),
21 "yaml" | "yml" => Some("yaml".to_string()),
22 "toml" => Some("toml".to_string()),
23 _ => None,
24 })
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn test_get_missing_files() {
33 let dir = tempfile::tempdir().unwrap();
34 let existing = dir.path().join("exists.txt");
35 std::fs::write(&existing, b"data").unwrap();
36
37 let nonexistent = dir.path().join("nope.txt");
38
39 let input = [existing.clone(), nonexistent.clone()];
40 let missing = get_missing_files(&input);
41 assert_eq!(missing, vec![nonexistent]);
42 }
43
44 #[test]
45 fn test_get_missing_files_all_exist() {
46 let dir = tempfile::tempdir().unwrap();
47 let a = dir.path().join("a.txt");
48 let b = dir.path().join("b.txt");
49 std::fs::write(&a, b"x").unwrap();
50 std::fs::write(&b, b"x").unwrap();
51
52 let input = [a, b];
53 let missing = get_missing_files(&input);
54 assert!(missing.is_empty());
55 }
56
57 #[test]
58 fn test_get_format_by_file_extension() {
59 let cases = [
60 ("foo.json", "json"),
61 ("bar.yaml", "yaml"),
62 ("bar.yml", "yaml"),
63 ("baz.toml", "toml"),
64 ];
65 for (path, expected) in cases {
66 let result = get_format_by_file_extension(Path::new(path));
67 assert_eq!(result.as_deref(), Some(expected), "path: {path}");
68 }
69
70 assert_eq!(get_format_by_file_extension(Path::new("noext")), None);
71 assert_eq!(get_format_by_file_extension(Path::new("bad.ext")), None);
72 }
73}