1
use serde::{Deserialize, Serialize};
2

            
3
use crate::{
4
    commands::{CommandResponse, ResponseParserError, single_optional_item_command_request},
5
    response_tokenizer::ResponseAttributes,
6
    types::{DbSelectionPrintResponse, Uri},
7
};
8

            
9
single_optional_item_command_request!(LsInfo, "lsinfo", Uri);
10

            
11
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12
pub struct LsInfoResponse(Vec<DbSelectionPrintResponse>);
13

            
14
impl LsInfoResponse {
15
    pub fn new(items: Vec<DbSelectionPrintResponse>) -> Self {
16
        LsInfoResponse(items)
17
    }
18
}
19

            
20
impl CommandResponse for LsInfoResponse {
21
    type Request = LsInfoRequest;
22

            
23
1
    fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
24
1
        let result = DbSelectionPrintResponse::parse(parts)?;
25

            
26
1
        Ok(LsInfoResponse(result))
27
1
    }
28

            
29
    fn serialize(&self) -> Vec<u8> {
30
        unimplemented!("response serialization is not yet implemented for LsInfoResponse")
31
    }
32
}
33

            
34
#[cfg(test)]
35
mod tests {
36
    use std::path::PathBuf;
37

            
38
    use crate::types::{DbDirectoryInfo, DbPlaylistInfo};
39

            
40
    use super::*;
41

            
42
    use indoc::indoc;
43

            
44
    use pretty_assertions::assert_eq;
45

            
46
    #[test]
47
1
    fn test_parse_response() {
48
1
        let response = indoc! {"
49
1
          directory: albums
50
1
          Last-Modified: 2024-10-01T10:00:00Z
51
1
          directory: albums/a
52
1
          Last-Modified: 2024-10-01T10:00:00Z
53
1
          playlist: albums/a/album a.m3u8
54
1
          Last-Modified: 2022-12-31T09:00:00Z
55
1
          directory: albums/b
56
1
          Last-Modified: 2023-10-01T10:00:00Z
57
1
          playlist: albums/b/album b.m3u8
58
1
          Last-Modified: 2021-12-31T09:00:00Z
59
1
          OK
60
1
      "};
61

            
62
1
        let result = LsInfoResponse::parse_raw(response.as_bytes());
63

            
64
1
        assert_eq!(
65
            result,
66
1
            Ok(LsInfoResponse(vec![
67
1
                DbSelectionPrintResponse::Directory(DbDirectoryInfo {
68
1
                    directory: PathBuf::from("albums"),
69
1
                    last_modified: Some("2024-10-01T10:00:00Z".to_string())
70
1
                }),
71
1
                DbSelectionPrintResponse::Directory(DbDirectoryInfo {
72
1
                    directory: PathBuf::from("albums/a"),
73
1
                    last_modified: Some("2024-10-01T10:00:00Z".to_string())
74
1
                }),
75
1
                DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
76
1
                    playlist: PathBuf::from("albums/a/album a.m3u8"),
77
1
                    last_modified: Some("2022-12-31T09:00:00Z".to_string())
78
1
                }),
79
1
                DbSelectionPrintResponse::Directory(DbDirectoryInfo {
80
1
                    directory: PathBuf::from("albums/b"),
81
1
                    last_modified: Some("2023-10-01T10:00:00Z".to_string())
82
1
                }),
83
1
                DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
84
1
                    playlist: PathBuf::from("albums/b/album b.m3u8"),
85
1
                    last_modified: Some("2021-12-31T09:00:00Z".to_string())
86
1
                })
87
1
            ])),
88
        );
89
1
    }
90
}