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,
})
}
}