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
use core::fmt;
use std::fmt::{Display, Formatter};

pub use serde::Serialize;

///A simple object to let you set informationons about the listing you are getting
#[derive(Clone, Debug, Serialize)]
pub struct FeedOption {
    pub after: Option<String>,
    pub before: Option<String>,
    pub count: Option<u32>,
    pub period: Option<TimePeriod>,
}

impl FeedOption {
    ///Returns the URL extension for the request
    pub fn url(&self) -> String {
        let mut url = String::new();
        if let Some(after) = &self.after {
            url.push_str(&mut format!("&after={}", after));
        }
        if let Some(before) = &self.before {
            url.push_str(&mut format!("&before={}", before));
        }

        if let Some(count) = &self.count {
            url.push_str(&mut format!("&count={}", count));
        }

        if let Some(period) = &self.period {
            url.push_str(&mut format!("&t={}", period.get_string()));
        }
        return url;
    }
    pub fn extend(&self, value: &mut String) {
        value.push_str("?");
        value.push_str(self.url().as_str());
    }
}

///Time Period for the request
#[derive(Copy, Clone, Debug, Serialize)]
pub enum TimePeriod {
    Now,
    Today,
    Week,
    Month,
    Year,
    AllTime,
}

impl TimePeriod {
    /// Gets the string for Reddit
    pub fn get_string(&self) -> &str {
        match self {
            TimePeriod::Now => "now",
            TimePeriod::Today => "day",
            TimePeriod::Week => "week",
            TimePeriod::Month => "month",
            TimePeriod::Year => "year",
            TimePeriod::AllTime => "all",
        }
    }
}

/// FriendType
pub enum FriendType {
    /// Contributor
    Contributor,
    /// Moderator
    Moderator,
    /// This exist if the reddit api changes in the future or I am missing features
    Custom(String),
}

impl Display for FriendType {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let string = match self {
            FriendType::Contributor => { "contributor" }
            FriendType::Moderator => { "moderator" }
            FriendType::Custom(str) => { str.as_str() }
        };
        write!(f, "{}", string)
    }
}