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    pub is_code_block: Option<bool>,
30    pub code_language: Option<String>,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct FragmentElement {
35    pub content: InlineContent,
36    pub fmt_font_family: Option<String>,
37    pub fmt_font_point_size: Option<i64>,
38    pub fmt_font_weight: Option<i64>,
39    pub fmt_font_bold: Option<bool>,
40    pub fmt_font_italic: Option<bool>,
41    pub fmt_font_underline: Option<bool>,
42    pub fmt_font_overline: Option<bool>,
43    pub fmt_font_strikeout: Option<bool>,
44    pub fmt_letter_spacing: Option<i64>,
45    pub fmt_word_spacing: Option<i64>,
46    pub fmt_anchor_href: Option<String>,
47    pub fmt_anchor_names: Vec<String>,
48    pub fmt_is_anchor: Option<bool>,
49    pub fmt_tooltip: Option<String>,
50    pub fmt_underline_style: Option<UnderlineStyle>,
51    pub fmt_vertical_alignment: Option<CharVerticalAlignment>,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct FragmentList {
56    pub style: ListStyle,
57    pub indent: i64,
58    pub prefix: String,
59    pub suffix: String,
60}
61
62impl FragmentElement {
63    pub fn from_entity(e: &InlineElement) -> Self {
64        FragmentElement {
65            content: e.content.clone(),
66            fmt_font_family: e.fmt_font_family.clone(),
67            fmt_font_point_size: e.fmt_font_point_size,
68            fmt_font_weight: e.fmt_font_weight,
69            fmt_font_bold: e.fmt_font_bold,
70            fmt_font_italic: e.fmt_font_italic,
71            fmt_font_underline: e.fmt_font_underline,
72            fmt_font_overline: e.fmt_font_overline,
73            fmt_font_strikeout: e.fmt_font_strikeout,
74            fmt_letter_spacing: e.fmt_letter_spacing,
75            fmt_word_spacing: e.fmt_word_spacing,
76            fmt_anchor_href: e.fmt_anchor_href.clone(),
77            fmt_anchor_names: e.fmt_anchor_names.clone(),
78            fmt_is_anchor: e.fmt_is_anchor,
79            fmt_tooltip: e.fmt_tooltip.clone(),
80            fmt_underline_style: e.fmt_underline_style.clone(),
81            fmt_vertical_alignment: e.fmt_vertical_alignment.clone(),
82        }
83    }
84
85    pub fn to_entity(&self) -> InlineElement {
86        InlineElement {
87            id: 0,
88            created_at: chrono::Utc::now(),
89            updated_at: chrono::Utc::now(),
90            content: self.content.clone(),
91            fmt_font_family: self.fmt_font_family.clone(),
92            fmt_font_point_size: self.fmt_font_point_size,
93            fmt_font_weight: self.fmt_font_weight,
94            fmt_font_bold: self.fmt_font_bold,
95            fmt_font_italic: self.fmt_font_italic,
96            fmt_font_underline: self.fmt_font_underline,
97            fmt_font_overline: self.fmt_font_overline,
98            fmt_font_strikeout: self.fmt_font_strikeout,
99            fmt_letter_spacing: self.fmt_letter_spacing,
100            fmt_word_spacing: self.fmt_word_spacing,
101            fmt_anchor_href: self.fmt_anchor_href.clone(),
102            fmt_anchor_names: self.fmt_anchor_names.clone(),
103            fmt_is_anchor: self.fmt_is_anchor,
104            fmt_tooltip: self.fmt_tooltip.clone(),
105            fmt_underline_style: self.fmt_underline_style.clone(),
106            fmt_vertical_alignment: self.fmt_vertical_alignment.clone(),
107        }
108    }
109}
110
111impl FragmentBlock {
112    /// Returns `true` when this block carries no block-level formatting,
113    /// meaning its content is purely inline.
114    pub fn is_inline_only(&self) -> bool {
115        self.heading_level.is_none()
116            && self.list.is_none()
117            && self.alignment.is_none()
118            && self.indent.unwrap_or(0) == 0
119            && self.text_indent.unwrap_or(0) == 0
120            && self.marker.is_none()
121            && self.top_margin.is_none()
122            && self.bottom_margin.is_none()
123            && self.left_margin.is_none()
124            && self.right_margin.is_none()
125            && self.line_height.is_none()
126            && self.non_breakable_lines.is_none()
127            && self.direction.is_none()
128            && self.background_color.is_none()
129            && self.is_code_block.is_none()
130            && self.code_language.is_none()
131    }
132
133    pub fn from_entity(block: &Block, elements: &[InlineElement], list: Option<&List>) -> Self {
134        FragmentBlock {
135            plain_text: block.plain_text.clone(),
136            elements: elements.iter().map(FragmentElement::from_entity).collect(),
137            heading_level: block.fmt_heading_level,
138            list: list.map(FragmentList::from_entity),
139            alignment: block.fmt_alignment.clone(),
140            indent: block.fmt_indent,
141            text_indent: block.fmt_text_indent,
142            marker: block.fmt_marker.clone(),
143            top_margin: block.fmt_top_margin,
144            bottom_margin: block.fmt_bottom_margin,
145            left_margin: block.fmt_left_margin,
146            right_margin: block.fmt_right_margin,
147            tab_positions: block.fmt_tab_positions.clone(),
148            line_height: block.fmt_line_height,
149            non_breakable_lines: block.fmt_non_breakable_lines,
150            direction: block.fmt_direction.clone(),
151            background_color: block.fmt_background_color.clone(),
152            is_code_block: block.fmt_is_code_block,
153            code_language: block.fmt_code_language.clone(),
154        }
155    }
156}
157
158impl FragmentList {
159    pub fn from_entity(list: &List) -> Self {
160        FragmentList {
161            style: list.style.clone(),
162            indent: list.indent,
163            prefix: list.prefix.clone(),
164            suffix: list.suffix.clone(),
165        }
166    }
167
168    pub fn to_entity(&self) -> List {
169        List {
170            id: 0,
171            created_at: chrono::Utc::now(),
172            updated_at: chrono::Utc::now(),
173            style: self.style.clone(),
174            indent: self.indent,
175            prefix: self.prefix.clone(),
176            suffix: self.suffix.clone(),
177        }
178    }
179}