rtdlib/types/
formatted_text.rs

1
2use crate::types::*;
3use crate::errors::*;
4use uuid::Uuid;
5
6
7
8
9/// A text with some entities
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11pub struct FormattedText {
12  #[doc(hidden)]
13  #[serde(rename(serialize = "@type", deserialize = "@type"))]
14  td_name: String,
15  #[doc(hidden)]
16  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
17  extra: Option<String>,
18  /// The text
19  text: String,
20  /// Entities contained in the text. Entities can be nested, but must not mutually intersect with each other. Pre, Code and PreCode entities can't contain other entities. Bold, Italic, Underline and Strikethrough entities can contain and to be contained in all other entities. All other entities can't contain each other
21  entities: Vec<TextEntity>,
22  
23}
24
25impl RObject for FormattedText {
26  #[doc(hidden)] fn td_name(&self) -> &'static str { "formattedText" }
27  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
28  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
29}
30
31
32
33impl FormattedText {
34  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
35  pub fn builder() -> RTDFormattedTextBuilder {
36    let mut inner = FormattedText::default();
37    inner.td_name = "formattedText".to_string();
38    inner.extra = Some(Uuid::new_v4().to_string());
39    RTDFormattedTextBuilder { inner }
40  }
41
42  pub fn text(&self) -> &String { &self.text }
43
44  pub fn entities(&self) -> &Vec<TextEntity> { &self.entities }
45
46}
47
48#[doc(hidden)]
49pub struct RTDFormattedTextBuilder {
50  inner: FormattedText
51}
52
53impl RTDFormattedTextBuilder {
54  pub fn build(&self) -> FormattedText { self.inner.clone() }
55
56   
57  pub fn text<T: AsRef<str>>(&mut self, text: T) -> &mut Self {
58    self.inner.text = text.as_ref().to_string();
59    self
60  }
61
62   
63  pub fn entities(&mut self, entities: Vec<TextEntity>) -> &mut Self {
64    self.inner.entities = entities;
65    self
66  }
67
68}
69
70impl AsRef<FormattedText> for FormattedText {
71  fn as_ref(&self) -> &FormattedText { self }
72}
73
74impl AsRef<FormattedText> for RTDFormattedTextBuilder {
75  fn as_ref(&self) -> &FormattedText { &self.inner }
76}
77
78
79