1
use std::collections::HashMap;
2

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

            
5
use crate::{
6
    commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
7
    response_tokenizer::{ResponseAttributes, get_and_parse_property, get_property},
8
};
9

            
10
pub struct Config;
11

            
12
empty_command_request!(Config, "config");
13

            
14
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15
pub struct ConfigResponse {
16
    pub music_directory: String,
17
    pub playlist_directory: String,
18
    pub pcre: bool,
19
}
20

            
21
impl ConfigResponse {
22
    pub fn new(music_directory: String, playlist_directory: String, pcre: bool) -> Self {
23
        ConfigResponse {
24
            music_directory,
25
            playlist_directory,
26
            pcre,
27
        }
28
    }
29
}
30

            
31
impl CommandResponse for ConfigResponse {
32
    fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
33
        let parts: HashMap<_, _> = parts.into_map()?;
34

            
35
        let music_directory = get_property!(parts, "music_directory", Text).to_string();
36
        let playlist_directory = get_property!(parts, "playlist_directory", Text).to_string();
37

            
38
        // TODO: is this parsed correctly?
39
        let pcre = get_and_parse_property!(parts, "pcre", Text);
40

            
41
        Ok(ConfigResponse {
42
            music_directory,
43
            playlist_directory,
44
            pcre,
45
        })
46
    }
47
}
48

            
49
impl Command for Config {
50
    type Request = ConfigRequest;
51
    type Response = ConfigResponse;
52
}