Skip to main content

text_document_common/parser_tools/
fragment_schema.rs

1use serde::{Deserialize, Serialize};
2
3use crate::entities::*;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct FragmentData {
7    pub blocks: Vec<FragmentBlock>,
8}
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct FragmentBlock {
12    pub plain_text: String,
13    pub elements: Vec<FragmentElement>,
14    pub heading_level: Option<i64>,
15    pub list: Option<FragmentList>,
16    pub alignment: Option<Alignment>,
17    pub indent: Option<i64>,
18    pub text_indent: Option<i64>,
19    pub marker: Option<MarkerType>,
20    pub top_margin: Option<i64>,
21    pub bottom_margin: Option<i64>,
22    pub left_margin: Option<i64>,
23    pub right_margin: Option<i64>,
24    pub tab_positions: Vec<i64>,
25    pub line_height: Option<i64>,
26    pub non_breakable_lines: Option<bool>,
27    pub direction: Option<TextDirection>,
28    pub background_color: Option<String>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct FragmentElement {
33    pub content: InlineContent,
34    pub fmt_font_family: Option<String>,
35    pub fmt_font_point_size: Option<i64>,
36    pub fmt_font_weight: Option<i64>,
37    pub fmt_font_bold: Option<bool>,
38    pub fmt_font_italic: Option<bool>,
39    pub fmt_font_underline: Option<bool>,
40    pub fmt_font_overline: Option<bool>,
41    pub fmt_font_strikeout: Option<bool>,
42    pub fmt_letter_spacing: Option<i64>,
43    pub fmt_word_spacing: Option<i64>,
44    pub fmt_anchor_href: Option<String>,
45    pub fmt_anchor_names: Vec<String>,
46    pub fmt_is_anchor: Option<bool>,
47    pub fmt_tooltip: Option<String>,
48    pub fmt_underline_style: Option<UnderlineStyle>,
49    pub fmt_vertical_alignment: Option<CharVerticalAlignment>,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct FragmentList {
54    pub style: ListStyle,
55    pub indent: i64,
56    pub prefix: String,
57    pub suffix: String,
58}
59
60impl FragmentElement {
61    pub fn from_entity(e: &InlineElement) -> Self {
62        FragmentElement {
63            content: e.content.clone(),
64            fmt_font_family: e.fmt_font_family.clone(),
65            fmt_font_point_size: e.fmt_font_point_size,
66            fmt_font_weight: e.fmt_font_weight,
67            fmt_font_bold: e.fmt_font_bold,
68            fmt_font_italic: e.fmt_font_italic,
69            fmt_font_underline: e.fmt_font_underline,
70            fmt_font_overline: e.fmt_font_overline,
71            fmt_font_strikeout: e.fmt_font_strikeout,
72            fmt_letter_spacing: e.fmt_letter_spacing,
73            fmt_word_spacing: e.fmt_word_spacing,
74            fmt_anchor_href: e.fmt_anchor_href.clone(),
75            fmt_anchor_names: e.fmt_anchor_names.clone(),
76            fmt_is_anchor: e.fmt_is_anchor,
77            fmt_tooltip: e.fmt_tooltip.clone(),
78            fmt_underline_style: e.fmt_underline_style.clone(),
79            fmt_vertical_alignment: e.fmt_vertical_alignment.clone(),
80        }
81    }
82
83    pub fn to_entity(&self) -> InlineElement {
84        InlineElement {
85            id: 0,
86            created_at: chrono::Utc::now(),
87            updated_at: chrono::Utc::now(),
88            content: self.content.clone(),
89            fmt_font_family: self.fmt_font_family.clone(),
90            fmt_font_point_size: self.fmt_font_point_size,
91            fmt_font_weight: self.fmt_font_weight,
92            fmt_font_bold: self.fmt_font_bold,
93            fmt_font_italic: self.fmt_font_italic,
94            fmt_font_underline: self.fmt_font_underline,
95            fmt_font_overline: self.fmt_font_overline,
96            fmt_font_strikeout: self.fmt_font_strikeout,
97            fmt_letter_spacing: self.fmt_letter_spacing,
98            fmt_word_spacing: self.fmt_word_spacing,
99            fmt_anchor_href: self.fmt_anchor_href.clone(),
100            fmt_anchor_names: self.fmt_anchor_names.clone(),
101            fmt_is_anchor: self.fmt_is_anchor,
102            fmt_tooltip: self.fmt_tooltip.clone(),
103            fmt_underline_style: self.fmt_underline_style.clone(),
104            fmt_vertical_alignment: self.fmt_vertical_alignment.clone(),
105        }
106    }
107}
108
109impl FragmentBlock {
110    pub fn from_entity(block: &Block, elements: &[InlineElement], list: Option<&List>) -> Self {
111        FragmentBlock {
112            plain_text: block.plain_text.clone(),
113            elements: elements.iter().map(FragmentElement::from_entity).collect(),
114            heading_level: block.fmt_heading_level,
115            list: list.map(FragmentList::from_entity),
116            alignment: block.fmt_alignment.clone(),
117            indent: block.fmt_indent,
118            text_indent: block.fmt_text_indent,
119            marker: block.fmt_marker.clone(),
120            top_margin: block.fmt_top_margin,
121            bottom_margin: block.fmt_bottom_margin,
122            left_margin: block.fmt_left_margin,
123            right_margin: block.fmt_right_margin,
124            tab_positions: block.fmt_tab_positions.clone(),
125            line_height: block.fmt_line_height,
126            non_breakable_lines: block.fmt_non_breakable_lines,
127            direction: block.fmt_direction.clone(),
128            background_color: block.fmt_background_color.clone(),
129        }
130    }
131}
132
133impl FragmentList {
134    pub fn from_entity(list: &List) -> Self {
135        FragmentList {
136            style: list.style.clone(),
137            indent: list.indent,
138            prefix: list.prefix.clone(),
139            suffix: list.suffix.clone(),
140        }
141    }
142
143    pub fn to_entity(&self) -> List {
144        List {
145            id: 0,
146            created_at: chrono::Utc::now(),
147            updated_at: chrono::Utc::now(),
148            style: self.style.clone(),
149            indent: self.indent,
150            prefix: self.prefix.clone(),
151            suffix: self.suffix.clone(),
152        }
153    }
154}