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
use num_format::ToFormattedString;
use serde::Deserialize;

use crate::utils;

#[derive(Deserialize)]
pub struct UserResponse {
    pub user: User,
}

#[derive(Deserialize)]
pub struct Registered {
    pub unixtime: String,
}

/// Response to a `user.getInfo` request
///
/// Returns information about a user's profile. Certain fields, such as `bootstrap` and `images`,
/// have been omitted from deserialization since they are not used in this application
#[derive(Deserialize)]
pub struct User {
    /// The total number of playlists the user has created
    playlists: String,

    /// The total number of tracks scrobbled by the user
    #[serde(rename = "playcount")]
    play_count: String,

    /// The user's gender
    pub gender: String,

    /// The user's username
    pub name: String,

    /// Indicates if the user is a subscriber to Last.fm
    pub subscriber: String,

    /// The user's profile URL
    pub url: String,

    /// The user's country
    pub country: String,

    /// The date and time the user registered their profile, represented as a unix timestamp
    ///
    /// See [Registered](struct.Registered.html)
    pub registered: Registered,

    /// The user's profile type. Could be a normal user or a staff user.
    #[serde(rename = "type")]
    pub user_type: String,

    /// The user's age
    pub age: String,
    // pub bootstrap: String,
    /// The user's real name, if provided
    #[serde(rename = "realname")]
    pub real_name: String,
}

impl User {
    /// Get the number of playlists created by the user
    pub fn playlists(&self) -> i32 {
        self.playlists.parse().unwrap()
    }

    /// Get the number of playlists created by the user, formatted according to the user's system locale
    pub fn playlists_formatted(&self) -> String {
        self.playlists().to_formatted_string(&utils::get_locale())
    }

    /// Get the total number of scrobbles by the user
    pub fn play_count(&self) -> i32 {
        self.play_count.parse().unwrap()
    }

    pub fn play_count_formatted(&self) -> String {
        let locale = utils::get_locale();
        self.play_count().to_formatted_string(&locale)
    }
}