rustywallet_descriptor/
error.rs

1//! Error types for descriptor parsing and operations
2
3use thiserror::Error;
4
5/// Errors that can occur when working with descriptors
6#[derive(Error, Debug, Clone, PartialEq, Eq)]
7pub enum DescriptorError {
8    /// Invalid descriptor syntax
9    #[error("Invalid descriptor syntax at position {position}: {message}")]
10    ParseError {
11        /// Position in the string where error occurred
12        position: usize,
13        /// Error message
14        message: String,
15    },
16
17    /// Invalid checksum
18    #[error("Invalid checksum: expected {expected}, got {got}")]
19    InvalidChecksum {
20        /// Expected checksum
21        expected: String,
22        /// Actual checksum
23        got: String,
24    },
25
26    /// Missing checksum
27    #[error("Missing checksum")]
28    MissingChecksum,
29
30    /// Invalid key
31    #[error("Invalid key: {0}")]
32    InvalidKey(String),
33
34    /// Invalid public key
35    #[error("Invalid public key: {0}")]
36    InvalidPublicKey(String),
37
38    /// Invalid extended key
39    #[error("Invalid extended key: {0}")]
40    InvalidExtendedKey(String),
41
42    /// Invalid derivation path
43    #[error("Invalid derivation path: {0}")]
44    InvalidDerivationPath(String),
45
46    /// Invalid fingerprint
47    #[error("Invalid fingerprint: {0}")]
48    InvalidFingerprint(String),
49
50    /// Invalid threshold
51    #[error("Invalid threshold: k={k} but n={n}")]
52    InvalidThreshold {
53        /// Required signatures
54        k: usize,
55        /// Total keys
56        n: usize,
57    },
58
59    /// Unsupported descriptor type
60    #[error("Unsupported descriptor type: {0}")]
61    UnsupportedType(String),
62
63    /// Wildcard not allowed
64    #[error("Wildcard not allowed in this context")]
65    WildcardNotAllowed,
66
67    /// Index out of range
68    #[error("Derivation index out of range: {0}")]
69    IndexOutOfRange(u32),
70
71    /// HD derivation error
72    #[error("HD derivation error: {0}")]
73    DerivationError(String),
74
75    /// Address generation error
76    #[error("Address generation error: {0}")]
77    AddressError(String),
78
79    /// Script generation error
80    #[error("Script generation error: {0}")]
81    ScriptError(String),
82
83    /// Empty descriptor
84    #[error("Empty descriptor")]
85    EmptyDescriptor,
86
87    /// Unexpected end of input
88    #[error("Unexpected end of input")]
89    UnexpectedEnd,
90
91    /// Unexpected character
92    #[error("Unexpected character '{0}' at position {1}")]
93    UnexpectedChar(char, usize),
94}
95
96impl DescriptorError {
97    /// Create a parse error at a specific position
98    pub fn parse_error(position: usize, message: impl Into<String>) -> Self {
99        Self::ParseError {
100            position,
101            message: message.into(),
102        }
103    }
104}