text_document_common/parser_tools/
fragment_schema.rs1use 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 is_inline_only(&self) -> bool {
113 self.heading_level.is_none()
114 && self.list.is_none()
115 && self.alignment.is_none()
116 && self.indent.unwrap_or(0) == 0
117 && self.text_indent.unwrap_or(0) == 0
118 && self.marker.is_none()
119 && self.top_margin.is_none()
120 && self.bottom_margin.is_none()
121 && self.left_margin.is_none()
122 && self.right_margin.is_none()
123 && self.line_height.is_none()
124 && self.non_breakable_lines.is_none()
125 && self.direction.is_none()
126 && self.background_color.is_none()
127 }
128
129 pub fn from_entity(block: &Block, elements: &[InlineElement], list: Option<&List>) -> Self {
130 FragmentBlock {
131 plain_text: block.plain_text.clone(),
132 elements: elements.iter().map(FragmentElement::from_entity).collect(),
133 heading_level: block.fmt_heading_level,
134 list: list.map(FragmentList::from_entity),
135 alignment: block.fmt_alignment.clone(),
136 indent: block.fmt_indent,
137 text_indent: block.fmt_text_indent,
138 marker: block.fmt_marker.clone(),
139 top_margin: block.fmt_top_margin,
140 bottom_margin: block.fmt_bottom_margin,
141 left_margin: block.fmt_left_margin,
142 right_margin: block.fmt_right_margin,
143 tab_positions: block.fmt_tab_positions.clone(),
144 line_height: block.fmt_line_height,
145 non_breakable_lines: block.fmt_non_breakable_lines,
146 direction: block.fmt_direction.clone(),
147 background_color: block.fmt_background_color.clone(),
148 }
149 }
150}
151
152impl FragmentList {
153 pub fn from_entity(list: &List) -> Self {
154 FragmentList {
155 style: list.style.clone(),
156 indent: list.indent,
157 prefix: list.prefix.clone(),
158 suffix: list.suffix.clone(),
159 }
160 }
161
162 pub fn to_entity(&self) -> List {
163 List {
164 id: 0,
165 created_at: chrono::Utc::now(),
166 updated_at: chrono::Utc::now(),
167 style: self.style.clone(),
168 indent: self.indent,
169 prefix: self.prefix.clone(),
170 suffix: self.suffix.clone(),
171 }
172 }
173}