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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::{error::Error as StdError, fmt};

use quick_xml::Error as XmlError;
use serde_json::error::Error as JsonError;
use std::{str::Utf8Error, string::FromUtf8Error};

#[derive(Clone, Debug, PartialEq)]
pub enum ErrorKind {
  Syntax,
  Encoding,
  Unknown
}

impl ErrorKind {
  pub fn to_str(&self) -> &'static str {
    match *self {
      ErrorKind::Syntax => "parse",
      ErrorKind::Encoding => "encoding",
      ErrorKind::Unknown => "unknown"
    }
  }
}

/// This type represents all possible errors that can occur when converting to or from XML and JSON
pub struct Error {
  details: String,
  desc:    &'static str,
  kind:    ErrorKind
}

impl Error {
  /// Initialize a new Error with `ErrorKind`
  pub fn new<T: Into<String>>(kind: ErrorKind, detail: T) -> Error {
    let desc = kind.to_str();
    Error {
      kind,
      desc,
      details: detail.into()
    }
  }

  /// Error kind
  pub fn kind(&self) -> ErrorKind {
    self.kind.clone()
  }

  /// Error details
  pub fn details(&self) -> String {
    self.details.clone()
  }
}

impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}: {}", self.kind.to_str(), self.details)
  }
}

impl fmt::Debug for Error {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}", self)
  }
}

impl StdError for Error {
  fn description(&self) -> &str {
    self.desc
  }
}

impl From<JsonError> for Error {
  fn from(e: JsonError) -> Self {
    Error::new(ErrorKind::Syntax, format!("{}", e))
  }
}

impl From<XmlError> for Error {
  fn from(e: XmlError) -> Self {
    Error::new(ErrorKind::Unknown, format!("{}", e))
  }
}

impl From<FromUtf8Error> for Error {
  fn from(e: FromUtf8Error) -> Self {
    Error::new(ErrorKind::Encoding, format!("{}", e))
  }
}

impl From<Utf8Error> for Error {
  fn from(e: Utf8Error) -> Self {
    Error::new(ErrorKind::Encoding, format!("{}", e))
  }
}