1
use std::collections::HashMap;
2

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

            
5
use crate::{
6
    commands::{CommandResponse, ResponseParserError, empty_command_request},
7
    response_tokenizer::{
8
        ResponseAttributes, get_and_parse_optional_property, get_and_parse_property,
9
    },
10
};
11

            
12
empty_command_request!(Stats, "stats");
13

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

            
25
impl StatsResponse {
26
    pub fn new(
27
        uptime: u64,
28
        playtime: u64,
29
        artists: Option<u64>,
30
        albums: Option<u64>,
31
        songs: Option<u64>,
32
        db_playtime: Option<u64>,
33
        db_update: Option<u64>,
34
    ) -> Self {
35
        Self {
36
            uptime,
37
            playtime,
38
            artists,
39
            albums,
40
            songs,
41
            db_playtime,
42
            db_update,
43
        }
44
    }
45
}
46

            
47
impl CommandResponse for StatsResponse {
48
    type Request = StatsRequest;
49

            
50
    fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
51
        let parts: HashMap<_, _> = parts.into_map()?;
52

            
53
        let uptime = get_and_parse_property!(parts, "uptime", Text);
54
        let playtime = get_and_parse_property!(parts, "playtime", Text);
55
        let artists = get_and_parse_optional_property!(parts, "artists", Text);
56
        let albums = get_and_parse_optional_property!(parts, "albums", Text);
57
        let songs = get_and_parse_optional_property!(parts, "songs", Text);
58
        let db_playtime = get_and_parse_optional_property!(parts, "db_playtime", Text);
59
        let db_update = get_and_parse_optional_property!(parts, "db_update", Text);
60

            
61
        Ok(StatsResponse {
62
            uptime,
63
            playtime,
64
            artists,
65
            albums,
66
            songs,
67
            db_playtime,
68
            db_update,
69
        })
70
    }
71

            
72
    fn serialize(&self) -> Vec<u8> {
73
        unimplemented!("response serialization is not yet implemented for StatsResponse")
74
    }
75
}