capsula_core/
error.rs

1use std::{io, path::PathBuf};
2use thiserror::Error;
3
4/// Library-wide result alias
5pub type CoreResult<T> = Result<T, CapsulaError>;
6
7/// Core error type for the Capsula library
8///
9/// This enum defines common infrastructure errors. Context-specific errors
10/// should be defined in their respective crates and converted to CoreError
11/// via the ContextFailed variant.
12#[derive(Debug, Error)]
13pub enum CapsulaError {
14    /// I/O operation failed
15    #[error("I/O error at {path:?}: {source}")]
16    Io {
17        path: Option<PathBuf>,
18        #[source]
19        source: io::Error,
20    },
21
22    /// Serialization/deserialization failed
23    #[error("Serialization failed: {0}")]
24    Serialization(#[from] serde_json::Error),
25
26    /// Configuration-related error
27    #[error("Configuration error: {message}")]
28    Configuration { message: String },
29
30    /// Context execution failed
31    /// This variant wraps context-specific errors while preserving the error chain
32    #[error("Context '{context}' failed: {message}")]
33    ContextFailed {
34        context: String,
35        message: String,
36        #[source]
37        source: Box<dyn std::error::Error + Send + Sync>,
38    },
39
40    /// Generic error for unexpected conditions
41    #[error("{0}")]
42    Other(String),
43}
44
45impl From<std::io::Error> for CapsulaError {
46    fn from(e: std::io::Error) -> Self {
47        CapsulaError::Io {
48            path: None,
49            source: e,
50        }
51    }
52}
53
54/// Helper to create I/O errors with path context
55impl CapsulaError {
56    pub fn io_with_path(path: impl Into<PathBuf>, source: io::Error) -> Self {
57        CapsulaError::Io {
58            path: Some(path.into()),
59            source,
60        }
61    }
62}