Skip to main content

olai_stack_state/
error.rs

1//! The crate's [`Error`].
2
3/// What can go wrong reading or writing the [`Store`](crate::Store).
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6    /// SQLite failed.
7    #[error("state store: {0}")]
8    Sqlite(#[from] rusqlite::Error),
9    /// The store was written by a newer version than this one understands.
10    #[error(
11        "state store schema version {found} is newer than this version supports ({supported}); \
12         upgrade trestle"
13    )]
14    UnsupportedVersion {
15        /// The store's schema version.
16        found: u32,
17        /// The newest version this crate reads.
18        supported: u32,
19    },
20    /// A name is not valid (see [`is_valid_name`](olai_stack_topology::workspace::is_valid_name)).
21    #[error(
22        "`{0}` is not a valid name: use lowercase letters, digits and dashes, starting with a letter"
23    )]
24    InvalidName(String),
25    /// Something already exists under that name.
26    #[error("{kind} `{name}` already exists")]
27    Duplicate {
28        /// What kind of thing (`workspace`, `environment`).
29        kind: &'static str,
30        /// Its name.
31        name: String,
32    },
33    /// No such thing.
34    #[error("no {kind} `{id}`")]
35    NotFound {
36        /// What kind of thing.
37        kind: &'static str,
38        /// Its id or name.
39        id: String,
40    },
41    /// Every slot is leased.
42    #[error("no free slot: the host runs at most {} instances", olai_stack_topology::MAX_SLOT + 1)]
43    SlotsExhausted,
44    /// An environment manifest didn't (de)serialize.
45    #[error(transparent)]
46    Manifest(#[from] olai_stack_topology::ManifestError),
47    /// A workspace manifest didn't (de)serialize.
48    #[error(transparent)]
49    Workspace(#[from] olai_stack_topology::WorkspaceError),
50    /// A stored JSON column didn't (de)serialize.
51    #[error("state store JSON: {0}")]
52    Json(#[from] serde_json::Error),
53    /// Reading or writing an import/export directory failed.
54    #[error("{path}: {source}")]
55    Io {
56        /// The file or directory.
57        path: std::path::PathBuf,
58        /// The I/O error.
59        #[source]
60        source: std::io::Error,
61    },
62    /// An export can't be written as asked.
63    #[error("can't export: {0}")]
64    Export(String),
65}
66
67impl Error {
68    pub(crate) fn io(path: impl Into<std::path::PathBuf>, source: std::io::Error) -> Self {
69        Error::Io {
70            path: path.into(),
71            source,
72        }
73    }
74}
75
76/// The crate's result type.
77pub type Result<T, E = Error> = std::result::Result<T, E>;