notionrs_schema/object/page/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    object::user::User,
5    object::{file::File, icon::Icon, parent::Parent},
6};
7
8pub mod button;
9pub mod checkbox;
10pub mod created_by;
11pub mod created_time;
12pub mod date;
13pub mod email;
14pub mod files;
15pub mod formula;
16pub mod last_edited_by;
17pub mod last_edited_time;
18pub mod multi_select;
19pub mod number;
20pub mod people;
21pub mod phone_number;
22pub mod relation;
23pub mod rich_text;
24pub mod rollup;
25pub mod select;
26pub mod status;
27pub mod title;
28pub mod unique_id;
29pub mod url;
30pub mod verification;
31
32#[derive(Debug, Deserialize, Serialize, Clone)]
33#[serde(tag = "type", rename_all = "snake_case")]
34pub enum PageProperty {
35    Button(button::PageButtonProperty),
36    Checkbox(checkbox::PageCheckboxProperty),
37    CreatedBy(created_by::PageCreatedByProperty),
38    CreatedTime(created_time::PageCreatedTimeProperty),
39    Date(date::PageDateProperty),
40    Email(email::PageEmailProperty),
41    Files(files::PageFilesProperty),
42    Formula(formula::PageFormulaProperty),
43    LastEditedBy(last_edited_by::PageLastEditedByProperty),
44    LastEditedTime(last_edited_time::PageLastEditedTimeProperty),
45    MultiSelect(multi_select::PageMultiSelectProperty),
46    Number(number::PageNumberProperty),
47    People(people::PagePeopleProperty),
48    PhoneNumber(phone_number::PagePhoneNumberProperty),
49    Relation(relation::PageRelationProperty),
50    RichText(rich_text::PageRichTextProperty),
51    Rollup(rollup::PageRollupProperty),
52    Select(select::PageSelectProperty),
53    Status(status::PageStatusProperty),
54    Title(title::PageTitleProperty),
55    UniqueId(unique_id::PageUniqueIdProperty),
56    Url(url::PageUrlProperty),
57    Verification(verification::PageVerificationProperty),
58}
59
60impl std::fmt::Display for PageProperty {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        match self {
63            PageProperty::Button(button) => write!(f, "{}", button),
64            PageProperty::Checkbox(checkbox) => write!(f, "{}", checkbox),
65            PageProperty::CreatedBy(created_by) => write!(f, "{}", created_by),
66            PageProperty::CreatedTime(created_time) => write!(f, "{}", created_time),
67            PageProperty::Date(date) => write!(f, "{}", date),
68            PageProperty::Email(email) => write!(f, "{}", email),
69            PageProperty::Files(files) => write!(f, "{}", files),
70            PageProperty::Formula(formula) => write!(f, "{}", formula),
71            PageProperty::LastEditedBy(last_edited_by) => write!(f, "{}", last_edited_by),
72            PageProperty::LastEditedTime(last_edited_time) => write!(f, "{}", last_edited_time),
73            PageProperty::MultiSelect(multi_select) => write!(f, "{}", multi_select),
74            PageProperty::Number(number) => write!(f, "{}", number),
75            PageProperty::People(people) => write!(f, "{}", people),
76            PageProperty::PhoneNumber(phone_number) => write!(f, "{}", phone_number),
77            PageProperty::Relation(relation) => write!(f, "{}", relation),
78            PageProperty::RichText(rich_text) => write!(f, "{}", rich_text),
79            PageProperty::Rollup(rollup) => write!(f, "{}", rollup),
80            PageProperty::Select(select) => write!(f, "{}", select),
81            PageProperty::Status(status) => write!(f, "{}", status),
82            PageProperty::Title(title) => write!(f, "{}", title),
83            PageProperty::UniqueId(unique_id) => write!(f, "{}", unique_id),
84            PageProperty::Url(url) => write!(f, "{}", url),
85            PageProperty::Verification(verification) => write!(f, "{}", verification),
86        }
87    }
88}
89
90/// <https://developers.notion.com/reference/page>
91#[derive(Debug, Deserialize, Serialize, Clone)]
92pub struct PageResponse {
93    pub id: String,
94    #[serde(with = "time::serde::rfc3339")]
95    pub created_time: time::OffsetDateTime,
96    #[serde(with = "time::serde::rfc3339")]
97    pub last_edited_time: time::OffsetDateTime,
98    pub created_by: User,
99    pub last_edited_by: User,
100    pub cover: Option<File>,
101    pub icon: Option<Icon>,
102    pub parent: Parent,
103    pub archived: bool,
104    pub properties: std::collections::HashMap<String, crate::object::page::PageProperty>,
105    pub url: String,
106    pub public_url: Option<String>,
107    pub developer_survey: Option<String>,
108    pub request_id: Option<String>,
109    pub in_trash: bool,
110}
111
112// # --------------------------------------------------------------------------------
113//
114// unit_tests
115//
116// # --------------------------------------------------------------------------------
117
118#[cfg(test)]
119mod tests {
120
121    #[test]
122    fn deserialize_wiki_page() {
123        let json_data = include_str!("./seeds/page_wiki.json");
124
125        let _page = serde_json::from_str::<crate::object::page::PageResponse>(json_data)
126            .expect("An error occurred while deserializing the page");
127    }
128
129    #[test]
130    fn deserialize_page() {
131        let json_data = include_str!("./seeds/page.json");
132
133        let _page = serde_json::from_str::<crate::object::page::PageResponse>(json_data)
134            .expect("An error occurred while deserializing the page");
135    }
136
137    #[test]
138    fn deserialize_page_icon_emoji() {
139        let json_data = include_str!("./seeds/page_icon_emoji.json");
140
141        let _page = serde_json::from_str::<crate::object::page::PageResponse>(json_data)
142            .expect("An error occurred while deserializing the page");
143    }
144}