lnmp_envelope/
error.rs

1//! Error types for LNMP Envelope
2
3use thiserror::Error;
4
5/// Errors that can occur when working with envelopes
6#[derive(Debug, Error, PartialEq)]
7pub enum EnvelopeError {
8    /// Invalid TLV type code
9    #[error("Invalid TLV type code: {0:#x}")]
10    InvalidTlvType(u8),
11
12    /// Invalid TLV length
13    #[error("Invalid TLV length: {0}")]
14    InvalidTlvLength(usize),
15
16    /// Unexpected end of data
17    #[error("Unexpected end of data at offset {0}")]
18    UnexpectedEof(usize),
19
20    /// Invalid UTF-8 in string field
21    #[error("Invalid UTF-8 in field: {0}")]
22    InvalidUtf8(#[from] std::string::FromUtf8Error),
23
24    /// Invalid timestamp value
25    #[error("Invalid timestamp: {0}")]
26    InvalidTimestamp(u64),
27
28    /// Invalid sequence value
29    #[error("Invalid sequence: {0}")]
30    InvalidSequence(u64),
31
32    /// String field exceeds maximum length
33    #[error("String field '{0}' exceeds maximum length of {1} bytes")]
34    StringTooLong(String, usize),
35
36    /// Malformed envelope header in text format
37    #[error("Malformed envelope header: {0}")]
38    MalformedHeader(String),
39
40    /// Duplicate TLV entry
41    #[error("Duplicate TLV entry for type {0:#x}")]
42    DuplicateTlvEntry(u8),
43
44    /// TLV entries not in canonical order
45    #[error("TLV entries not in canonical order: {0:#x} after {1:#x}")]
46    NonCanonicalOrder(u8, u8),
47
48    /// IO error
49    #[error("IO error: {0}")]
50    Io(String),
51}
52
53impl From<std::io::Error> for EnvelopeError {
54    fn from(err: std::io::Error) -> Self {
55        EnvelopeError::Io(err.to_string())
56    }
57}
58
59/// Result type for envelope operations
60pub type Result<T> = std::result::Result<T, EnvelopeError>;