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
    types::{DbSongInfo, Priority, SongId, SongPosition},
11
};
12

            
13
// Displays the song info of the current song (same song that is identified in status)
14
empty_command_request!(CurrentSong, "currentsong");
15

            
16
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17
pub struct CurrentSongResponse {
18
    position: SongPosition,
19
    id: SongId,
20
    priority: Option<Priority>,
21
    song_info: DbSongInfo,
22
}
23

            
24
impl CurrentSongResponse {
25
    pub fn new(
26
        position: SongPosition,
27
        id: SongId,
28
        priority: Option<Priority>,
29
        song_info: DbSongInfo,
30
    ) -> Self {
31
        Self {
32
            position,
33
            id,
34
            priority,
35
            song_info,
36
        }
37
    }
38
}
39

            
40
impl CommandResponse for CurrentSongResponse {
41
    type Request = CurrentSongRequest;
42

            
43
    fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
44
        let mut parts: HashMap<_, _> = parts.into_map()?;
45

            
46
        let position: SongPosition = get_and_parse_property!(parts, "Pos", Text);
47
        let id: SongId = get_and_parse_property!(parts, "Id", Text);
48
        let priority: Option<Priority> = get_and_parse_optional_property!(parts, "Prio", Text);
49

            
50
        parts.remove("Pos");
51
        parts.remove("Id");
52
        parts.remove("Prio");
53

            
54
        let song_info = DbSongInfo::parse_map(parts)?;
55

            
56
        Ok(CurrentSongResponse {
57
            position,
58
            id,
59
            priority,
60
            song_info,
61
        })
62
    }
63

            
64
    fn serialize(&self) -> Vec<u8> {
65
        unimplemented!("response serialization is not yet implemented for CurrentSongResponse")
66
    }
67
}