notionrs_schema/object/page/checkbox.rs
1use serde::{Deserialize, Serialize};
2
3/// <https://developers.notion.com/reference/page-property-values#checkbox>
4///
5/// - `$.['*'].id`: An underlying identifier for the property.
6/// `id` remains constant when the property name changes.
7/// - `$.['*'].type`: Always `"checkbox"`
8/// - `$.['*'].checkbox`: Whether the checkbox is checked (`true`) or unchecked (`false`).
9///
10/// **Note**: The `['*']` part represents the column name you set when creating the database.
11///
12/// Example checkbox page property value
13///
14/// ```json
15/// {
16/// "Task completed": {
17/// "id": "ZI%40W",
18/// "type": "checkbox",
19/// "checkbox": true
20/// }
21/// }
22/// ```
23#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Default, notionrs_macro::Setter)]
24pub struct PageCheckboxProperty {
25 /// An underlying identifier for the property.
26 /// `id` remains constant when the property name changes.
27 #[serde(skip_serializing_if = "Option::is_none")]
28 pub id: Option<String>,
29
30 /// Whether the checkbox is checked (`true`) or unchecked (`false`).
31 pub checkbox: bool,
32}
33
34impl From<bool> for PageCheckboxProperty {
35 fn from(value: bool) -> Self {
36 Self {
37 id: None,
38 checkbox: value,
39 }
40 }
41}
42
43impl std::fmt::Display for PageCheckboxProperty {
44 /// display the checkbox value as "Yes" if checked, "No" if unchecked
45 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46 write!(f, "{}", if self.checkbox { "Yes" } else { "No" })
47 }
48}
49
50// # --------------------------------------------------------------------------------
51//
52// unit test
53//
54// # --------------------------------------------------------------------------------
55
56#[cfg(test)]
57mod unit_tests {
58 use super::*;
59
60 #[test]
61 fn deserialize_page_checkbox_property() {
62 let json_data = r#"
63 {
64 "Task completed": {
65 "id": "ZI%40W",
66 "type": "checkbox",
67 "checkbox": true
68 }
69 }
70 "#;
71
72 let checkbox_map = serde_json::from_str::<
73 std::collections::HashMap<String, PageCheckboxProperty>,
74 >(json_data)
75 .unwrap();
76
77 let checkbox = checkbox_map.get("Task completed").unwrap();
78
79 assert_eq!(checkbox.id, Some("ZI%40W".to_string()));
80 assert!(checkbox.checkbox);
81 }
82}