notion_tools/structs/
database.rs

1//! Database struct and its dependencies.
2//!
3//! This module defines the `Database` struct and its associated sub-structs used for representing
4//! and manipulating database objects in the application. The primary struct, `Database`, contains
5//! various fields that describe the properties and metadata of a database, such as its ID, creation
6//! time, last edited time, URL, and more. Additionally, it includes nested structs to represent
7//! specific property types within the database, such as `FormulaExpression`, `MultiSelectObject`,
8//! `NumberFormat`, `RelationObject`, `RollupObject`, `SelectObject`, and `StatusObject`.
9//!
10//! Each sub-struct is equipped with serialization and deserialization capabilities using Serde,
11//! and provides methods to check if the struct is empty. The `DatabaseProperty` struct is used to
12//! define the properties of a database, with various fields representing different property types
13//! and their corresponding data.
14//!
15use crate::structs::common::*;
16use fxhash::FxHashMap;
17use serde::{Deserialize, Serialize};
18
19#[derive(Debug, Clone, Serialize, Deserialize, Default)]
20pub struct FormulaExpression {
21    #[serde(default = "String::new")]
22    pub expression: String,
23}
24
25impl FormulaExpression {
26    pub fn is_empty(&self) -> bool {
27        self.expression.is_empty()
28    }
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize, Default)]
32pub struct MultiSelectObject {
33    #[serde(default = "Vec::default")]
34    pub options: Vec<SelectOption>,
35}
36
37impl MultiSelectObject {
38    pub fn is_empty(&self) -> bool {
39        self.options.is_empty()
40    }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, Default)]
44pub struct NumberFormat {
45    #[serde(default = "String::new")]
46    pub format: String,
47}
48
49impl NumberFormat {
50    pub fn is_empty(&self) -> bool {
51        self.format.is_empty()
52    }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize, Default)]
56pub struct RelationObject {
57    #[serde(default = "String::new")]
58    pub database_id: String,
59    #[serde(default = "String::new")]
60    pub synced_property_id: String,
61    #[serde(default = "String::new")]
62    pub synced_property_name: String,
63}
64
65impl RelationObject {
66    pub fn is_empty(&self) -> bool {
67        self.database_id.is_empty()
68    }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, Default)]
72pub struct RollupObject {
73    #[serde(default = "String::new")]
74    pub rollup_property_id: String,
75    #[serde(default = "String::new")]
76    pub rollup_property_name: String,
77    #[serde(default = "String::new")]
78    pub relation_property_id: String,
79    #[serde(default = "String::new")]
80    pub relation_property_name: String,
81    #[serde(default = "String::new")]
82    pub function: String,
83}
84
85impl RollupObject {
86    pub fn is_empty(&self) -> bool {
87        self.rollup_property_id.is_empty()
88    }
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize, Default)]
92pub struct SelectObject {
93    #[serde(default = "Vec::default")]
94    pub options: Vec<SelectOption>,
95}
96
97impl SelectObject {
98    pub fn is_empty(&self) -> bool {
99        self.options.is_empty()
100    }
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize, Default)]
104pub struct StatusObject {
105    #[serde(default = "Vec::default")]
106    pub options: Vec<SelectOption>,
107}
108
109impl StatusObject {
110    pub fn is_empty(&self) -> bool {
111        self.options.is_empty()
112    }
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize, Default)]
116pub struct DatabaseProperty {
117    #[serde(default = "String::new")]
118    pub id: String,
119    #[serde(default = "String::new")]
120    pub name: String,
121    #[serde(rename = "type", default = "String::new")]
122    pub type_name: String,
123    #[serde(
124        default = "FxHashMap::default",
125        skip_serializing_if = "FxHashMap::is_empty"
126    )]
127    pub checkbox: FxHashMap<String, String>,
128    #[serde(
129        default = "FxHashMap::default",
130        skip_serializing_if = "FxHashMap::is_empty"
131    )]
132    pub created_by: FxHashMap<String, String>,
133    #[serde(
134        default = "FxHashMap::default",
135        skip_serializing_if = "FxHashMap::is_empty"
136    )]
137    pub created_time: FxHashMap<String, String>,
138    #[serde(
139        default = "FxHashMap::default",
140        skip_serializing_if = "FxHashMap::is_empty"
141    )]
142    pub date: FxHashMap<String, String>,
143    #[serde(
144        default = "FxHashMap::default",
145        skip_serializing_if = "FxHashMap::is_empty"
146    )]
147    pub email: FxHashMap<String, String>,
148    #[serde(
149        default = "FxHashMap::default",
150        skip_serializing_if = "FxHashMap::is_empty"
151    )]
152    pub files: FxHashMap<String, String>,
153    #[serde(
154        default = "FormulaExpression::default",
155        skip_serializing_if = "FormulaExpression::is_empty"
156    )]
157    pub formula: FormulaExpression,
158    #[serde(
159        default = "FxHashMap::default",
160        skip_serializing_if = "FxHashMap::is_empty"
161    )]
162    pub last_edited_by: FxHashMap<String, String>,
163    #[serde(
164        default = "FxHashMap::default",
165        skip_serializing_if = "FxHashMap::is_empty"
166    )]
167    pub last_edited_time: FxHashMap<String, String>,
168    #[serde(
169        default = "MultiSelectObject::default",
170        skip_serializing_if = "MultiSelectObject::is_empty"
171    )]
172    pub multi_select: MultiSelectObject,
173    #[serde(
174        default = "NumberFormat::default",
175        skip_serializing_if = "NumberFormat::is_empty"
176    )]
177    pub number: NumberFormat,
178    #[serde(
179        default = "FxHashMap::default",
180        skip_serializing_if = "FxHashMap::is_empty"
181    )]
182    pub people: FxHashMap<String, String>,
183    #[serde(
184        default = "FxHashMap::default",
185        skip_serializing_if = "FxHashMap::is_empty"
186    )]
187    pub phone_number: FxHashMap<String, String>,
188    #[serde(
189        default = "RelationObject::default",
190        skip_serializing_if = "RelationObject::is_empty"
191    )]
192    pub relation: RelationObject,
193    #[serde(
194        default = "FxHashMap::default",
195        skip_serializing_if = "FxHashMap::is_empty"
196    )]
197    pub rich_text: FxHashMap<String, String>,
198    #[serde(
199        default = "RollupObject::default",
200        skip_serializing_if = "RollupObject::is_empty"
201    )]
202    pub rollup: RollupObject,
203    #[serde(
204        default = "SelectObject::default",
205        skip_serializing_if = "SelectObject::is_empty"
206    )]
207    pub select: SelectObject,
208    #[serde(
209        default = "StatusObject::default",
210        skip_serializing_if = "StatusObject::is_empty"
211    )]
212    pub status: StatusObject,
213    #[serde(
214        default = "FxHashMap::default",
215        skip_serializing_if = "FxHashMap::is_empty"
216    )]
217    pub title: FxHashMap<String, String>,
218    #[serde(
219        default = "FxHashMap::default",
220        skip_serializing_if = "FxHashMap::is_empty"
221    )]
222    pub url: FxHashMap<String, String>,
223}
224
225#[derive(Debug, Clone, Serialize, Deserialize)]
226pub struct Database {
227    #[serde(default = "String::new")]
228    pub object: String,
229    #[serde(default = "u32::default", skip_serializing)]
230    pub status: u32,
231    #[serde(default = "String::new", skip_serializing)]
232    pub code: String,
233    #[serde(default = "String::new", skip_serializing)]
234    pub message: String,
235    #[serde(default = "String::new", skip_serializing)]
236    pub request_id: String,
237    #[serde(default = "String::new")]
238    pub id: String,
239    #[serde(default = "String::new")]
240    pub created_time: String,
241    #[serde(default = "String::new")]
242    pub last_edited_time: String,
243    #[serde(default = "User::default")]
244    pub created_by: User,
245    #[serde(default = "User::default")]
246    pub last_edited_by: User,
247    #[serde(default = "String::new")]
248    pub url: String,
249    #[serde(default = "bool::default")]
250    pub archived: bool,
251    #[serde(default = "bool::default")]
252    pub is_inline: bool,
253    #[serde(default = "Option::default")]
254    pub public_url: Option<String>,
255    #[serde(default = "Vec::default")]
256    pub title: Vec<RichText>,
257    #[serde(default = "Vec::default")]
258    pub description: Vec<RichText>,
259    #[serde(default = "FxHashMap::default")]
260    pub properties: FxHashMap<String, DatabaseProperty>,
261}