use crate::core::error::NeatifyError;
use anyhow::Result;
use std::fs;
use std::path::Path;
#[derive(Debug, Default, Clone)]
pub struct FormattingStats {
pub formatted_files: usize,
pub files_needing_formatting: usize,
pub total_files: usize,
}
impl FormattingStats {
pub fn new() -> Self {
Self::default()
}
pub fn merge(&mut self, other: &FormattingStats) {
self.formatted_files += other.formatted_files;
self.files_needing_formatting += other.files_needing_formatting;
self.total_files += other.total_files;
}
}
pub trait Formatter {
fn format(&self, content: &str) -> String;
fn is_supported(&self, file_path: &Path) -> bool;
fn format_file(&self, file_path: &Path, write: bool) -> Result<bool> {
if !file_path.exists() {
return Err(NeatifyError::IoError(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File does not exist: {}", file_path.display()),
))
.into());
}
if !self.is_supported(file_path) {
return Err(NeatifyError::UnsupportedFile(file_path.display().to_string()).into());
}
let content = fs::read_to_string(file_path)?;
let formatted = self.format(&content);
let needs_formatting = content != formatted;
if needs_formatting && write {
fs::write(file_path, formatted)?;
}
Ok(needs_formatting)
}
}