1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! When something goes wrong

use core::fmt;

/// Length of the argument is not valid
#[derive(Debug)]
pub struct InvalidLength;

impl fmt::Display for InvalidLength {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid length")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidLength {}

/// Value was out of range
#[derive(Debug)]
pub struct OutOfRange;

impl fmt::Display for OutOfRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("out of range")
    }
}

#[cfg(feature = "std")]
impl std::error::Error for OutOfRange {}

/// Error returned by parsing child index
#[derive(Debug)]
pub enum ParseChildIndexError {
    /// Indicates that parsing an `u32` integer failed
    ParseInt(core::num::ParseIntError),
    /// Parsed index was out of acceptable range
    IndexNotInRange(OutOfRange),
}

impl fmt::Display for ParseChildIndexError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ParseInt(_) => f.write_str("child index is not valid u32 integer"),
            Self::IndexNotInRange(_) => f.write_str("child index is not in acceptable range"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseChildIndexError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ParseChildIndexError::ParseInt(e) => Some(e),
            ParseChildIndexError::IndexNotInRange(e) => Some(e),
        }
    }
}