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