Skip to main content

provenance_mark/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5    /// Invalid Seed length
6    #[error("invalid seed length: expected 32 bytes, got {actual} bytes")]
7    InvalidSeedLength { actual: usize },
8
9    /// Duplicate key
10    #[error("duplicate key: {0}")]
11    DuplicateKey(String),
12
13    /// Missing key
14    #[error("missing key: {0}")]
15    MissingKey(String),
16
17    /// Invalid key
18    #[error("invalid key: {0}")]
19    InvalidKey(String),
20
21    /// Extra keys
22    #[error("wrong number of keys: expected {0}, got {1}")]
23    ExtraKeys(usize, usize),
24
25    /// Invalid key length for the given resolution
26    #[error("invalid key length: expected {expected}, got {actual}")]
27    InvalidKeyLength { expected: usize, actual: usize },
28
29    /// Invalid next key length for the given resolution
30    #[error("invalid next key length: expected {expected}, got {actual}")]
31    InvalidNextKeyLength { expected: usize, actual: usize },
32
33    /// Invalid chain ID length for the given resolution
34    #[error("invalid chain ID length: expected {expected}, got {actual}")]
35    InvalidChainIdLength { expected: usize, actual: usize },
36
37    /// Invalid message length for the given resolution
38    #[error(
39        "invalid message length: expected at least {expected}, got {actual}"
40    )]
41    InvalidMessageLength { expected: usize, actual: usize },
42
43    /// Invalid CBOR data in info field
44    #[error("invalid CBOR data in info field")]
45    InvalidInfoCbor,
46
47    /// Date out of range for serialization
48    #[error("date out of range: {details}")]
49    DateOutOfRange { details: String },
50
51    /// Invalid date components
52    #[error("invalid date: {details}")]
53    InvalidDate { details: String },
54
55    /// Missing required URL parameter
56    #[error("missing required URL parameter: {parameter}")]
57    MissingUrlParameter { parameter: String },
58
59    /// Year out of range for 2-byte serialization
60    #[error(
61        "year out of range for 2-byte serialization: must be between 2023-2150, got {year}"
62    )]
63    YearOutOfRange { year: i32 },
64
65    /// Invalid month or day
66    #[error("invalid month ({month}) or day ({day}) for year {year}")]
67    InvalidMonthOrDay { year: i32, month: u32, day: u32 },
68
69    /// Resolution serialization error
70    #[error("resolution serialization error: {details}")]
71    ResolutionError { details: String },
72
73    /// Bytewords encoding/decoding error
74    #[error("bytewords error: {0}")]
75    Bytewords(#[from] bc_ur::Error),
76
77    /// CBOR encoding/decoding error
78    #[error("CBOR error: {0}")]
79    Cbor(#[from] dcbor::Error),
80
81    /// URL parsing error
82    #[error("URL parsing error: {0}")]
83    Url(#[from] url::ParseError),
84
85    /// Base64 decoding error
86    #[error("base64 decoding error: {0}")]
87    Base64(#[from] base64::DecodeError),
88
89    /// JSON serialization error
90    #[error("JSON error: {0}")]
91    Json(#[from] serde_json::Error),
92
93    /// Integer conversion error
94    #[error("integer conversion error: {0}")]
95    TryFromInt(#[from] std::num::TryFromIntError),
96
97    #[cfg(feature = "envelope")]
98    #[error("envelope error: {0}")]
99    Envelope(#[from] bc_envelope::Error),
100
101    /// Validation error
102    #[error("validation error: {0}")]
103    Validation(#[from] crate::validate::ValidationIssue),
104}
105
106pub type Result<T> = std::result::Result<T, Error>;
107
108impl From<Error> for dcbor::Error {
109    fn from(error: Error) -> dcbor::Error {
110        match error {
111            Error::Cbor(err) => err,
112            _ => dcbor::Error::Custom(error.to_string()),
113        }
114    }
115}