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
use std::str::FromStr;

#[derive(Clone)]
pub enum Feed {
    Atom(atom_syndication::Feed),
    RSS(rss::Channel),
}

impl FromStr for Feed {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match atom_syndication::Feed::from_str(s) {
            Ok(feed) => Ok(Feed::Atom(feed)),
            _ => match rss::Channel::from_str(s) {
                Ok(feed) => Ok(Feed::RSS(feed)),
                _ => Err("Could not parse XML as Atom or RSS from input"),
            },
        }
    }
}

impl ToString for Feed {
    fn to_string(&self) -> String {
        match self {
            &Feed::Atom(ref atom_feed) => atom_feed.to_string(),
            &Feed::RSS(ref rss_channel) => rss_channel.to_string(),
        }
    }
}

#[cfg(test)]
mod test {
    use std::fs::File;
    use std::io::Read;
    use std::str::FromStr;

    use super::Feed;

    // Source: https://github.com/vtduncan/rust-atom/blob/master/src/lib.rs
    #[test]
    fn test_from_atom_file() {
        let mut file = File::open("test-data/atom.xml").unwrap();
        let mut atom_string = String::new();
        file.read_to_string(&mut atom_string).unwrap();
        let feed = Feed::from_str(&atom_string).unwrap();
        assert!(feed.to_string().len() > 0);
    }

    // Source: https://github.com/frewsxcv/rust-rss/blob/master/src/lib.rs
    #[test]
    fn test_from_rss_file() {
        let mut file = File::open("test-data/rss.xml").unwrap();
        let mut rss_string = String::new();
        file.read_to_string(&mut rss_string).unwrap();
        let rss = Feed::from_str(&rss_string).unwrap();
        assert!(rss.to_string().len() > 0);
    }

    // Source: https://github.com/vtduncan/rust-atom/blob/master/src/lib.rs
    #[test]
    fn test_atom_to_string() {
        let author = atom_syndication::PersonBuilder::default()
            .name("N. Blogger")
            .build()
            .unwrap();

        let entry = atom_syndication::EntryBuilder::default()
            .title("My first post!")
            .content(
                atom_syndication::ContentBuilder::default()
                    .value("This is my first post".to_string())
                    .build()
                    .unwrap(),
            )
            .build()
            .unwrap();

        let feed = atom_syndication::FeedBuilder::default()
            .title("My Blog")
            .authors(vec![author])
            .entries(vec![entry])
            .build()
            .unwrap();

        assert_eq!(feed.to_string(), "<feed xmlns=\"http://www.w3.org/2005/Atom\"><title>My Blog</title><id></id><updated></updated><author><name>N. Blogger</name></author><entry><title>My first post!</title><id></id><updated></updated><content>This is my first post</content></entry></feed>");
    }

    // Source: https://github.com/frewsxcv/rust-rss/blob/master/src/lib.rs
    #[test]
    fn test_rss_to_string() {
        let item = rss::ItemBuilder::default()
            .title("My first post!".to_string())
            .link("http://myblog.com/post1".to_string())
            .description("This is my first post".to_string())
            .build()
            .unwrap();

        let channel = rss::ChannelBuilder::default()
            .title("My Blog")
            .link("http://myblog.com")
            .description("Where I write stuff")
            .items(vec![item])
            .build()
            .unwrap();

        let rss = Feed::RSS(channel);
        assert_eq!(rss.to_string(), "<rss version=\"2.0\"><channel><title>My Blog</title><link>http://myblog.com</link><description>Where I write stuff</description><item><title>My first post!</title><link>http://myblog.com/post1</link><description><![CDATA[This is my first post]]></description></item></channel></rss>");
    }
}