1
use std::collections::HashMap;
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
use crate::commands::{
6
    get_and_parse_optional_property, get_and_parse_property, Command, Request, RequestParserResult,
7
    ResponseAttributes, ResponseParserError,
8
};
9

            
10
pub struct Stats;
11

            
12
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13
pub struct StatsResponse {
14
    pub uptime: u64,
15
    pub playtime: u64,
16
    pub artists: Option<u64>,
17
    pub albums: Option<u64>,
18
    pub songs: Option<u64>,
19
    pub db_playtime: Option<u64>,
20
    pub db_update: Option<u64>,
21
}
22

            
23
impl Command for Stats {
24
    type Response = StatsResponse;
25
    const COMMAND: &'static str = "stats";
26

            
27
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
28
        debug_assert!(parts.next().is_none());
29

            
30
        Ok((Request::Stats, ""))
31
    }
32

            
33
    fn parse_response(
34
        parts: ResponseAttributes<'_>,
35
    ) -> Result<Self::Response, ResponseParserError> {
36
        let parts: HashMap<_, _> = parts.into();
37

            
38
        let uptime = get_and_parse_property!(parts, "uptime", Text);
39
        let playtime = get_and_parse_property!(parts, "playtime", Text);
40
        let artists = get_and_parse_optional_property!(parts, "artists", Text);
41
        let albums = get_and_parse_optional_property!(parts, "albums", Text);
42
        let songs = get_and_parse_optional_property!(parts, "songs", Text);
43
        let db_playtime = get_and_parse_optional_property!(parts, "db_playtime", Text);
44
        let db_update = get_and_parse_optional_property!(parts, "db_update", Text);
45

            
46
        Ok(StatsResponse {
47
            uptime,
48
            playtime,
49
            artists,
50
            albums,
51
            songs,
52
            db_playtime,
53
            db_update,
54
        })
55
    }
56
}