use crate::parser::Config;
const VALID_TYPES: [&'static str; 26] = [
"f", "w", "d", "D", "e", "v", "q", "Q", "p", "p+", "L", "c", "b", "C", "x", "X", "r", "R", "z",
"Z", "t", "T", "h", "H", "a", "A",
];
const VALID_TYPE_MODIFIERS: [&'static str; 6] = ["+", "!", "-", "=", "~", "^"];
const VALID_SPECIFIERS: [&'static str; 25] = [
"%a", "%A", "%b", "%B", "%C", "%g", "%G", "%h", "%H", "%l", "%L", "%m", "%M", "%o", "%q", "%S",
"%t", "%T", "%u", "%U", "%v", "%V", "%w", "%W", "%%",
];
pub fn sanity_check(config: &Config) -> Result<(), anyhow::Error> {
let mut specifier_prefix: bool = false;
if VALID_SPECIFIERS.contains(
&config
.file_type
.as_str()) == true {
specifier_prefix = true;
}
if VALID_TYPES.contains(
&config
.file_type
.chars()
.next()
.unwrap()
.to_string()
.as_str(),
) == false && specifier_prefix == false
{
anyhow::bail!("Invalid file type!");
}
if config.file_type.len() > 1 {
if VALID_TYPE_MODIFIERS.contains(
&config
.file_type
.chars()
.nth(1)
.unwrap()
.to_string()
.as_str(),
) == false && specifier_prefix == false
{
anyhow::bail!("Invalid file type!")
}
}
if !config.path.is_absolute() {
anyhow::bail!("Path does not begin with the root!");
}
if config.mode.parse::<i32>().is_err() {
if config.mode != "-" {
anyhow::bail!("Invalid file mode!");
}
}
Ok(())
}