notionrs_schema/object/database/
multi_select.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq, notionrs_macro::Setter)]
4pub struct DatabaseMultiSelectProperty {
5    /// Property Identifier
6    #[serde(skip_serializing)]
7    pub id: Option<String>,
8
9    /// Modify the value of this field when updating the column name of the property.
10    #[serde(skip_serializing)]
11    pub name: String,
12
13    /// Although it is not explicitly stated in the official documentation,
14    /// you can add a description to the property by specifying this.
15    #[serde(skip_serializing)]
16    pub description: Option<String>,
17
18    pub multi_select: DatabaseMultiSelectOptionProperty,
19}
20
21#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq, notionrs_macro::Setter)]
22pub struct DatabaseMultiSelectOptionProperty {
23    pub options: Vec<crate::object::select::Select>,
24}
25
26impl DatabaseMultiSelectProperty {
27    pub fn options(mut self, options: Vec<crate::object::select::Select>) -> Self {
28        self.multi_select.options = options;
29        self
30    }
31}
32
33// # --------------------------------------------------------------------------------
34//
35// unit test
36//
37// # --------------------------------------------------------------------------------
38#[cfg(test)]
39mod unit_tests {
40
41    use super::*;
42
43    #[test]
44    fn deserialize_database_multi_select_property() {
45        let json_data = r#"
46        {
47            "id": "flsb",
48            "name": "Store availability",
49            "type": "multi_select",
50            "multi_select": {
51                "options": [
52                {
53                    "id": "5de29601-9c24-4b04-8629-0bca891c5120",
54                    "name": "Duc Loi Market",
55                    "color": "blue"
56                },
57                {
58                    "id": "385890b8-fe15-421b-b214-b02959b0f8d9",
59                    "name": "Rainbow Grocery",
60                    "color": "gray"
61                },
62                {
63                    "id": "72ac0a6c-9e00-4e8c-80c5-720e4373e0b9",
64                    "name": "Nijiya Market",
65                    "color": "purple"
66                },
67                {
68                    "id": "9556a8f7-f4b0-4e11-b277-f0af1f8c9490",
69                    "name": "Gus's Community Market",
70                    "color": "yellow"
71                }
72                ]
73            }
74        }
75        "#;
76
77        let multi_select = serde_json::from_str::<DatabaseMultiSelectProperty>(json_data).unwrap();
78
79        assert_eq!(multi_select.id, Some("flsb".to_string()));
80        assert_eq!(multi_select.name, "Store availability");
81
82        let options = &multi_select.multi_select.options;
83        assert_eq!(options.len(), 4);
84
85        assert_eq!(
86            options[0].id,
87            Some("5de29601-9c24-4b04-8629-0bca891c5120".to_string())
88        );
89        assert_eq!(options[0].name, "Duc Loi Market");
90        assert_eq!(
91            options[0].color,
92            Some(crate::object::select::SelectColor::Blue)
93        );
94
95        assert_eq!(
96            options[1].id,
97            Some("385890b8-fe15-421b-b214-b02959b0f8d9".to_string())
98        );
99        assert_eq!(options[1].name, "Rainbow Grocery");
100        assert_eq!(
101            options[1].color,
102            Some(crate::object::select::SelectColor::Gray)
103        );
104
105        assert_eq!(
106            options[2].id,
107            Some("72ac0a6c-9e00-4e8c-80c5-720e4373e0b9".to_string())
108        );
109        assert_eq!(options[2].name, "Nijiya Market");
110        assert_eq!(
111            options[2].color,
112            Some(crate::object::select::SelectColor::Purple)
113        );
114
115        assert_eq!(
116            options[3].id,
117            Some("9556a8f7-f4b0-4e11-b277-f0af1f8c9490".to_string())
118        );
119        assert_eq!(options[3].name, "Gus's Community Market");
120        assert_eq!(
121            options[3].color,
122            Some(crate::object::select::SelectColor::Yellow)
123        );
124    }
125}