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
use std::{error::Error, fmt, io, string};
use xml::writer;

// Errors
#[derive(Debug)]
pub enum TwimlErr {
    Io(io::Error),
    Utf8Err(string::FromUtf8Error),
    EmitterErr(writer::Error),
}

pub use super::TwimlErr::*;

pub type TwimlResult<T> = Result<T, TwimlErr>;

impl Error for TwimlErr {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            Io(ref e) => e.source(),
            Utf8Err(ref e) => e.source(),
            EmitterErr(ref e) => e.source(),
        }
    }
}

impl fmt::Display for TwimlErr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Io(ref e) => write!(f, "IO Error: {}", e),
            Utf8Err(ref e) => write!(f, "Error converting to utf-8 string: {}", e),
            EmitterErr(ref e) => write!(f, "Error emitting xml: {}", e),
        }
    }
}

impl From<io::Error> for TwimlErr {
    fn from(e: io::Error) -> Self {
        Io(e)
    }
}

impl From<string::FromUtf8Error> for TwimlErr {
    fn from(e: string::FromUtf8Error) -> Self {
        Utf8Err(e)
    }
}

impl From<writer::Error> for TwimlErr {
    fn from(e: writer::Error) -> Self {
        EmitterErr(e)
    }
}