Skip to main content

appcore_dnt/
error.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: error.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/08/02 00:04:12 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/08/02 10:29:16 by dnettoRaw
8//      ###########      S: 1.0.1-rc.8
9// =============================================================================
10
11//! DNT error contracts.
12
13/// DNT-local result type.
14pub type DntResult<T> = Result<T, DntError>;
15
16/// Controlled DNT failures.
17#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
18pub enum DntError {
19    /// The bytes do not contain a structurally valid DNT envelope.
20    #[error("invalid DNT container")]
21    InvalidFormat,
22    /// The envelope version is newer or otherwise unsupported.
23    #[error("unsupported DNT envelope version")]
24    UnsupportedVersion,
25    /// The configured maximum payload size would be exceeded.
26    #[error("DNT payload exceeds configured limit")]
27    PayloadTooLarge,
28    /// The envelope contains an impossible or unsupported flag combination.
29    #[error("DNT flags are invalid")]
30    InvalidFlags,
31    /// The envelope is not bound to the expected context.
32    #[error("DNT context mismatch")]
33    ContextMismatch,
34    /// The key provider could not return usable material.
35    #[error("DNT key is unavailable")]
36    KeyUnavailable,
37    /// AEAD authentication failed.
38    #[error("DNT authentication failed")]
39    AuthenticationFailed,
40    /// The codec identifier is unknown or incompatible.
41    #[error("DNT codec is unavailable")]
42    CodecUnavailable,
43    /// Encoding or decoding failed without exposing payload material.
44    #[error("DNT codec failed")]
45    CodecFailed,
46    /// Filesystem persistence failed.
47    #[error("DNT I/O failed")]
48    Io,
49}
50
51/// Controlled key-provider failures.
52#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
53pub enum DntKeyError {
54    /// The requested key identity is unknown.
55    #[error("DNT key was not found")]
56    NotFound,
57    /// The key provider is intentionally unavailable.
58    #[error("DNT key provider is unavailable")]
59    Unavailable,
60    /// The resolved key material is malformed.
61    #[error("DNT key material is invalid")]
62    InvalidKey,
63    /// The key is not valid for the supplied context.
64    #[error("DNT key is not valid for the requested context")]
65    Forbidden,
66}
67
68/// Controlled codec failures.
69#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
70pub enum CodecError {
71    /// The codec cannot encode the supplied bytes.
72    #[error("DNT codec encode failed")]
73    EncodeFailed,
74    /// The codec cannot decode the supplied bytes.
75    #[error("DNT codec decode failed")]
76    DecodeFailed,
77    /// The payload violates a codec-specific bound.
78    #[error("DNT codec payload is too large")]
79    PayloadTooLarge,
80}
81
82impl From<DntKeyError> for DntError {
83    fn from(value: DntKeyError) -> Self {
84        match value {
85            DntKeyError::NotFound | DntKeyError::Unavailable => Self::KeyUnavailable,
86            DntKeyError::InvalidKey => Self::KeyUnavailable,
87            DntKeyError::Forbidden => Self::ContextMismatch,
88        }
89    }
90}
91
92impl From<CodecError> for DntError {
93    fn from(_: CodecError) -> Self {
94        Self::CodecFailed
95    }
96}