slip_10/
errors.rs

1//! When something goes wrong
2
3use core::fmt;
4
5/// Length of the argument is not valid
6#[derive(Debug)]
7pub struct InvalidLength;
8
9impl fmt::Display for InvalidLength {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        f.write_str("invalid length")
12    }
13}
14
15#[cfg(feature = "std")]
16impl std::error::Error for InvalidLength {}
17
18/// Value was out of range
19#[derive(Debug)]
20pub struct OutOfRange;
21
22impl fmt::Display for OutOfRange {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.write_str("out of range")
25    }
26}
27
28#[cfg(feature = "std")]
29impl std::error::Error for OutOfRange {}
30
31/// Error returned by parsing child index
32#[derive(Debug)]
33pub enum ParseChildIndexError {
34    /// Indicates that parsing an `u32` integer failed
35    ParseInt(core::num::ParseIntError),
36    /// Parsed index was out of acceptable range
37    IndexNotInRange(OutOfRange),
38}
39
40impl fmt::Display for ParseChildIndexError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::ParseInt(_) => f.write_str("child index is not valid u32 integer"),
44            Self::IndexNotInRange(_) => f.write_str("child index is not in acceptable range"),
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl std::error::Error for ParseChildIndexError {
51    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
52        match self {
53            ParseChildIndexError::ParseInt(e) => Some(e),
54            ParseChildIndexError::IndexNotInRange(e) => Some(e),
55        }
56    }
57}