Skip to main content

prikk_error/
lib.rs

1#![forbid(unsafe_code)]
2#![warn(missing_docs)]
3
4//! Shared error taxonomy for Prikk crates.
5
6use core::fmt;
7
8/// Shared result type.
9pub type Result<T> = core::result::Result<T, PrikkError>;
10
11/// Error type used by the initial implementation crates.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum PrikkError {
14    /// Canonical encoding failed because input data violates the frozen schema contract.
15    CanonicalEncoding(String),
16    /// An object identifier had an invalid form.
17    InvalidObjectId(String),
18    /// A signature had an invalid form or did not match its envelope context.
19    InvalidSignature(String),
20    /// A path-like name failed Prikk path/ref validation.
21    InvalidName(String),
22    /// A persistent object had an unexpected type.
23    ObjectTypeMismatch {
24        /// The object type required by the caller.
25        expected: String,
26        /// The object type actually found in the stored envelope.
27        actual: String,
28    },
29    /// The persistent format version is unsupported.
30    UnsupportedFormatVersion(u32),
31    /// A persisted object or record has malformed bytes.
32    MalformedData(String),
33    /// A persisted object was found at a path that does not match its computed ID.
34    Integrity(String),
35    /// A lock could not be acquired because another writer may be active.
36    LockConflict(String),
37    /// The requested object type cannot be persisted in the requested store.
38    UnsupportedObjectType(String),
39    /// Placeholder for I/O errors without making this crate depend on std::io::Error in variants.
40    Io(String),
41}
42
43impl fmt::Display for PrikkError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::CanonicalEncoding(msg) => write!(f, "canonical encoding error: {msg}"),
47            Self::InvalidObjectId(msg) => write!(f, "invalid object id: {msg}"),
48            Self::InvalidSignature(msg) => write!(f, "invalid signature: {msg}"),
49            Self::InvalidName(msg) => write!(f, "invalid name: {msg}"),
50            Self::ObjectTypeMismatch { expected, actual } => {
51                write!(f, "object type mismatch: expected {expected}, got {actual}")
52            }
53            Self::UnsupportedFormatVersion(version) => {
54                write!(f, "unsupported format version: {version}")
55            }
56            Self::MalformedData(msg) => write!(f, "malformed persisted data: {msg}"),
57            Self::Integrity(msg) => write!(f, "integrity error: {msg}"),
58            Self::LockConflict(msg) => write!(f, "lock conflict: {msg}"),
59            Self::UnsupportedObjectType(msg) => write!(f, "unsupported object type: {msg}"),
60            Self::Io(msg) => write!(f, "i/o error: {msg}"),
61        }
62    }
63}
64
65impl std::error::Error for PrikkError {}
66
67impl From<std::io::Error> for PrikkError {
68    fn from(value: std::io::Error) -> Self {
69        Self::Io(value.to_string())
70    }
71}