use std::path::Path;
use crate::prelude::*;
pub fn is_directory_path(path: &Path) -> bool {
path.is_dir()
}
pub fn detect_format_from_extension(filename: &str, is_reader: bool) -> Option<String> {
let ext = Path::new(filename).extension().map(|e| e.to_string_lossy().to_lowercase())?;
if is_reader {
get_reader_format_by_extension(&ext).map(|s| s.to_string())
} else {
get_writer_format_by_extension(&ext).map(|s| s.to_string())
}
}
pub fn detect_dir_format_from_files(dir_path: &Path, is_reader: bool) -> Option<String> {
if !dir_path.is_dir() {
return None;
}
let entries = std::fs::read_dir(dir_path).ok()?;
let mut detected_formats: Vec<String> = Vec::new();
for entry in entries {
let entry = entry.ok()?;
let path = entry.path();
if path.is_dir() {
continue;
}
let ext = path.extension().map(|e| e.to_string_lossy().to_lowercase())?;
let fmt = if is_reader {
get_reader_format_by_extension(&ext).map(|s| s.to_string())
} else {
get_writer_format_by_extension(&ext).map(|s| s.to_string())
};
if let Some(f) = fmt {
detected_formats.push(f);
}
}
if detected_formats.is_empty() {
return None;
}
let first_format = &detected_formats[0];
if detected_formats.iter().all(|f| f == first_format) {
Some(format!("dir-{}", first_format))
} else {
None
}
}