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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Support for creating Atom feeds from a list of posts.

use crate::config::Author;
use crate::post::Post;
use atom_syndication::{Entry, Error as AtomError, Feed, Link, Person, Text};
use chrono::{
    FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, ParseError, ParseResult,
    TimeZone, Utc,
};
use std::fmt;
use std::io::Write;
use url::Url;

/// Bundled configuration for creating a feed.
pub struct FeedConfig {
    pub title: String,
    pub id: String,
    pub author: Option<Author>,
    pub home_page: Url,
}

/// Creates a feed from some configuration ([`FeedConfig`]) and a list of
/// [`Post`]s and writes the result to a [`std::io::Write`]. This function
/// takes ownership of the provided [`FeedConfig`].
pub fn write_feed<W: Write>(
    config: FeedConfig,
    posts: &[Post],
    w: W,
) -> Result<()> {
    feed(config, posts)?.write_to(w)?;
    Ok(())
}

fn feed(config: FeedConfig, posts: &[Post]) -> ParseResult<Feed> {
    use std::collections::BTreeMap;
    Ok(Feed {
        entries: feed_entries(&config, posts)?,
        title: Text::plain(config.title),
        id: config.id,
        updated: FixedOffset::east(0)
            .from_utc_datetime(&Utc::now().naive_utc()),
        authors: author_to_people(config.author),
        categories: Vec::new(),
        contributors: Vec::new(),
        generator: None,
        icon: None,
        logo: None,
        rights: None,
        subtitle: None,
        extensions: BTreeMap::new(),
        namespaces: BTreeMap::new(),
        links: vec![Link {
            href: config.home_page.into(),
            rel: "alternate".to_string(),
            title: None,
            hreflang: None,
            mime_type: None,
            length: None,
        }],
        base: None,
        lang: None,
    })
}

fn feed_entries(
    config: &FeedConfig,
    posts: &[Post],
) -> ParseResult<Vec<Entry>> {
    use std::collections::BTreeMap;
    let mut entries: Vec<Entry> = Vec::with_capacity(posts.len());

    for post in posts {
        let (summary, _) = post.summary();

        // Good grief, `chrono` is ridiculous. If we try to skip this ceremony
        // and just do FixedOffset::parse_from_str(), we will get a runtime
        // error because we don't have fully-precise time information or a
        // timezone. Below I'm intending to use the UTC timezone. I think
        // that's what `FixedOffset::east(0)` does, but it's hard to
        // say because chrono is so complicated and the documentation
        // doesn't provide enough context.
        let naive_date = NaiveDate::parse_from_str(&post.date, "%Y-%m-%d")?;
        let naive_time = NaiveTime::from_hms(0, 0, 0);
        let naive_date_time = NaiveDateTime::new(naive_date, naive_time);
        let offset = FixedOffset::east(0);
        let date = offset.from_utc_datetime(&naive_date_time);

        entries.push(Entry {
            id: post.url.to_string(),
            title: Text::plain(post.title.clone()),
            updated: date,
            authors: author_to_people(config.author.clone()),
            links: vec![Link {
                href: post.url.to_string(),
                rel: "alternate".to_owned(),
                title: None,
                mime_type: None,
                hreflang: None,
                length: None,
            }],
            rights: None,
            summary: Some(Text::html(summary.to_owned())),
            categories: Vec::new(),
            contributors: Vec::new(),
            published: Some(date),
            source: None,
            content: None,
            extensions: BTreeMap::new(),
        })
    }
    Ok(entries)
}

fn author_to_people(author: Option<Author>) -> Vec<Person> {
    match author {
        Some(author) => vec![Person {
            name: author.name,
            email: author.email,
            uri: None,
        }],
        None => Vec::new(),
    }
}

type Result<T> = std::result::Result<T, Error>;

/// Represents a problem creating a feed. Variants inlude I/O, Atom, and
/// date-time parsing issues.
#[derive(Debug)]
pub enum Error {
    /// Returned when there is a generic I/O error.
    Io(std::io::Error),

    /// Returned when there is an Atom-related error.
    Atom(AtomError),

    /// Returned when there is an issue parsing a post's date.
    DateTimeParse(ParseError),
}

impl fmt::Display for Error {
    /// Implements [`fmt::Display`] for [`Error`].
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::Io(err) => err.fmt(f),
            Error::Atom(err) => err.fmt(f),
            Error::DateTimeParse(err) => err.fmt(f),
        }
    }
}

impl std::error::Error for Error {
    /// Implements [`std::error::Error`] for [`Error`].
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Io(err) => Some(err),
            Error::Atom(err) => Some(err),
            Error::DateTimeParse(err) => Some(err),
        }
    }
}

impl From<std::io::Error> for Error {
    /// Converts [`std::io::Error`]s into [`Error`]. This allows us to use the
    /// `?` operator in fallible feed operations.
    fn from(err: std::io::Error) -> Error {
        Error::Io(err)
    }
}

impl From<AtomError> for Error {
    /// Converts [`AtomError`]s into [`Error`]. This allows us to use the `?`
    /// operator in fallible feed operations.
    fn from(err: AtomError) -> Error {
        Error::Atom(err)
    }
}

impl From<ParseError> for Error {
    /// Converts [`ParseError`]s into [`Error`]. This allows us to use the `?`
    /// operator in fallible feed operations.
    fn from(err: ParseError) -> Error {
        Error::DateTimeParse(err)
    }
}