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
use std::fmt::Display;
use quick_xml::{de::from_str, DeError};
use crate::blog::Blog;
use self::{atom::AtomFeed, rss::RssFeed, traits::WebFeed};
pub mod atom;
pub mod rss;
mod traits;
#[derive(Debug, Eq, PartialEq, Hash)]
pub enum ParserError {
Parse(String),
Date(DateError),
}
#[derive(Debug, Eq, PartialEq, Hash)]
pub enum DateError {
Generic(String),
TimeZoneError(String),
Empty,
}
impl ParserError {
const fn generic_date_error(msg: String) -> Self {
Self::Date(DateError::Generic(msg))
}
const fn timezone_date_error(msg: String) -> Self {
Self::Date(DateError::TimeZoneError(msg))
}
const fn empty_date_error() -> Self {
Self::Date(DateError::Empty)
}
}
pub fn parse_web_feed(xml: &str) -> Result<Blog, ParserError> {
from_str::<RssFeed>(xml).into_blog().or_else(|e1| {
from_str::<AtomFeed>(xml)
.into_blog()
.map_err(|e2| ParserError::Parse(format!("{}\n{}", e1, e2)))
})
}
impl From<DeError> for ParserError {
fn from(e: DeError) -> Self {
Self::Parse(e.to_string())
}
}
impl Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Parse(e) => write!(f, "Parse error: {}", e),
Self::Date(e) => write!(f, "{}", e),
}
}
}
impl Display for DateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Generic(e) => write!(f, "Date error: {}", e),
Self::TimeZoneError(e) => write!(f, "Timezone error: {}", e),
Self::Empty => write!(f, "Date was empty"),
}
}
}