1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Style {
    #[serde(rename = "Name")]
    pub name: String,
    #[serde(rename = "Content")]
    pub content: String,
    #[serde(rename = "Category")]
    pub category: Option<String>,
    #[serde(
        rename = "CreateTime",
        skip_serializing_if = "Option::is_none",
    )]
    pub create_time: Option<String>,
    #[serde(
        rename = "LastModifyTime",
        skip_serializing_if = "Option::is_none",
    )]
    pub last_modify_time: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct StyleList {
    #[serde(rename = "Style")]
    pub style: Option<Vec<Style>>,
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn style_1() {
        let xml_content = r#"<Style>
  <Name>imagestyle</Name>
  <Content>image/resize,p_50</Content>
  <Category>image</Category>
  <CreateTime>Wed, 20 May 2020 12:07:15 GMT</CreateTime>
  <LastModifyTime>Wed, 20 May 2020 12:07:15 GMT</LastModifyTime>
</Style>"#;

        let style: Style = quick_xml::de::from_str(&xml_content).unwrap();
        assert_eq!(style.category, Some("image".to_string()));
    }

    #[test]
    fn style_2() {
        let xml_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<StyleList>
    <Style>
        <Name>imagestyle</Name>
        <Content>image/resize,p_50</Content>
        <Category>image</Category>
        <CreateTime>Wed, 20 May 2020 12:07:15 GMT</CreateTime>
        <LastModifyTime>Wed, 20 May 2020 12:07:15 GMT</LastModifyTime>
    </Style>
    <Style>
        <Name>imagestyle1</Name>
        <Content>image/resize,w_200</Content>
        <Category>image</Category>
        <CreateTime>Wed, 20 May 2020 12:08:04 GMT</CreateTime>
        <LastModifyTime>Wed, 20 May 2020 12:07:15 GMT</LastModifyTime>
    </Style>
    <Style>
        <Name>imagestyle2</Name>
        <Content>image/resize,w_300</Content>
        <Category>image</Category>
        <CreateTime>Fri, 12 Mar 2021 06:19:13 GMT</CreateTime>
        <LastModifyTime>Wed, 20 May 2020 12:07:15 GMT</LastModifyTime>
    </Style>
</StyleList>"#;

        let style_list: StyleList = quick_xml::de::from_str(&xml_content).unwrap();
        let left = "image/resize,p_50";
        let right = &style_list.style.unwrap()[0].content;
        assert_eq!(left, right);
    }

    #[test]
    fn style_3() {
        let style = Style {
            name: "imagestyle".to_string(),
            content: "image/resize,p_50".to_string(),
            category: Some("image".to_string()),
            create_time: None,
            last_modify_time: None,
        };

        let left = quick_xml::se::to_string(&style).unwrap();
        let right = r#"<Style><Name>imagestyle</Name><Content>image/resize,p_50</Content><Category>image</Category></Style>"#;
        assert_eq!(left, right);
    }
}