notion_api_client/models/
text.rs

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