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
//! User struct and associated methods.

use crate::prelude::*;
use miniserde::{Deserialize, Serialize};

#[non_exhaustive]
#[derive(Debug, Serialize, Deserialize)]
/// Struct representing a User.
pub struct User {
    /// Username of the user, unique and case-sensitive.
    pub id: String,
    /// User creation time as a Unix timestamp.
    pub created: u64,
    /// The total karma of the user.
    pub karma: u64,
    /// Optional description of the user.
    pub about: String,
    pub(crate) submitted: Vec<u64>,
}

impl User {
    /// Returns a list of [Comments](Comment) this user has posted.
    pub fn comments(&self, client: &Client) -> Result<Vec<Comment>, HError> {
        let mut comments = vec![];
        for x in self.submitted.iter() {
            let comment = client.get_comment(*x)?;
            comments.push(comment);
        }
        Ok(comments)
    }

    /// Returns a list of [Polls](Poll) this user has posted.
    pub fn polls(&self, client: &Client) -> Result<Vec<Poll>, HError> {
        let mut polls = vec![];
        for x in self.submitted.iter() {
            let poll = client.get_poll(*x)?;
            polls.push(poll);
        }
        Ok(polls)
    }

    /// Returns a list of [Stories](Story) this user has posted.
    pub fn stories(&self, client: &Client) -> Result<Vec<Story>, HError> {
        let mut stories = vec![];
        for x in self.submitted.iter() {
            let story = client.get_story(*x)?;
            stories.push(story);
        }
        Ok(stories)
    }
}