notion_wasi/models/
text.rs

1use crate::models::{properties::DateValue, users::User, Database, Page};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
5#[serde(rename_all = "snake_case")]
6pub enum TextColor {
7    Default,
8    Gray,
9    Brown,
10    Orange,
11    Yellow,
12    Green,
13    Blue,
14    Purple,
15    Pink,
16    Red,
17    GrayBackground,
18    BrownBackground,
19    OrangeBackground,
20    YellowBackground,
21    GreenBackground,
22    BlueBackground,
23    PurpleBackground,
24    PinkBackground,
25    RedBackground,
26}
27
28/// Rich text annotations
29/// See <https://developers.notion.com/reference/rich-text#annotations>
30#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
31pub struct Annotations {
32    pub bold: Option<bool>,
33    pub code: Option<bool>,
34    pub color: Option<TextColor>,
35    pub italic: Option<bool>,
36    pub strikethrough: Option<bool>,
37    pub underline: Option<bool>,
38}
39
40/// Properties common on all rich text objects
41/// See <https://developers.notion.com/reference/rich-text#all-rich-text>
42#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
43pub struct RichTextCommon {
44    pub plain_text: String,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub href: Option<String>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub annotations: Option<Annotations>,
49}
50
51#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
52pub struct Link {
53    pub url: String,
54}
55
56#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
57pub struct Text {
58    pub content: String,
59    pub link: Option<Link>,
60}
61
62/// See https://developers.notion.com/reference/rich-text#mention-objects
63#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
64#[serde(tag = "type")]
65#[serde(rename_all = "snake_case")]
66pub enum MentionObject {
67    User {
68        user: User,
69    },
70    // TODO: need to add tests
71    Page {
72        page: Page,
73    },
74    // TODO: need to add tests
75    Database {
76        database: Database,
77    },
78    Date {
79        date: DateValue,
80    },
81    // TODO: need to add LinkPreview
82    // LinkPreview {
83    //
84    // },
85    #[serde(other)]
86    Unknown,
87}
88
89/// Rich text objects contain data for displaying formatted text, mentions, and equations.
90/// A rich text object also contains annotations for style information.
91/// Arrays of rich text objects are used within property objects and property
92/// value objects to create what a user sees as a single text value in Notion.
93#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
94#[serde(tag = "type")]
95#[serde(rename_all = "snake_case")]
96pub enum RichText {
97    /// See <https://developers.notion.com/reference/rich-text#text-objects>
98    Text {
99        #[serde(flatten)]
100        rich_text: RichTextCommon,
101        text: Text,
102    },
103    /// See <https://developers.notion.com/reference/rich-text#mention-objects>
104    Mention {
105        #[serde(flatten)]
106        rich_text: RichTextCommon,
107        mention: MentionObject,
108    },
109    /// See <https://developers.notion.com/reference/rich-text#equation-objects>
110    Equation {
111        #[serde(flatten)]
112        rich_text: RichTextCommon,
113    },
114}
115
116impl RichText {
117    pub fn plain_text(&self) -> &str {
118        use RichText::*;
119        match self {
120            Text { rich_text, .. } | Mention { rich_text, .. } | Equation { rich_text, .. } => {
121                &rich_text.plain_text
122            }
123        }
124    }
125}