notionrs_schema/object/page/
verification.rs

1use serde::{Deserialize, Serialize};
2
3/// <https://developers.notion.com/reference/page-property-values#verification>
4///
5/// The verification status of a page in a wiki database. Pages can be verified or unverified,
6/// and verifications can have an optional expiration date set.
7///
8/// The verification status cannot currently be set or updated via the public API.
9///
10/// - `$.['*'].state`: The verification state of the page. `"verified"` or `"unverified"`.
11/// - `$.['*'].verified_by`: Always `"url"`
12/// - `$.['*'].date?`: If the page if verified, a User object will be included to indicate the user who verified the page.
13///   If an expiration date is set for the verification, an end date (end) will be included.
14///
15/// **Note**: The `['*']` part represents the column name you set when creating the database.
16///
17#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default, notionrs_macro::Setter)]
18pub struct PageVerificationProperty {
19    #[serde(skip_serializing)]
20    pub id: Option<String>,
21
22    pub verification: PageVerificationPropertyParameter,
23}
24
25#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default, notionrs_macro::Setter)]
26pub struct PageVerificationPropertyParameter {
27    /// The verification state of the page. `"verified"` or `"unverified"`.
28    pub state: PageVerificationState,
29
30    pub verified_by: Option<crate::object::user::User>,
31
32    pub date: Option<PageVerificationDate>,
33}
34
35#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default, PartialEq)]
36#[serde(rename_all = "lowercase")]
37pub enum PageVerificationState {
38    Verified,
39    #[default]
40    Unverified,
41    Expired,
42}
43
44#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default, notionrs_macro::Setter)]
45pub struct PageVerificationDate {
46    /// A date, with an optional time.
47    pub start: Option<crate::object::date::DateOrDateTime>,
48
49    /// A string representing the end of a date range.
50    /// If the value is null, then the date value is not a range.
51    pub end: Option<crate::object::date::DateOrDateTime>,
52
53    /// Always `null`. The time zone is already included in the formats of start and end times.
54    #[serde(skip_deserializing)]
55    pub time_zone: Option<String>,
56}
57
58impl PageVerificationProperty {
59    pub fn state(&mut self, state: PageVerificationState) -> &mut Self {
60        self.verification.state = state;
61        self
62    }
63
64    pub fn verified_by(&mut self, verified_by: crate::object::user::User) -> &mut Self {
65        self.verification.verified_by = Some(verified_by);
66        self
67    }
68
69    pub fn date(&mut self, date: PageVerificationDate) -> &mut Self {
70        self.verification.date = Some(date);
71        self
72    }
73}
74
75impl std::fmt::Display for PageVerificationProperty {
76    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
77        write!(
78            f,
79            "{}",
80            match self.verification.state {
81                PageVerificationState::Verified => "verified",
82                PageVerificationState::Unverified => "unverified",
83                PageVerificationState::Expired => "expired",
84            }
85        )
86    }
87}
88
89// # --------------------------------------------------------------------------------
90//
91// unit test
92//
93// # --------------------------------------------------------------------------------
94
95#[cfg(test)]
96mod unit_tests {
97    use super::*;
98
99    #[test]
100    fn deserialize_page_verification_property() {
101        let json_data = r#"
102        {
103            "id": "%3DP%7CC",
104            "type": "verification",
105            "verification": {
106                "state": "verified",
107                "verified_by": {
108                    "object": "user",
109                    "id": "174984bc-2b3e-408f-97fd-fa5ff989e907",
110                    "name": "<masked>",
111                    "avatar_url": "https://example.com/",
112                    "type": "person",
113                    "person": {
114                        "email": "<masked>@example.com"
115                    }
116                },
117                "date": {
118                    "start": "2024-12-11T15:00:00.000Z",
119                    "end": "2024-12-18T15:00:00.000Z",
120                    "time_zone": null
121                }
122            }
123        }
124        "#;
125
126        let page_verification: PageVerificationProperty = serde_json::from_str(json_data).unwrap();
127
128        assert_eq!(
129            page_verification.verification.state,
130            PageVerificationState::Verified
131        );
132    }
133}