Skip to main content

mini_build/
error.rs

1use std::fmt;
2
3use crate::source::SourceError;
4
5/// Why a build could not be configured or could not run.
6///
7/// The split is deliberate: `Io` and `Config` are reported while the [`crate::Builder`]
8/// is being assembled, before any file is touched, and `ToolMissing` before any tool is
9/// invoked. Only `Build` can arrive after work has started. A caller can therefore treat
10/// the first three as "you configured this wrong" and the last as "the build failed",
11/// which are different problems for different people.
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum BuildError {
15    /// A configured path could not be canonicalized — most often, it does not exist.
16    Io(std::io::Error),
17    /// A path relationship the builder refuses: a source folder overlapping the output
18    /// dir, two source folders overlapping each other, or a JS bundle entry outside every
19    /// registered source folder.
20    Config(String),
21    /// A configured tool's binary is not on `PATH`, discovered before any build work runs
22    /// rather than partway through.
23    ToolMissing(String),
24    /// A pipeline ran and failed.
25    Build(SourceError),
26}
27
28impl fmt::Display for BuildError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            BuildError::Io(e) => write!(f, "io error: {e}"),
32            BuildError::Config(msg) => write!(f, "invalid configuration: {msg}"),
33            BuildError::ToolMissing(msg) => write!(f, "required tool missing: {msg}"),
34            BuildError::Build(e) => write!(f, "build failed: {e}"),
35        }
36    }
37}
38
39impl std::error::Error for BuildError {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        match self {
42            BuildError::Io(e) => Some(e),
43            BuildError::Build(e) => Some(e),
44            _ => None,
45        }
46    }
47}
48
49impl From<std::io::Error> for BuildError {
50    fn from(e: std::io::Error) -> Self {
51        BuildError::Io(e)
52    }
53}
54
55impl From<SourceError> for BuildError {
56    fn from(e: SourceError) -> Self {
57        BuildError::Build(e)
58    }
59}
60
61#[cfg(test)]
62#[path = "../tests/unit/error.rs"]
63mod tests;