use core::result::Result;
use std::path::Path;
use anyhow::Result as anyResult;
use charset::CharsetHandler;
use end_of_line::EndOfLineHandler;
use log::info;
use crate::cli::EditorConfigArgs;
mod charset;
mod end_of_line;
pub mod errors;
pub fn check_file(path: &Path, editorconfig_args: &EditorConfigArgs) -> anyResult<()> {
info!("check {}", path.display());
for handler in build_handlers(path, editorconfig_args)? {
handler.check(path)?
}
Ok(())
}
pub fn fix_file(path: &Path, editorconfig_args: &EditorConfigArgs) -> anyResult<()> {
info!("fix {}", path.display());
for handler in build_handlers(path, editorconfig_args)? {
handler.fix(path)?
}
Ok(())
}
fn build_handlers(
file_path: &Path,
editorconfig_args: &EditorConfigArgs,
) -> Result<Box<[BoxedPropHandler]>, ec4rs::Error> {
let handler_count = 2; let properties = ec4rs::properties_of(file_path)?;
let mut handlers: Vec<Option<Box<dyn PropertyHandler>>> = Vec::with_capacity(handler_count);
if editorconfig_args.charset {
handlers.push(CharsetHandler::build(&properties).map(|h| Box::new(h) as BoxedPropHandler));
}
if editorconfig_args.end_of_line {
handlers
.push(EndOfLineHandler::build(&properties).map(|h| Box::new(h) as BoxedPropHandler));
}
debug_assert!(handlers.len() <= handler_count, "handler count is to small");
Ok(handlers.into_iter()
.flatten() .collect::<Vec<BoxedPropHandler>>().into_boxed_slice())
}
type BoxedPropHandler = Box<dyn PropertyHandler>;
trait PropertyHandler {
fn check(&self, file_path: &Path) -> anyResult<()>;
fn fix(&self, file_path: &Path) -> anyResult<()>;
}