notion_sdk/common/
rich_text.rs

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