ferrous-forge 1.9.6

System-wide Rust development standards enforcer
Documentation
//! File-level formatting operations

use super::utils::ensure_rustfmt_installed;
use crate::{Error, Result};
use std::path::Path;

/// Check if a single file is properly formatted
///
/// # Errors
///
/// Returns [`Error::Process`] if rustfmt is not installed or fails to execute.
pub async fn check_file_formatting(file_path: &Path) -> Result<bool> {
    ensure_rustfmt_installed().await?;

    let output = tokio::process::Command::new("rustfmt")
        .args(&["--check", "--edition", "2021"])
        .arg(file_path)
        .output()
        .await
        .map_err(|e| Error::process(format!("Failed to check file formatting: {}", e)))?;

    Ok(output.status.success())
}

/// Format a single file
///
/// # Errors
///
/// Returns [`Error::Process`] if rustfmt is not installed or fails to execute.
pub async fn format_file(file_path: &Path) -> Result<()> {
    ensure_rustfmt_installed().await?;

    let status = tokio::process::Command::new("rustfmt")
        .args(&["--edition", "2021"])
        .arg(file_path)
        .status()
        .await
        .map_err(|e| Error::process(format!("Failed to format file: {}", e)))?;

    if !status.success() {
        return Err(Error::process(format!(
            "Failed to format file: {}",
            file_path.display()
        )));
    }

    Ok(())
}