Skip to main content

tauri_plugin_material_you/
models.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct MaterialYouResponse {
7    pub supported: bool,
8    pub api_level: i32,
9    pub palettes: Palettes,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Palettes {
14    pub system_accent1: Option<HashMap<String, String>>,
15    pub system_accent2: Option<HashMap<String, String>>,
16    pub system_accent3: Option<HashMap<String, String>>,
17    pub system_neutral1: Option<HashMap<String, String>>,
18    pub system_neutral2: Option<HashMap<String, String>>,
19}
20
21#[cfg(test)]
22mod tests {
23    use super::{MaterialYouResponse, Palettes};
24    use std::collections::HashMap;
25
26    #[test]
27    fn serialises_api_level_as_camel_case() {
28        let mut accent = HashMap::new();
29        accent.insert("500".to_string(), "#FF123456".to_string());
30
31        let response = MaterialYouResponse {
32            supported: true,
33            api_level: 34,
34            palettes: Palettes {
35                system_accent1: Some(accent),
36                system_accent2: None,
37                system_accent3: None,
38                system_neutral1: None,
39                system_neutral2: None,
40            },
41        };
42
43        let json = serde_json::to_value(response).expect("serialisation should succeed");
44        assert_eq!(json["supported"], true);
45        assert_eq!(json["apiLevel"], 34);
46        assert!(json.get("api_level").is_none());
47    }
48
49    #[test]
50    fn deserialises_api_level_from_camel_case() {
51        let json = serde_json::json!({
52            "supported": false,
53            "apiLevel": 0,
54            "palettes": {
55                "system_accent1": null,
56                "system_accent2": null,
57                "system_accent3": null,
58                "system_neutral1": null,
59                "system_neutral2": null
60            }
61        });
62
63        let response: MaterialYouResponse =
64            serde_json::from_value(json).expect("deserialisation should succeed");
65        assert!(!response.supported);
66        assert_eq!(response.api_level, 0);
67        assert!(response.palettes.system_accent1.is_none());
68    }
69}