Skip to main content

ic_storage/adapter/
error.rs

1///
2/// `CollectionError`
3///
4/// A rejected collection operation. Encoding, validation and capacity errors
5/// precede writes; backing panics still follow the host's failure contract.
6///
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
9pub enum CollectionError {
10    /// Application encoding or declared size is invalid.
11    #[error("{0}")]
12    Codec(#[from] crate::storable::CodecError),
13    /// The persisted record or collection structure is invalid.
14    #[error("invalid collection: {0}")]
15    Corrupt(&'static str),
16    /// The bytes belong to another format.
17    #[error("unrecognized collection format")]
18    Format,
19    /// Record integrity verification failed.
20    #[error("collection checksum mismatch")]
21    Checksum,
22    /// A record, arithmetic operation or allocation exceeds its resource budget.
23    #[error("collection resource budget exceeded")]
24    Budget,
25    /// Capacity could not be reserved.
26    #[error("collection growth refused")]
27    Growth,
28    /// Creation was attempted in nonempty memory.
29    #[error("collection memory is not empty")]
30    NotEmpty,
31    /// A prior backing failure prevents further access through this owner.
32    #[error("collection owner is poisoned")]
33    Poisoned,
34    /// An index or range is invalid.
35    #[error("invalid collection index or range")]
36    Bounds,
37    /// Bounded Wire encoding or decoding failed.
38    #[error("collection Wire record: {0}")]
39    Wire(#[from] ic_wire::Error),
40    /// Cell envelope admission failed before reading its payload.
41    #[error("{0}")]
42    Cell(#[from] crate::StableCellPayloadError),
43}
44
45pub(super) type Result<T> = std::result::Result<T, CollectionError>;
46
47pub(super) fn ensure(condition: bool, error: CollectionError) -> Result<()> {
48    if condition { Ok(()) } else { Err(error) }
49}