rss2email_lib/xml/
error.rs

1use std::fmt::Display;
2
3use quick_xml::DeError;
4
5/// Represents possible issues that may arise when trying to parse web feeds.
6/// If this occurs then a web feed is considered invalid.
7#[derive(Debug, Eq, PartialEq, Hash)]
8pub enum ParserError {
9  /// Generic parsing error.
10  Parse(String),
11  /// Date format error.
12  Date(DateError),
13}
14
15/// Represents different types of Date errors.
16#[derive(Debug, Eq, PartialEq, Hash)]
17pub enum DateError {
18  Generic(String),
19  TimeZoneError(String),
20  Empty,
21}
22
23impl ParserError {
24  /// Less verbose way of instantiating a [`DateError::Generic`].
25  pub const fn generic_date_error(msg: String) -> Self {
26    Self::Date(DateError::Generic(msg))
27  }
28
29  /// Less verbose way of instantiating a [`DateError::TimeZoneError`].
30  pub const fn timezone_date_error(msg: String) -> Self {
31    Self::Date(DateError::TimeZoneError(msg))
32  }
33
34  /// Less verbose way of instantiating a [`DateError::Empty`].
35  pub const fn empty_date_error() -> Self {
36    Self::Date(DateError::Empty)
37  }
38}
39
40impl From<DeError> for ParserError {
41  fn from(e: DeError) -> Self {
42    Self::Parse(e.to_string())
43  }
44}
45
46impl Display for ParserError {
47  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48    match self {
49      Self::Parse(e) => write!(f, "Parse error: {e}"),
50      Self::Date(e) => write!(f, "{e}"),
51    }
52  }
53}
54
55impl Display for DateError {
56  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57    match self {
58      Self::Generic(e) => write!(f, "Date error: {e}"),
59      Self::TimeZoneError(e) => write!(f, "Timezone error: {e}"),
60      Self::Empty => write!(f, "Date was empty"),
61    }
62  }
63}