notionrs_types/object/page/
last_edited_time.rs

1use serde::{Deserialize, Serialize};
2
3/// <https://developers.notion.com/reference/page-property-values#last-edited-time>
4///
5/// - `$.['*'].id`: An underlying identifier for the property.
6///   `id` remains constant when the property name changes.
7/// - `$.['*'].type`: Always `"last_edited_time"`
8/// - `$.['*'].last_edited_time`: The date and time that the page was last edited.
9///
10/// **Note**: The `['*']` part represents the column name you set when creating the database.
11///
12/// Example last_edited_time page property value
13///
14/// ```json
15/// {
16///   "Last edited time": {
17///     "id": "sv%3Fi",
18///     "type": "last_edited_time",
19///     "last_edited_time": "2024-04-03T10:55:00.000Z"
20///   }
21/// }
22/// ```
23#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, notionrs_macro::Setter)]
24pub struct PageLastEditedTimeProperty {
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    /// The date and time that the page was last edited.
31    #[serde(with = "time::serde::rfc3339")]
32    pub last_edited_time: time::OffsetDateTime,
33}
34
35impl std::fmt::Display for PageLastEditedTimeProperty {
36    /// Display
37    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38        write!(
39            f,
40            "{}",
41            self.last_edited_time
42                .format(&time::format_description::well_known::Rfc3339)
43                .unwrap_or("[Invalid Format]".to_owned())
44        )
45    }
46}
47
48// # --------------------------------------------------------------------------------
49//
50// unit test
51//
52// # --------------------------------------------------------------------------------
53
54#[cfg(test)]
55mod unit_tests {
56
57    use super::*;
58
59    #[test]
60    fn deserialize_page_last_edited_time_property() {
61        let json_data = r#"
62        {
63            "Last edited time": {
64                "id": "sv%3Fi",
65                "type": "last_edited_time",
66                "last_edited_time": "2024-04-03T10:55:00.000Z"
67            }
68        }
69        "#;
70
71        let last_edited_time_map = serde_json::from_str::<
72            std::collections::HashMap<String, PageLastEditedTimeProperty>,
73        >(json_data)
74        .unwrap();
75
76        let last_edited_time = last_edited_time_map.get("Last edited time").unwrap();
77
78        assert_eq!(last_edited_time.id, Some("sv%3Fi".to_string()));
79
80        let expected_last_edited_time = time::OffsetDateTime::new_utc(
81            time::Date::from_calendar_date(2024, time::Month::April, 3).unwrap(),
82            time::Time::from_hms(10, 55, 0).unwrap(),
83        );
84        assert_eq!(last_edited_time.last_edited_time, expected_last_edited_time);
85    }
86}