Skip to main content

cesr/core/indexer/
error.rs

1use super::code::IndexedSigCode;
2#[cfg(feature = "alloc")]
3#[allow(
4    unused_imports,
5    reason = "alloc prelude items; subset used per cfg/feature combination"
6)]
7use alloc::string::String;
8use thiserror::Error as ThisError;
9
10/// Errors produced while parsing an indexed CESR signature stream.
11#[derive(Debug, ThisError, PartialEq, Eq)]
12pub enum IndexerParseError {
13    /// The input stream is empty.
14    #[error("empty CESR stream")]
15    EmptyStream,
16
17    /// The stream ended before enough characters were available.
18    #[error("stream too short: need {need} chars, got {got}")]
19    StreamTooShort {
20        /// Number of characters needed.
21        need: usize,
22        /// Number of characters available.
23        got: usize,
24    },
25
26    /// The code prefix does not match any known indexed signature code.
27    #[error("unknown indexed sig code: '{0}'")]
28    UnknownCode(String),
29
30    /// A Base64 decode failure was encountered in the stream.
31    #[error("invalid base64 in indexed sig stream")]
32    InvalidBase64,
33
34    /// Non-canonical encoding: index had unnecessary leading zeros.
35    #[error("non-canonical encoding: leading zeros in indexed sig")]
36    NonCanonical,
37
38    /// The ondex field was non-zero for a `CurrentOnly` code.
39    #[error("ondex must be 0 for current-only code, got {0}")]
40    OndexNotZeroForCurrentOnly(u32),
41
42    /// The computed full size overflowed `usize` (soft-field index too large).
43    #[error("indexer full size overflow")]
44    SizeOverflow,
45}
46
47/// Errors produced while validating an indexed CESR signature builder.
48#[derive(Debug, ThisError, PartialEq, Eq)]
49pub enum IndexerValidationError {
50    /// The signer index exceeds the maximum allowed by the code.
51    #[error("index {index} exceeds max {max} for code {code:?}")]
52    IndexTooLarge {
53        /// The indexed signature code.
54        code: IndexedSigCode,
55        /// The index that was supplied.
56        index: u32,
57        /// The maximum index permitted.
58        max: u32,
59    },
60
61    /// The ondex exceeds the maximum allowed by the code.
62    #[error("ondex {ondex} exceeds max {max} for code {code:?}")]
63    OndexTooLarge {
64        /// The indexed signature code.
65        code: IndexedSigCode,
66        /// The ondex that was supplied.
67        ondex: u32,
68        /// The maximum ondex permitted.
69        max: u32,
70    },
71
72    /// An ondex was provided for a `CurrentOnly` code which has no ondex field.
73    #[error("ondex not allowed for current-only code {0:?}")]
74    OndexOnCurrentOnly(IndexedSigCode),
75
76    /// The ondex differs from index on a Both-mode code with os=0 (no wire
77    /// space for a separate ondex). keripy raises `InvalidVarIndexError` here.
78    #[error("ondex {ondex} must equal index {index} for code {code:?} (os=0)")]
79    OndexMustEqualIndex {
80        /// The indexed signature code.
81        code: IndexedSigCode,
82        /// The index that was supplied.
83        index: u32,
84        /// The ondex that was supplied.
85        ondex: u32,
86    },
87
88    /// The raw byte slice length does not match the code's expected raw size.
89    #[error("unexpected raw size for {code:?}: expected {expected}, got {got}")]
90    UnexpectedRawSize {
91        /// The indexed signature code.
92        code: IndexedSigCode,
93        /// Expected number of bytes.
94        expected: usize,
95        /// Actual number of bytes received.
96        got: usize,
97    },
98}
99
100impl From<super::code::CodeError> for IndexerParseError {
101    fn from(e: super::code::CodeError) -> Self {
102        match e {
103            super::code::CodeError::UnknownCode(s) => Self::UnknownCode(s),
104        }
105    }
106}
107
108impl From<crate::b64::error::Error> for IndexerParseError {
109    fn from(e: crate::b64::error::Error) -> Self {
110        match e {
111            crate::b64::error::Error::InvalidBase64Char(_)
112            | crate::b64::error::Error::InvalidBase64Value(_)
113            | crate::b64::error::Error::Misaligned { .. }
114            | crate::b64::error::Error::IntegerOverflow => Self::InvalidBase64,
115            crate::b64::error::Error::ShortBinaryStream => Self::StreamTooShort { need: 0, got: 0 },
116        }
117    }
118}