#[derive(Clone, Debug)]
pub struct FeedOption {
pub after: Option<String>,
pub before: Option<String>,
pub limit: Option<u32>,
pub count: Option<u32>,
pub period: Option<TimePeriod>,
}
impl FeedOption {
pub fn new() -> FeedOption {
FeedOption {
after: None,
before: None,
count: None,
limit: None,
period: None,
}
}
pub fn after(mut self, ty: &str) -> FeedOption {
if self.before.is_some() {
panic!("Cannot have an after and before param at the same time");
}
self.after = Some(ty.to_owned());
self
}
pub fn before(mut self, ty: &str) -> FeedOption {
if self.after.is_some() {
panic!("Cannot have an after and before param at the same time");
}
self.before = Some(ty.to_owned());
self
}
pub fn count(mut self, ty: u32) -> FeedOption {
self.count = Some(ty);
self
}
pub fn limit(mut self, ty: u32) -> FeedOption {
self.limit = Some(ty);
self
}
pub fn period(mut self, period: TimePeriod) -> FeedOption {
self.period = Some(period);
self
}
pub fn build_url(self, url: &mut String) {
if let Some(after) = self.after {
url.push_str(&format!("&after={}", after));
} else if let Some(before) = self.before {
url.push_str(&format!("&before={}", before));
}
if let Some(count) = self.count {
url.push_str(&format!("&count={}", count));
}
if let Some(limit) = self.limit {
url.push_str(&format!("&limit={}", limit));
}
if let Some(period) = self.period {
url.push_str(&format!("&t={}", period.get_string_for_period()));
}
url.push_str(&String::from("&"));
}
}
impl Default for FeedOption {
fn default() -> Self {
Self::new()
}
}
#[derive(Copy, Clone, Debug)]
pub enum TimePeriod {
Now,
Today,
ThisWeek,
ThisMonth,
ThisYear,
AllTime,
}
impl TimePeriod {
pub fn get_string_for_period(&self) -> &str {
match self {
TimePeriod::Now => "now",
TimePeriod::Today => "day",
TimePeriod::ThisWeek => "week",
TimePeriod::ThisMonth => "month",
TimePeriod::ThisYear => "year",
TimePeriod::AllTime => "all",
}
}
}
#[cfg(test)]
mod tests {
use super::FeedOption;
#[test]
fn test_build_url_after() {
let after = "some_after";
let options = FeedOption::new().after(after);
let url = &mut String::from("");
options.build_url(url);
assert!(*url == format!("&after={}&", after))
}
#[test]
fn test_build_url_before() {
let before = "some_before";
let options = FeedOption::new().before(before);
let url = &mut String::from("");
options.build_url(url);
assert!(*url == format!("&before={}&", before))
}
#[test]
fn test_build_url_count() {
let count = 100u32;
let options = FeedOption::new().count(count);
let url = &mut String::from("");
options.build_url(url);
assert!(*url == format!("&count={}&", count))
}
}