1use std::{io, path::PathBuf};
2use thiserror::Error;
3
4pub type CoreResult<T> = Result<T, CapsulaError>;
6
7#[derive(Debug, Error)]
13pub enum CapsulaError {
14 #[error("I/O error at {path:?}: {source}")]
16 Io {
17 path: Option<PathBuf>,
18 #[source]
19 source: io::Error,
20 },
21
22 #[error("Serialization failed: {0}")]
24 Serialization(#[from] serde_json::Error),
25
26 #[error("Configuration error: {message}")]
28 Configuration { message: String },
29
30 #[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 #[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
54impl 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}