Skip to main content

cashu/nuts/nut26/
error.rs

1//! NUT-26 Error types
2
3use std::fmt;
4
5/// NUT-26 specific errors
6#[derive(Debug)]
7pub enum Error {
8    /// Invalid bech32m prefix (expected "creqb")
9    InvalidPrefix,
10    /// Invalid TLV structure (missing required fields, unexpected values, malformed TLV)
11    InvalidStructure,
12    /// Invalid length of a TLV field or the overall structure
13    InvalidLength,
14    /// Invalid UTF-8 in string field
15    InvalidUtf8,
16    /// Invalid public key
17    InvalidPubkey,
18    /// Unknown NUT-10 kind
19    UnknownKind(u8),
20    /// Tag too long (>255 bytes)
21    TagTooLong,
22    /// Bech32 encoding error
23    Bech32Error(bitcoin::bech32::DecodeError),
24    /// Base64 decoding error
25    Base64DecodeError(bitcoin::base64::DecodeError),
26    /// CBOR serialization error
27    CborError(ciborium::de::Error<std::io::Error>),
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Error::InvalidPrefix => write!(f, "Invalid bech32m prefix, expected 'creqb'"),
34            Error::InvalidStructure => write!(f, "Invalid TLV structure"),
35            Error::InvalidLength => write!(f, "Invalid TLV field length"),
36            Error::InvalidUtf8 => write!(f, "Invalid UTF-8 encoding in string field"),
37            Error::InvalidPubkey => write!(f, "Invalid public key"),
38            Error::UnknownKind(kind) => write!(f, "Unknown NUT-10 kind: {}", kind),
39            Error::TagTooLong => write!(f, "Tag exceeds 255 byte limit"),
40            Error::Bech32Error(e) => write!(f, "Bech32 error: {}", e),
41            Error::Base64DecodeError(e) => write!(f, "Base64 decode error: {}", e),
42            Error::CborError(e) => write!(f, "CBOR error: {}", e),
43        }
44    }
45}
46
47impl std::error::Error for Error {}
48
49impl From<bitcoin::bech32::DecodeError> for Error {
50    fn from(e: bitcoin::bech32::DecodeError) -> Self {
51        Error::Bech32Error(e)
52    }
53}
54
55impl From<bitcoin::base64::DecodeError> for Error {
56    fn from(e: bitcoin::base64::DecodeError) -> Self {
57        Error::Base64DecodeError(e)
58    }
59}
60
61impl From<ciborium::de::Error<std::io::Error>> for Error {
62    fn from(e: ciborium::de::Error<std::io::Error>) -> Self {
63        Error::CborError(e)
64    }
65}