use std::{fs, path::PathBuf};
use ec4rs::PropertyKey;
#[allow(dead_code)] pub(crate) mod charset {
use std::path::Path;
use ec4rs::property::Charset;
pub fn get_charset_from_file_name(file_path: &Path) -> Result<Charset, String> {
match file_path.file_stem().unwrap().to_str().unwrap() {
"utf-8" => Ok(Charset::Utf8),
"latin1" => Ok(Charset::Latin1),
"utf-16le" => Ok(Charset::Utf16Le),
"utf-16be" => Ok(Charset::Utf16Be),
"utf-8-bom" => Ok(Charset::Utf8Bom),
other => Err(String::from(other)),
}
}
}
#[allow(dead_code)] pub(crate) mod end_of_line {
use std::path::Path;
use ec4rs::property::EndOfLine;
pub fn get_end_of_line_from_file_name(file_path: &Path) -> Vec<EndOfLine> {
file_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.split("-")
.map(|name| match name {
"LF" => EndOfLine::Lf,
"CRLF" => EndOfLine::CrLf,
"CR" => EndOfLine::Cr,
other => panic!(
"Unknown end of line character short hand '{other}' found in file name of {}",
file_path.display()
),
})
.collect()
}
}
pub(crate) struct TestFileHelper {
dir_name: &'static str,
}
impl TestFileHelper {
pub(crate) fn new<T: PropertyKey>() -> TestFileHelper {
TestFileHelper { dir_name: T::key() }
}
pub(crate) fn get_test_file_paths(&self) -> impl Iterator<Item = PathBuf> {
let test_dir = fs::read_dir(self.get_test_dir()).expect("tests resources have to exist");
test_dir
.into_iter()
.map(|entry| entry.expect("test files have to be accessible").path())
.filter(|path| {
path.extension()
.is_none_or(|ext| ext != "editorconfig" && ext != "license")
})
.inspect(|path| println!("Test file {}", path.display()))
}
pub(crate) fn get_test_dir(&self) -> PathBuf {
["tests", "resources", self.dir_name].into_iter().collect()
}
}