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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
extern crate serde_json;
use crate::client::Client;
use crate::util::{FeedOption, RouxError};
use crate::models::{About, Comments, Overview, Submissions};
pub struct User {
    pub user: String,
    client: Client,
}
impl User {
    pub fn new(user: &str) -> User {
        User {
            user: user.to_owned(),
            client: Client::new(),
        }
    }
    #[maybe_async::maybe_async]
    pub async fn overview(&self, options: Option<FeedOption>) -> Result<Overview, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/overview/.json", self.user);
        if let Some(options) = options {
            options.build_url(url);
        }
        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<Overview>()
            .await?)
    }
    #[maybe_async::maybe_async]
    pub async fn submitted(&self, options: Option<FeedOption>) -> Result<Submissions, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/submitted/.json", self.user);
        if let Some(options) = options {
            options.build_url(url);
        }
        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<Submissions>()
            .await?)
    }
    #[maybe_async::maybe_async]
    pub async fn comments(&self, options: Option<FeedOption>) -> Result<Comments, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/comments/.json", self.user);
        if let Some(options) = options {
            options.build_url(url);
        }
        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<Comments>()
            .await?)
    }
    #[maybe_async::maybe_async]
    pub async fn about(&self, options: Option<FeedOption>) -> Result<About, RouxError> {
        let url = &mut format!("https://www.reddit.com/user/{}/about/.json", self.user);
        if let Some(options) = options {
            options.build_url(url);
        }
        Ok(self
            .client
            .get(&url.to_owned())
            .send()
            .await?
            .json::<About>()
            .await?)
    }
}
#[cfg(test)]
mod tests {
    use super::User;
    use crate::util::FeedOption;
    #[maybe_async::test(feature = "blocking", async(not(feature = "blocking"), tokio::test))]
    async fn test_no_auth() {
        let user = User::new("beneater");
        let overview = user.overview(None).await;
        assert!(overview.is_ok());
        let submitted = user.submitted(None).await;
        assert!(submitted.is_ok());
        let comments = user.comments(None).await;
        assert!(comments.is_ok());
        let about = user.about(None).await;
        assert!(about.is_ok());
        let after = comments.unwrap().data.after.unwrap();
        let after_options = FeedOption::new().after(&after);
        let next_comments = user.comments(Some(after_options)).await;
        assert!(next_comments.is_ok());
    }
}