11use atom_syndication as atom;
22use rss;
33
4+ use std:: str:: FromStr ;
45use chrono:: { DateTime , UTC } ;
56use category:: Category ;
67use link:: Link ;
78use person:: Person ;
9+ use guid:: Guid ;
810
911enum EntryData {
1012 Atom ( atom:: Entry ) ,
11- RSS ( rss:: Item ) ,
13+ Rss ( rss:: Item ) ,
1214}
1315
1416pub struct Entry {
1517 // If created from an Atom or RSS entry, this is the original contents
1618 source_data : Option < EntryData > ,
1719
1820 // `id` in Atom (required), and `guid` in RSS
19- pub id : Option < String > ,
21+ pub id : Option < Guid > ,
2022 // `title` in Atom and RSS, optional only in RSS
2123 pub title : Option < String > ,
2224 // `updated` in Atom (required), not present in RSS
@@ -40,13 +42,15 @@ pub struct Entry {
4042 pub authors : Vec < Person > ,
4143 // `contributors` in Atom, not present in RSS (produces an empty Vec)
4244 pub contributors : Vec < Person > ,
45+ // not present in Atom (produces None), `comments` in RSS
46+ pub comments : Option < String > ,
4347}
4448
4549impl From < atom:: Entry > for Entry {
4650 fn from ( entry : atom:: Entry ) -> Self {
4751 Entry {
4852 source_data : Some ( EntryData :: Atom ( entry. clone ( ) ) ) ,
49- id : Some ( entry. id ) ,
53+ id : Some ( Guid :: from_id ( entry. id ) ) ,
5054 title : Some ( entry. title ) ,
5155 updated : DateTime :: parse_from_rfc3339 ( entry. updated . as_str ( ) )
5256 . map ( |date| date. with_timezone ( & UTC ) )
@@ -60,6 +64,7 @@ impl From<atom::Entry> for Entry {
6064 categories : entry. categories . into_iter ( ) . map ( |category| category. into ( ) ) . collect ( ) ,
6165 authors : entry. authors . into_iter ( ) . map ( |person| person. into ( ) ) . collect ( ) ,
6266 contributors : entry. contributors . into_iter ( ) . map ( |person| person. into ( ) ) . collect ( ) ,
67+ comments : None ,
6368 }
6469 }
6570}
@@ -71,10 +76,11 @@ impl From<Entry> for atom::Entry {
7176 } else {
7277 atom:: Entry {
7378 // TODO: How should we handle a missing id?
74- id : entry. id . unwrap_or_else ( || String :: from ( "" ) ) ,
79+ id : entry. id . unwrap_or_else ( || Guid :: from_id ( "" . into ( ) ) ) . id ,
7580 title : entry. title . unwrap_or_else ( || String :: from ( "" ) ) ,
7681 updated : entry. updated . to_rfc3339 ( ) ,
7782 published : entry. published . map ( |date| date. to_rfc3339 ( ) ) ,
83+ // TODO: Figure out this thing...
7884 source : None ,
7985 summary : entry. summary ,
8086 content : entry. content ,
@@ -86,3 +92,43 @@ impl From<Entry> for atom::Entry {
8692 }
8793 }
8894}
95+
96+ impl From < rss:: Item > for Entry {
97+ fn from ( entry : rss:: Item ) -> Self {
98+ let entry_clone = entry. clone ( ) ;
99+ let date = entry. pub_date . and_then ( |d| DateTime :: from_str ( & d[ ..] ) . ok ( ) ) ;
100+ Entry {
101+ source_data : Some ( EntryData :: Rss ( entry_clone) ) ,
102+ id : entry. guid . map ( |id| id. into ( ) ) ,
103+ title : entry. title ,
104+ updated : date. clone ( ) . unwrap_or_else ( UTC :: now) ,
105+ published : date,
106+ summary : None ,
107+ content : entry. description ,
108+ links : entry. link . into_iter ( ) . map ( Link :: from_href) . collect ( ) ,
109+ categories : entry. categories . into_iter ( ) . map ( |category| category. into ( ) ) . collect ( ) ,
110+ authors : entry. author . into_iter ( ) . map ( Person :: from_name) . collect ( ) ,
111+ contributors : vec ! [ ] ,
112+ comments : entry. comments ,
113+ }
114+ }
115+ }
116+
117+ impl From < Entry > for rss:: Item {
118+ fn from ( entry : Entry ) -> rss:: Item {
119+ if let Some ( EntryData :: Rss ( entry) ) = entry. source_data {
120+ entry
121+ } else {
122+ rss:: Item {
123+ guid : entry. id . map ( |id| id. into ( ) ) ,
124+ title : entry. title ,
125+ author : entry. authors . into_iter ( ) . next ( ) . map ( |person| person. name ) ,
126+ pub_date : entry. published . map ( |date| date. to_rfc2822 ( ) ) ,
127+ description : entry. content ,
128+ link : entry. links . into_iter ( ) . next ( ) . map ( |link| link. href ) ,
129+ categories : entry. categories . into_iter ( ) . map ( |category| category. into ( ) ) . collect ( ) ,
130+ comments : entry. comments ,
131+ }
132+ }
133+ }
134+ }
0 commit comments