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
58
59
60
use crate::std::fmt;

/**
An error encountered buffering data.
*/
#[derive(Debug)]
pub struct Error(ErrorKind);

#[derive(Debug)]
enum ErrorKind {
    Unsupported {
        actual: &'static str,
        expected: &'static str,
    },
    #[cfg(feature = "alloc")]
    OutsideContainer { method: &'static str },
    #[cfg(not(feature = "alloc"))]
    NoAlloc { method: &'static str },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            ErrorKind::Unsupported { actual, expected } => {
                write!(f, "unexpected {}, expected {}", actual, expected)
            }
            #[cfg(feature = "alloc")]
            ErrorKind::OutsideContainer { method } => {
                write!(f, "expected a fragment while buffering {}", method)
            }
            #[cfg(not(feature = "alloc"))]
            ErrorKind::NoAlloc { method } => write!(f, "cannot allocate for {}", method),
        }
    }
}

impl Error {
    pub(crate) fn unsupported(expected: &'static str, actual: &'static str) -> Self {
        Error(ErrorKind::Unsupported { actual, expected })
    }

    #[cfg(feature = "alloc")]
    pub(crate) fn outside_container(method: &'static str) -> Self {
        Error(ErrorKind::OutsideContainer { method })
    }

    #[cfg(not(feature = "alloc"))]
    pub(crate) fn no_alloc(method: &'static str) -> Self {
        Error(ErrorKind::NoAlloc { method })
    }
}

#[cfg(feature = "std")]
mod std_support {
    use super::*;

    use crate::std::error;

    impl error::Error for Error {}
}