Skip to main content

text_document/
convert.rs

1//! Conversion helpers between public API types and backend DTOs.
2//!
3//! The backend uses `i64` for all positions/sizes. The public API uses `usize`.
4//! All Option mapping between public format structs and backend DTOs lives here.
5
6use crate::{
7    BlockFormat, BlockInfo, DocumentStats, FindMatch, FindOptions, FrameFormat, TextFormat,
8};
9
10// ── Position conversion ─────────────────────────────────────────
11
12pub fn to_i64(v: usize) -> i64 {
13    debug_assert!(
14        v <= i64::MAX as usize,
15        "position overflow: {v}"
16    );
17    v as i64
18}
19
20pub fn to_usize(v: i64) -> usize {
21    assert!(v >= 0, "negative position: {v}");
22    v as usize
23}
24
25// ── DocumentStats ───────────────────────────────────────────────
26
27impl From<&frontend::document_inspection::DocumentStatsDto> for DocumentStats {
28    fn from(dto: &frontend::document_inspection::DocumentStatsDto) -> Self {
29        Self {
30            character_count: to_usize(dto.character_count),
31            word_count: to_usize(dto.word_count),
32            block_count: to_usize(dto.block_count),
33            frame_count: to_usize(dto.frame_count),
34            image_count: to_usize(dto.image_count),
35            list_count: to_usize(dto.list_count),
36        }
37    }
38}
39
40// ── BlockInfo ───────────────────────────────────────────────────
41
42impl From<&frontend::document_inspection::BlockInfoDto> for BlockInfo {
43    fn from(dto: &frontend::document_inspection::BlockInfoDto) -> Self {
44        Self {
45            block_id: to_usize(dto.block_id),
46            block_number: to_usize(dto.block_number),
47            start: to_usize(dto.block_start),
48            length: to_usize(dto.block_length),
49        }
50    }
51}
52
53// ── FindMatch / FindOptions ─────────────────────────────────────
54
55impl FindOptions {
56    pub(crate) fn to_find_text_dto(
57        &self,
58        query: &str,
59        start_position: usize,
60    ) -> frontend::document_search::FindTextDto {
61        frontend::document_search::FindTextDto {
62            query: query.into(),
63            case_sensitive: self.case_sensitive,
64            whole_word: self.whole_word,
65            use_regex: self.use_regex,
66            search_backward: self.search_backward,
67            start_position: to_i64(start_position),
68        }
69    }
70
71    pub(crate) fn to_find_all_dto(&self, query: &str) -> frontend::document_search::FindAllDto {
72        frontend::document_search::FindAllDto {
73            query: query.into(),
74            case_sensitive: self.case_sensitive,
75            whole_word: self.whole_word,
76            use_regex: self.use_regex,
77        }
78    }
79
80    pub(crate) fn to_replace_dto(
81        &self,
82        query: &str,
83        replacement: &str,
84        replace_all: bool,
85    ) -> frontend::document_search::ReplaceTextDto {
86        frontend::document_search::ReplaceTextDto {
87            query: query.into(),
88            replacement: replacement.into(),
89            case_sensitive: self.case_sensitive,
90            whole_word: self.whole_word,
91            use_regex: self.use_regex,
92            replace_all,
93        }
94    }
95}
96
97pub fn find_result_to_match(dto: &frontend::document_search::FindResultDto) -> Option<FindMatch> {
98    if dto.found {
99        Some(FindMatch {
100            position: to_usize(dto.position),
101            length: to_usize(dto.length),
102        })
103    } else {
104        None
105    }
106}
107
108pub fn find_all_to_matches(dto: &frontend::document_search::FindAllResultDto) -> Vec<FindMatch> {
109    dto.positions
110        .iter()
111        .zip(dto.lengths.iter())
112        .map(|(&pos, &len)| FindMatch {
113            position: to_usize(pos),
114            length: to_usize(len),
115        })
116        .collect()
117}
118
119// ── Domain ↔ DTO enum conversions ───────────────────────────────
120//
121// The DTO layer has its own enum types, separate from domain enums
122// in `common::entities`. This keeps the API boundary stable even
123// when domain internals change.
124
125// Formatting DTOs have their own enum types (separate from entity DTO enums).
126// These conversion functions bridge the two at the public API boundary.
127use frontend::document_formatting::dtos as fmt_dto;
128
129fn underline_style_to_dto(v: &crate::UnderlineStyle) -> fmt_dto::UnderlineStyle {
130    match v {
131        crate::UnderlineStyle::NoUnderline => fmt_dto::UnderlineStyle::NoUnderline,
132        crate::UnderlineStyle::SingleUnderline => fmt_dto::UnderlineStyle::SingleUnderline,
133        crate::UnderlineStyle::DashUnderline => fmt_dto::UnderlineStyle::DashUnderline,
134        crate::UnderlineStyle::DotLine => fmt_dto::UnderlineStyle::DotLine,
135        crate::UnderlineStyle::DashDotLine => fmt_dto::UnderlineStyle::DashDotLine,
136        crate::UnderlineStyle::DashDotDotLine => fmt_dto::UnderlineStyle::DashDotDotLine,
137        crate::UnderlineStyle::WaveUnderline => fmt_dto::UnderlineStyle::WaveUnderline,
138        crate::UnderlineStyle::SpellCheckUnderline => fmt_dto::UnderlineStyle::SpellCheckUnderline,
139    }
140}
141
142fn vertical_alignment_to_dto(v: &crate::CharVerticalAlignment) -> fmt_dto::CharVerticalAlignment {
143    match v {
144        crate::CharVerticalAlignment::Normal => fmt_dto::CharVerticalAlignment::Normal,
145        crate::CharVerticalAlignment::SuperScript => fmt_dto::CharVerticalAlignment::SuperScript,
146        crate::CharVerticalAlignment::SubScript => fmt_dto::CharVerticalAlignment::SubScript,
147        crate::CharVerticalAlignment::Middle => fmt_dto::CharVerticalAlignment::Middle,
148        crate::CharVerticalAlignment::Bottom => fmt_dto::CharVerticalAlignment::Bottom,
149        crate::CharVerticalAlignment::Top => fmt_dto::CharVerticalAlignment::Top,
150        crate::CharVerticalAlignment::Baseline => fmt_dto::CharVerticalAlignment::Baseline,
151    }
152}
153
154fn alignment_to_dto(v: &crate::Alignment) -> fmt_dto::Alignment {
155    match v {
156        crate::Alignment::Left => fmt_dto::Alignment::Left,
157        crate::Alignment::Right => fmt_dto::Alignment::Right,
158        crate::Alignment::Center => fmt_dto::Alignment::Center,
159        crate::Alignment::Justify => fmt_dto::Alignment::Justify,
160    }
161}
162
163fn marker_to_dto(v: &crate::MarkerType) -> fmt_dto::MarkerType {
164    match v {
165        crate::MarkerType::NoMarker => fmt_dto::MarkerType::NoMarker,
166        crate::MarkerType::Unchecked => fmt_dto::MarkerType::Unchecked,
167        crate::MarkerType::Checked => fmt_dto::MarkerType::Checked,
168    }
169}
170
171// ── TextFormat → SetTextFormatDto ───────────────────────────────
172//
173// Backend DTOs now use `Option` fields: `None` means "don't change
174// this property" and `Some(value)` means "set to value".
175
176impl TextFormat {
177    pub(crate) fn to_set_dto(
178        &self,
179        position: usize,
180        anchor: usize,
181    ) -> frontend::document_formatting::SetTextFormatDto {
182        frontend::document_formatting::SetTextFormatDto {
183            position: to_i64(position),
184            anchor: to_i64(anchor),
185            font_family: self.font_family.clone(),
186            font_point_size: self.font_point_size.map(|v| v as i64),
187            font_weight: self.font_weight.map(|v| v as i64),
188            font_bold: self.font_bold,
189            font_italic: self.font_italic,
190            font_underline: self.font_underline,
191            font_overline: self.font_overline,
192            font_strikeout: self.font_strikeout,
193            letter_spacing: self.letter_spacing.map(|v| v as i64),
194            word_spacing: self.word_spacing.map(|v| v as i64),
195            underline_style: self.underline_style.as_ref().map(underline_style_to_dto),
196            vertical_alignment: self.vertical_alignment.as_ref().map(vertical_alignment_to_dto),
197        }
198    }
199
200    pub(crate) fn to_merge_dto(
201        &self,
202        position: usize,
203        anchor: usize,
204    ) -> frontend::document_formatting::MergeTextFormatDto {
205        frontend::document_formatting::MergeTextFormatDto {
206            position: to_i64(position),
207            anchor: to_i64(anchor),
208            font_family: self.font_family.clone(),
209            font_bold: self.font_bold,
210            font_italic: self.font_italic,
211            font_underline: self.font_underline,
212        }
213    }
214}
215
216// ── InlineElement entity → TextFormat ───────────────────────────
217
218impl From<&frontend::inline_element::dtos::InlineElementDto> for TextFormat {
219    fn from(el: &frontend::inline_element::dtos::InlineElementDto) -> Self {
220        Self {
221            font_family: el.fmt_font_family.clone(),
222            font_point_size: el.fmt_font_point_size.map(|v| v as u32),
223            font_weight: el.fmt_font_weight.map(|v| v as u32),
224            font_bold: el.fmt_font_bold,
225            font_italic: el.fmt_font_italic,
226            font_underline: el.fmt_font_underline,
227            font_overline: el.fmt_font_overline,
228            font_strikeout: el.fmt_font_strikeout,
229            letter_spacing: el.fmt_letter_spacing.map(|v| v as i32),
230            word_spacing: el.fmt_word_spacing.map(|v| v as i32),
231            underline_style: el.fmt_underline_style.clone(),
232            vertical_alignment: el.fmt_vertical_alignment.clone(),
233            anchor_href: el.fmt_anchor_href.clone(),
234            anchor_names: el.fmt_anchor_names.clone(),
235            is_anchor: el.fmt_is_anchor,
236            tooltip: el.fmt_tooltip.clone(),
237        }
238    }
239}
240
241// ── BlockFormat ─────────────────────────────────────────────────
242
243impl BlockFormat {
244    pub(crate) fn to_set_dto(
245        &self,
246        position: usize,
247        anchor: usize,
248    ) -> frontend::document_formatting::SetBlockFormatDto {
249        frontend::document_formatting::SetBlockFormatDto {
250            position: to_i64(position),
251            anchor: to_i64(anchor),
252            alignment: self.alignment.as_ref().map(alignment_to_dto),
253            heading_level: self.heading_level.map(|v| v as i64),
254            indent: self.indent.map(|v| v as i64),
255            marker: self.marker.as_ref().map(marker_to_dto),
256        }
257    }
258}
259
260impl From<&frontend::block::dtos::BlockDto> for BlockFormat {
261    fn from(b: &frontend::block::dtos::BlockDto) -> Self {
262        Self {
263            alignment: b.fmt_alignment.clone(),
264            top_margin: b.fmt_top_margin.map(|v| v as i32),
265            bottom_margin: b.fmt_bottom_margin.map(|v| v as i32),
266            left_margin: b.fmt_left_margin.map(|v| v as i32),
267            right_margin: b.fmt_right_margin.map(|v| v as i32),
268            heading_level: b.fmt_heading_level.map(|v| v as u8),
269            indent: b.fmt_indent.map(|v| v as u8),
270            text_indent: b.fmt_text_indent.map(|v| v as i32),
271            marker: b.fmt_marker.clone(),
272            tab_positions: b.fmt_tab_positions.iter().map(|&v| v as i32).collect(),
273        }
274    }
275}
276
277// ── FrameFormat ─────────────────────────────────────────────────
278
279impl FrameFormat {
280    pub(crate) fn to_set_dto(
281        &self,
282        position: usize,
283        anchor: usize,
284        frame_id: usize,
285    ) -> frontend::document_formatting::SetFrameFormatDto {
286        frontend::document_formatting::SetFrameFormatDto {
287            position: to_i64(position),
288            anchor: to_i64(anchor),
289            frame_id: to_i64(frame_id),
290            height: self.height.map(|v| v as i64),
291            width: self.width.map(|v| v as i64),
292            top_margin: self.top_margin.map(|v| v as i64),
293            bottom_margin: self.bottom_margin.map(|v| v as i64),
294            left_margin: self.left_margin.map(|v| v as i64),
295            right_margin: self.right_margin.map(|v| v as i64),
296            padding: self.padding.map(|v| v as i64),
297            border: self.border.map(|v| v as i64),
298        }
299    }
300}