Skip to main content

intentional_core/
error.rs

1// ---
2// relationships:
3//   implements: intent-driven-polyglot-release
4// ---
5
6//! Error types shared by core operations.
7
8use std::path::PathBuf;
9
10/// Core operation result.
11pub type Result<T> = std::result::Result<T, Error>;
12
13/// Errors produced by `intentional-core`.
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16    /// The configuration uses `contract-1`, which only `intentional init` loads.
17    #[error(
18        ".intentional/config.yml uses contract-1, which this version of intentional does not load; run `intentional init` to upgrade it to contract-2"
19    )]
20    ContractUpgradeRequired,
21
22    /// A configuration or intent is invalid.
23    #[error("validation failed: {0}")]
24    Validation(String),
25
26    /// A required file could not be read or written.
27    #[error("failed to access {path}: {source}")]
28    Io {
29        /// File involved in the operation.
30        path: PathBuf,
31        /// Underlying filesystem error.
32        source: std::io::Error,
33    },
34
35    /// YAML could not be parsed or serialized.
36    #[error("invalid YAML: {0}")]
37    Yaml(#[from] serde_yaml::Error),
38
39    /// Git state could not be discovered or traversed.
40    #[error("git operation failed: {0}")]
41    Git(String),
42
43    /// A semantic version could not be represented.
44    #[error("invalid semantic version: {0}")]
45    Semver(#[from] semver::Error),
46}
47
48impl Error {
49    /// Attach a path to an I/O error.
50    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
51        Self::Io {
52            path: path.into(),
53            source,
54        }
55    }
56}