mini-build 0.1.0

Builds the directory a static server serves: CSS/JS bundling and minification via external tools, plus asset mirroring.
Documentation
use std::fmt;

use crate::source::SourceError;

/// Why a build could not be configured or could not run.
///
/// The split is deliberate: `Io` and `Config` are reported while the [`crate::Builder`]
/// is being assembled, before any file is touched, and `ToolMissing` before any tool is
/// invoked. Only `Build` can arrive after work has started. A caller can therefore treat
/// the first three as "you configured this wrong" and the last as "the build failed",
/// which are different problems for different people.
#[derive(Debug)]
#[non_exhaustive]
pub enum BuildError {
    /// A configured path could not be canonicalized — most often, it does not exist.
    Io(std::io::Error),
    /// A path relationship the builder refuses: a source folder overlapping the output
    /// dir, two source folders overlapping each other, or a JS bundle entry outside every
    /// registered source folder.
    Config(String),
    /// A configured tool's binary is not on `PATH`, discovered before any build work runs
    /// rather than partway through.
    ToolMissing(String),
    /// A pipeline ran and failed.
    Build(SourceError),
}

impl fmt::Display for BuildError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BuildError::Io(e) => write!(f, "io error: {e}"),
            BuildError::Config(msg) => write!(f, "invalid configuration: {msg}"),
            BuildError::ToolMissing(msg) => write!(f, "required tool missing: {msg}"),
            BuildError::Build(e) => write!(f, "build failed: {e}"),
        }
    }
}

impl std::error::Error for BuildError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            BuildError::Io(e) => Some(e),
            BuildError::Build(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for BuildError {
    fn from(e: std::io::Error) -> Self {
        BuildError::Io(e)
    }
}

impl From<SourceError> for BuildError {
    fn from(e: SourceError) -> Self {
        BuildError::Build(e)
    }
}

#[cfg(test)]
#[path = "../tests/unit/error.rs"]
mod tests;