notionrs_schema/object/block/
paragraph.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Deserialize, Serialize, Debug, Default, Clone, notionrs_macro::Setter)]
8pub struct ParagraphBlock {
9 pub rich_text: Vec<crate::object::rich_text::RichText>,
11
12 pub color: crate::object::color::Color,
14}
15
16impl ParagraphBlock {
17 crate::color_setters!(self, self.color);
18}
19
20impl<T> From<T> for ParagraphBlock
21where
22 T: AsRef<str>,
23{
24 fn from(plain_text: T) -> Self {
25 let rich_text = crate::object::rich_text::RichText::from(plain_text.as_ref().to_string());
26 Self::default().rich_text(vec![rich_text])
27 }
28}
29
30impl std::fmt::Display for ParagraphBlock {
31 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
32 write!(
33 f,
34 "{}",
35 self.rich_text
36 .iter()
37 .map(|t| { t.to_string() })
38 .collect::<String>()
39 )
40 }
41}
42
43#[cfg(test)]
50mod unit_tests {
51
52 use super::ParagraphBlock;
53
54 #[test]
55 fn deserialize_block_paragraph() {
56 let json_data = r#"
57 {
58 "rich_text": [
59 {
60 "type": "text",
61 "text": {
62 "content": "List Item 1",
63 "link": null
64 },
65 "annotations": {
66 "bold": false,
67 "italic": false,
68 "strikethrough": false,
69 "underline": false,
70 "code": false,
71 "color": "default"
72 },
73 "plain_text": "p",
74 "href": null
75 }
76 ],
77 "color": "default"
78 }
79 "#;
80
81 let paragraph: ParagraphBlock = serde_json::from_str::<ParagraphBlock>(json_data).unwrap();
82
83 assert_eq!(paragraph.color, crate::object::color::Color::Default);
84
85 let rich_text = paragraph.rich_text.first().unwrap();
86
87 match rich_text {
101 crate::object::rich_text::RichText::Text {
102 annotations,
103 plain_text,
104 href,
105 ..
106 } => {
107 assert_eq!(plain_text, "p");
108 assert_eq!(*href, None);
109
110 assert!(!annotations.bold);
111 assert!(!annotations.code);
112 assert!(!annotations.strikethrough);
113 assert!(!annotations.underline);
114 assert!(!annotations.italic);
115 assert_eq!(annotations.color, crate::object::color::Color::Default)
116 }
117 _ => panic!(),
118 }
119 }
120}