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
use chrono::{DateTime, FixedOffset, ParseError};
use self::super::super::Outcome;
use self::super::read_toml_file;
use std::iter::FromIterator;
use std::cmp::Ordering;
use toml::encode_str;
use std::path::Path;
use std::io::Write;
use std::fs::File;
#[derive(Debug, Clone, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub struct QueuedTweet {
pub author: String,
pub time: DateTime<FixedOffset>,
pub content: String,
pub time_posted: Option<DateTime<FixedOffset>>,
pub id: Option<i64>,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
struct QueuedTweetForSerialisation {
pub author: String,
pub time: String,
pub content: String,
pub time_posted: Option<String>,
pub id: Option<i64>,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
struct QueuedTweets {
tweet: Vec<QueuedTweetForSerialisation>,
}
impl QueuedTweet {
pub fn read(p: &Path) -> Result<Vec<QueuedTweet>, Option<Outcome>> {
let queued_tweets: QueuedTweets = try!(read_toml_file(p, "queued tweets"));
Result::from_iter(queued_tweets.tweet.into_iter().map(|qts| qts.into()).collect::<Vec<_>>()).map_err(|_| None)
}
pub fn write(queued_tweets: Vec<QueuedTweet>, p: &Path) {
File::create(p)
.unwrap()
.write_all(encode_str(&QueuedTweets { tweet: queued_tweets.into_iter().map(QueuedTweetForSerialisation::from).collect() }).as_bytes())
.unwrap();
}
}
impl Ord for QueuedTweet {
fn cmp(&self, other: &Self) -> Ordering {
self.time.cmp(&other.time)
}
}
impl PartialOrd for QueuedTweet {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.time.partial_cmp(&other.time)
}
}
impl From<QueuedTweet> for QueuedTweetForSerialisation {
fn from(qt: QueuedTweet) -> QueuedTweetForSerialisation {
QueuedTweetForSerialisation {
author: qt.author,
time: qt.time.to_rfc3339(),
content: qt.content,
time_posted: qt.time_posted.map(|dt| dt.to_rfc3339()),
id: qt.id,
}
}
}
impl Into<Result<QueuedTweet, ParseError>> for QueuedTweetForSerialisation {
fn into(self) -> Result<QueuedTweet, ParseError> {
Ok(QueuedTweet {
author: self.author,
time: try!(DateTime::parse_from_rfc3339(&self.time)),
content: self.content,
time_posted: match self.time_posted {
Some(dts) => Some(try!(DateTime::parse_from_rfc3339(&dts))),
None => None,
},
id: self.id,
})
}
}