Skip to main content

common/parser_tools/
fragment_schema.rs

1use serde::{Deserialize, Serialize};
2
3use crate::entities::*;
4use crate::format_runs::{InlineContent, InlineSegment};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct FragmentData {
8    pub blocks: Vec<FragmentBlock>,
9    /// Table fragments extracted from cell selections. Empty for text-only fragments.
10    #[serde(default, skip_serializing_if = "Vec::is_empty")]
11    pub tables: Vec<FragmentTable>,
12}
13
14/// A table (or rectangular sub-region) captured from a cell selection.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct FragmentTable {
17    pub rows: usize,
18    pub columns: usize,
19    pub cells: Vec<FragmentTableCell>,
20    /// Index into the parent `FragmentData::blocks` at which this table
21    /// should be inserted.  Blocks `[0..index)` come before the table,
22    /// blocks `[index..]` come after.  Default `0` for backward compat.
23    #[serde(default)]
24    pub block_insert_index: usize,
25    // ── Table-level formatting ────────────────────────────────────
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub fmt_border: Option<i64>,
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub fmt_cell_spacing: Option<i64>,
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub fmt_cell_padding: Option<i64>,
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub fmt_width: Option<i64>,
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub fmt_alignment: Option<Alignment>,
36    #[serde(default, skip_serializing_if = "Vec::is_empty")]
37    pub column_widths: Vec<i64>,
38}
39
40/// One cell within a [`FragmentTable`].
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct FragmentTableCell {
43    pub row: usize,
44    pub column: usize,
45    pub row_span: usize,
46    pub column_span: usize,
47    pub blocks: Vec<FragmentBlock>,
48    // ── Cell-level formatting ─────────────────────────────────────
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub fmt_padding: Option<i64>,
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub fmt_border: Option<i64>,
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub fmt_vertical_alignment: Option<CellVerticalAlignment>,
55    #[serde(default, skip_serializing_if = "Option::is_none")]
56    pub fmt_background_color: Option<String>,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct FragmentBlock {
61    pub plain_text: String,
62    pub elements: Vec<FragmentElement>,
63    pub heading_level: Option<i64>,
64    pub list: Option<FragmentList>,
65    pub alignment: Option<Alignment>,
66    pub indent: Option<i64>,
67    pub text_indent: Option<i64>,
68    pub marker: Option<MarkerType>,
69    pub top_margin: Option<i64>,
70    pub bottom_margin: Option<i64>,
71    pub left_margin: Option<i64>,
72    pub right_margin: Option<i64>,
73    pub tab_positions: Vec<i64>,
74    pub line_height: Option<i64>,
75    pub non_breakable_lines: Option<bool>,
76    pub direction: Option<TextDirection>,
77    pub background_color: Option<String>,
78    pub is_code_block: Option<bool>,
79    pub code_language: Option<String>,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct FragmentElement {
84    pub content: InlineContent,
85    pub fmt_font_family: Option<String>,
86    pub fmt_font_point_size: Option<i64>,
87    pub fmt_font_weight: Option<i64>,
88    pub fmt_font_bold: Option<bool>,
89    pub fmt_font_italic: Option<bool>,
90    pub fmt_font_underline: Option<bool>,
91    pub fmt_font_overline: Option<bool>,
92    pub fmt_font_strikeout: Option<bool>,
93    pub fmt_letter_spacing: Option<i64>,
94    pub fmt_word_spacing: Option<i64>,
95    pub fmt_anchor_href: Option<String>,
96    pub fmt_anchor_names: Vec<String>,
97    pub fmt_is_anchor: Option<bool>,
98    pub fmt_tooltip: Option<String>,
99    pub fmt_underline_style: Option<UnderlineStyle>,
100    pub fmt_vertical_alignment: Option<CharVerticalAlignment>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct FragmentList {
105    pub style: ListStyle,
106    pub indent: i64,
107    pub prefix: String,
108    pub suffix: String,
109}
110
111impl FragmentElement {
112    pub fn from_segment(seg: &InlineSegment) -> Self {
113        FragmentElement {
114            content: seg.content.clone(),
115            fmt_font_family: seg.fmt_font_family.clone(),
116            fmt_font_point_size: seg.fmt_font_point_size,
117            fmt_font_weight: seg.fmt_font_weight,
118            fmt_font_bold: seg.fmt_font_bold,
119            fmt_font_italic: seg.fmt_font_italic,
120            fmt_font_underline: seg.fmt_font_underline,
121            fmt_font_overline: seg.fmt_font_overline,
122            fmt_font_strikeout: seg.fmt_font_strikeout,
123            fmt_letter_spacing: seg.fmt_letter_spacing,
124            fmt_word_spacing: seg.fmt_word_spacing,
125            fmt_anchor_href: seg.fmt_anchor_href.clone(),
126            fmt_anchor_names: seg.fmt_anchor_names.clone(),
127            fmt_is_anchor: seg.fmt_is_anchor,
128            fmt_tooltip: seg.fmt_tooltip.clone(),
129            fmt_underline_style: seg.fmt_underline_style.clone(),
130            fmt_vertical_alignment: seg.fmt_vertical_alignment.clone(),
131        }
132    }
133}
134
135impl FragmentBlock {
136    /// Returns `true` when this block carries no block-level formatting,
137    /// meaning its content is purely inline.
138    pub fn is_inline_only(&self) -> bool {
139        self.heading_level.is_none()
140            && self.list.is_none()
141            && self.alignment.is_none()
142            && self.indent.unwrap_or(0) == 0
143            && self.text_indent.unwrap_or(0) == 0
144            && self.marker.is_none()
145            && self.top_margin.is_none()
146            && self.bottom_margin.is_none()
147            && self.left_margin.is_none()
148            && self.right_margin.is_none()
149            && self.line_height.is_none()
150            && self.non_breakable_lines.is_none()
151            && self.direction.is_none()
152            && self.background_color.is_none()
153            && self.is_code_block.is_none()
154            && self.code_language.is_none()
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}