use std::{
fs,
path::{self, 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()
}
}
#[allow(dead_code)] pub(crate) mod indentation {
use std::path::Path;
use ec4rs::{PropertyValue, property::IndentStyle, rawvalue::RawValue};
pub fn get_infos_from_file_name(file_path: &Path) -> (IndentStyle, usize, usize, Vec<usize>) {
let mut name_parts = file_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.split("-")
.skip(1); let indent_style = IndentStyle::parse(&RawValue::from(
name_parts
.next()
.expect("There have to be a second name part")
.to_string(),
))
.unwrap_or_else(|_| {
panic!(
"Second name part of the file name '{}' needs to be an indent_style",
file_path.display()
)
});
let indent_size = name_parts
.next()
.expect("There have to be a third name part")
.parse()
.unwrap_or_else(|_| {
panic!(
"Third part of the file name '{}' is no valid indent_size",
file_path.display()
)
});
let tab_width = name_parts
.next()
.expect("There have to be a fourth name part")
.parse()
.unwrap_or_else(|_| {
panic!(
"Fourth part of the file name '{}' is no valid tab_width",
file_path.display()
)
});
let lines = name_parts
.map(|expected_line| {
expected_line.parse().unwrap_or_else(|_| {
panic!(
"'{expected_line}' is no valid line number in file name of {}",
file_path.display()
)
})
})
.collect();
(indent_style, indent_size, tab_width, lines)
}
}
#[allow(dead_code)] pub(crate) mod insert_final_newline {
use std::path::Path;
use itertools::Itertools;
pub fn get_needs_final_newline_from_file_name(file_path: &Path) -> bool {
let file_name = file_path.file_stem().unwrap().to_str().unwrap();
if let Some((_, indicator)) = file_name.split("-").collect_tuple() {
match indicator {
"needs_final_newline" => true,
"needs_no_final_newline" => false,
_ => panic!("'{indicator}' is no supported indicator"),
}
} else {
panic!("File name '{file_name}' has no two parts separated by '-'")
}
}
}
#[allow(dead_code)] pub(crate) mod spelling_language {
use std::path::Path;
use itertools::Itertools;
pub fn is_faulty(file_path: &Path) -> bool {
let file_name = file_path.file_stem().unwrap().to_str().unwrap();
if let Some((_, indicator)) = file_name.split("-").collect_tuple() {
match indicator {
"faulty" => true,
"correct" => false,
_ => panic!("'{indicator}' is no supported indicator"),
}
} else {
panic!("File name '{file_name}' has no two parts separated by '-'")
}
}
}
#[allow(dead_code)] pub(crate) mod trim_trailing_whitespace {
use std::path::Path;
pub fn get_lines_with_trailing_whitespace_from_file_name(file_path: &Path) -> Vec<usize> {
file_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.split("-")
.skip(1) .map(|expected_line| {
expected_line.parse().unwrap_or_else(|_| panic!(
"'{expected_line}' is no valid line number 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() }
}
#[allow(dead_code)] pub(crate) fn get_test_file_paths(&self) -> impl Iterator<Item = PathBuf> {
self.get_test_dir_content()
.filter(|path| {
path.extension()
.is_none_or(|ext| ext != "editorconfig" && ext != "license")
})
.inspect(|path| println!("Test file {}", path.display()))
}
#[allow(dead_code)] pub(crate) fn get_test_editorconfig_paths(&self) -> impl Iterator<Item = PathBuf> {
self.get_test_dir_content()
.filter(|path| path.extension().is_some_and(|ext| ext == "editorconfig"))
.inspect(|path| println!("Test .editorconfig file {}", path.display()))
}
pub(crate) fn get_test_dir(&self) -> PathBuf {
["tests", "resources", self.dir_name].into_iter().collect()
}
fn get_test_dir_content(&self) -> impl Iterator<Item = PathBuf> {
fs::read_dir(self.get_test_dir())
.expect("tests resources have to exist")
.map(|entry| {
entry
.expect("content of test directory have to be accessible")
.path()
})
}
}
#[allow(dead_code)] pub fn get_repo() -> PathBuf {
path::absolute(get_repo_relative()).expect("Absolute path to repository have to be available")
}
#[allow(dead_code)] pub fn get_repo_relative() -> PathBuf {
PathBuf::from("./")
}