acdc_parser/model/inlines/
text.rs

1use serde::{
2    Deserialize, Serialize,
3    ser::{SerializeMap, Serializer},
4};
5
6use crate::{Location, Role};
7
8use super::InlineNode;
9
10/// The form of an inline formatting element (how it was expressed in the source)
11#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13pub enum Form {
14    Constrained,
15    Unconstrained,
16}
17
18/// A `Subscript` represents a subscript section of text in a document.
19#[derive(Clone, Debug, PartialEq, Deserialize)]
20pub struct Subscript {
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub role: Option<Role>,
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub id: Option<String>,
25    pub form: Form,
26    #[serde(rename = "inlines")]
27    pub content: Vec<InlineNode>,
28    pub location: Location,
29}
30
31/// A `Superscript` represents a superscript section of text in a document.
32#[derive(Clone, Debug, PartialEq, Deserialize)]
33pub struct Superscript {
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub role: Option<Role>,
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub id: Option<String>,
38    pub form: Form,
39    #[serde(rename = "inlines")]
40    pub content: Vec<InlineNode>,
41    pub location: Location,
42}
43
44/// A `CurvedQuotation` represents a curved quotation section of text in a document.
45#[derive(Clone, Debug, PartialEq, Deserialize)]
46pub struct CurvedQuotation {
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub role: Option<Role>,
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub id: Option<String>,
51    pub form: Form,
52    #[serde(rename = "inlines")]
53    pub content: Vec<InlineNode>,
54    pub location: Location,
55}
56
57/// A `CurvedApostrophe` represents a curved apostrophe section of text in a document.
58#[derive(Clone, Debug, PartialEq, Deserialize)]
59pub struct CurvedApostrophe {
60    #[serde(default, skip_serializing_if = "Option::is_none")]
61    pub role: Option<Role>,
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub id: Option<String>,
64    pub form: Form,
65    #[serde(rename = "inlines")]
66    pub content: Vec<InlineNode>,
67    pub location: Location,
68}
69
70/// A `StandaloneCurvedApostrophe` represents a standalone curved apostrophe character.
71#[derive(Clone, Debug, PartialEq, Deserialize)]
72pub struct StandaloneCurvedApostrophe {
73    pub location: Location,
74}
75
76/// A `Monospace` represents a monospace section of text in a document.
77#[derive(Clone, Debug, PartialEq, Deserialize)]
78pub struct Monospace {
79    #[serde(default, skip_serializing_if = "Option::is_none")]
80    pub role: Option<Role>,
81    #[serde(default, skip_serializing_if = "Option::is_none")]
82    pub id: Option<String>,
83    pub form: Form,
84    #[serde(rename = "inlines")]
85    pub content: Vec<InlineNode>,
86    pub location: Location,
87}
88
89/// A `Highlight` represents a highlighted section of text in a document.
90#[derive(Clone, Debug, PartialEq, Deserialize)]
91pub struct Highlight {
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub role: Option<Role>,
94    #[serde(default, skip_serializing_if = "Option::is_none")]
95    pub id: Option<String>,
96    pub form: Form,
97    #[serde(rename = "inlines")]
98    pub content: Vec<InlineNode>,
99    pub location: Location,
100}
101
102/// A `Bold` represents a bold section of text in a document.
103#[derive(Clone, Debug, PartialEq, Deserialize)]
104pub struct Bold {
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub role: Option<Role>,
107    #[serde(default, skip_serializing_if = "Option::is_none")]
108    pub id: Option<String>,
109    pub form: Form,
110    #[serde(rename = "inlines")]
111    pub content: Vec<InlineNode>,
112    pub location: Location,
113}
114
115/// An `Italic` represents an italic section of text in a document.
116#[derive(Clone, Debug, PartialEq, Deserialize)]
117pub struct Italic {
118    #[serde(default, skip_serializing_if = "Option::is_none")]
119    pub role: Option<Role>,
120    #[serde(default, skip_serializing_if = "Option::is_none")]
121    pub id: Option<String>,
122    pub form: Form,
123    #[serde(rename = "inlines")]
124    pub content: Vec<InlineNode>,
125    pub location: Location,
126}
127
128/// A `LineBreak` represents a line break (inline).
129#[derive(Clone, Debug, PartialEq, Deserialize)]
130pub struct LineBreak {
131    pub location: Location,
132}
133
134impl Serialize for LineBreak {
135    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
136    where
137        S: Serializer,
138    {
139        let mut state = serializer.serialize_map(Some(3))?;
140        state.serialize_entry("name", "linebreak")?;
141        state.serialize_entry("type", "string")?;
142        state.serialize_entry("location", &self.location)?;
143        state.end()
144    }
145}
146
147/// A `Plain` represents a plain text section in a document.
148///
149/// This is the most basic form of text in a document.
150#[derive(Clone, Debug, PartialEq, Deserialize)]
151pub struct Plain {
152    #[serde(rename = "value")]
153    pub content: String,
154    pub location: Location,
155}
156
157impl Serialize for Plain {
158    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
159    where
160        S: Serializer,
161    {
162        let mut state = serializer.serialize_map(Some(4))?;
163        state.serialize_entry("name", "text")?;
164        state.serialize_entry("type", "string")?;
165        state.serialize_entry("value", &self.content)?;
166        state.serialize_entry("location", &self.location)?;
167        state.end()
168    }
169}
170
171/// A `Raw` represents a raw text section in a document.
172///
173/// This is the most basic form of text in a document and it should note that its contents
174/// must be rendered as they are (e.g: "\<h1>" should not end up being a \<h1> tag, it
175/// should be "\<h1>" text in html, very likely \&lt;h1\&gt;).
176#[derive(Clone, Debug, PartialEq, Deserialize)]
177pub struct Raw {
178    #[serde(rename = "value")]
179    pub content: String,
180    pub location: Location,
181}
182
183/// A `Verbatim` represents verbatim text section in a document.
184///
185/// This is the most basic form of text in a document and it should note that its contents
186/// must be rendered as they are (e.g: "\<h1>" should not end up being a \<h1> tag, it
187/// should be "\<h1>" text in html, very likely \&lt;h1\&gt;).
188///
189/// It is similar to `Raw`, but is intended for use in contexts where verbatim text is
190/// used, and some substitutions are done, namely converting callouts.
191#[derive(Clone, Debug, PartialEq, Deserialize)]
192pub struct Verbatim {
193    #[serde(rename = "value")]
194    pub content: String,
195    pub location: Location,
196}
197
198impl Serialize for StandaloneCurvedApostrophe {
199    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
200    where
201        S: Serializer,
202    {
203        let mut state = serializer.serialize_map(Some(3))?;
204        state.serialize_entry("name", "curved_apostrophe")?;
205        state.serialize_entry("type", "string")?;
206        state.serialize_entry("location", &self.location)?;
207        state.end()
208    }
209}