Skip to main content

infinity_build_core/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4pub type BuildResult<T> = Result<T, BuildError>;
5
6#[derive(Debug, Error)]
7pub enum BuildError {
8    #[error("invalid path {path}: {reason}")]
9    InvalidPath { path: PathBuf, reason: String },
10
11    #[error("invalid config: {0}")]
12    InvalidConfig(String),
13
14    #[error("{stage} failed: {message}")]
15    BackendFailure {
16        stage: &'static str,
17        message: String,
18    },
19
20    #[error("io error at {path}: {source}")]
21    Io {
22        path: PathBuf,
23        #[source]
24        source: std::io::Error,
25    },
26
27    #[error(transparent)]
28    Backend(#[from] anyhow::Error),
29}
30
31impl BuildError {
32    pub fn invalid_path(path: impl Into<PathBuf>, reason: impl Into<String>) -> Self {
33        Self::InvalidPath {
34            path: path.into(),
35            reason: reason.into(),
36        }
37    }
38
39    pub fn invalid_config(msg: impl Into<String>) -> Self {
40        Self::InvalidConfig(msg.into())
41    }
42
43    pub fn backend_failure(stage: &'static str, message: impl Into<String>) -> Self {
44        Self::BackendFailure {
45            stage,
46            message: message.into(),
47        }
48    }
49
50    pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
51        Self::Io {
52            path: path.into(),
53            source,
54        }
55    }
56}