notion_client/objects/
rich_text.rs

1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3
4use super::{property::DatePropertyValue, user::User};
5
6#[skip_serializing_none]
7#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
8#[serde(tag = "type", rename_all = "snake_case")]
9pub enum RichText {
10    #[default]
11    None,
12    Equation {
13        equation: Equation,
14        annotations: Annotations,
15        plain_text: String,
16        href: Option<String>,
17    },
18    Mention {
19        mention: Mention,
20        annotations: Annotations,
21        plain_text: String,
22        href: Option<String>,
23    },
24    Text {
25        text: Text,
26        annotations: Option<Annotations>,
27        plain_text: Option<String>,
28        href: Option<String>,
29    },
30}
31
32#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
33pub struct Equation {
34    pub expression: String,
35}
36
37#[skip_serializing_none]
38#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
39pub struct Text {
40    pub content: String,
41    pub link: Option<Link>,
42}
43
44#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
45#[serde(rename_all = "snake_case")]
46pub enum TextColor {
47    #[default]
48    Default,
49    Gray,
50    Brown,
51    Orange,
52    Yellow,
53    Green,
54    Blue,
55    Purple,
56    Pink,
57    Red,
58    GrayBackground,
59    BrownBackground,
60    OrangeBackground,
61    YellowBackground,
62    GreenBackground,
63    BlueBackground,
64    PurpleBackground,
65    PinkBackground,
66    RedBackground,
67}
68
69#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
70pub struct Annotations {
71    pub bold: bool,
72    pub italic: bool,
73    pub strikethrough: bool,
74    pub underline: bool,
75    pub code: bool,
76    pub color: TextColor,
77}
78
79#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
80pub struct Link {
81    pub url: String,
82}
83
84#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
85#[serde(tag = "type")]
86#[serde(rename_all = "snake_case")]
87pub enum Mention {
88    Database { database: DatabaseMention },
89    Date { date: DatePropertyValue },
90    LinkPreview { link_preview: LinkPreviewMention },
91    LinkMention { link_mention: LinkMentionMention },
92    TemplateMention { template_mention: TemplateMention },
93    Page { page: PageMention },
94    User { user: User },
95}
96
97#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
98pub struct DatabaseMention {
99    pub id: String,
100}
101
102#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
103pub struct LinkPreviewMention {
104    pub url: String,
105}
106
107#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
108pub struct LinkMentionMention {
109    pub description: Option<String>,
110    pub href: Option<String>,
111    pub icon_url: Option<String>,
112    pub iframe_url: Option<String>,
113    pub link_author: Option<String>,
114    pub padding: Option<u32>,
115    pub thumbnail_url: Option<String>,
116    pub title: String,
117}
118
119#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
120pub struct PageMention {
121    pub id: String,
122}
123
124#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
125#[serde(tag = "type", rename_all = "snake_case")]
126pub enum TemplateMention {
127    TemplateMentionDate {
128        template_mention_date: TemplateMentionDate,
129    },
130    TemplateMentionUser {
131        template_mention_user: TemplateMentionUser,
132    },
133}
134
135#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
136#[serde(rename_all = "snake_case")]
137pub enum TemplateMentionDate {
138    Today,
139    Now,
140}
141
142#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
143#[serde(rename_all = "snake_case")]
144pub enum TemplateMentionUser {
145    Me,
146}
147
148impl RichText {
149    pub fn plain_text(&self) -> Option<String> {
150        match self {
151            RichText::None => None,
152            RichText::Equation { plain_text, .. } => Some(plain_text.clone()),
153            RichText::Mention { plain_text, .. } => Some(plain_text.clone()),
154            RichText::Text { plain_text, .. } => plain_text.clone(),
155        }
156    }
157}