1use crate::{
7 BlockFormat, BlockInfo, DocumentStats, FindMatch, FindOptions, FrameFormat, ListFormat,
8 TextFormat,
9};
10
11pub fn to_i64(v: usize) -> i64 {
14 debug_assert!(v <= i64::MAX as usize, "position overflow: {v}");
15 v as i64
16}
17
18pub fn to_usize(v: i64) -> usize {
19 assert!(v >= 0, "negative position: {v}");
20 v as usize
21}
22
23impl From<&frontend::document_inspection::DocumentStatsDto> for DocumentStats {
26 fn from(dto: &frontend::document_inspection::DocumentStatsDto) -> Self {
27 Self {
28 character_count: to_usize(dto.character_count),
29 word_count: to_usize(dto.word_count),
30 block_count: to_usize(dto.block_count),
31 frame_count: to_usize(dto.frame_count),
32 image_count: to_usize(dto.image_count),
33 list_count: to_usize(dto.list_count),
34 table_count: to_usize(dto.table_count),
35 }
36 }
37}
38
39impl From<&frontend::document_inspection::BlockInfoDto> for BlockInfo {
42 fn from(dto: &frontend::document_inspection::BlockInfoDto) -> Self {
43 Self {
44 block_id: to_usize(dto.block_id),
45 block_number: to_usize(dto.block_number),
46 start: to_usize(dto.block_start),
47 length: to_usize(dto.block_length),
48 }
49 }
50}
51
52impl FindOptions {
55 pub(crate) fn to_find_text_dto(
56 &self,
57 query: &str,
58 start_position: usize,
59 ) -> frontend::document_search::FindTextDto {
60 frontend::document_search::FindTextDto {
61 query: query.into(),
62 case_sensitive: self.case_sensitive,
63 whole_word: self.whole_word,
64 diacritic_sensitive: self.diacritic_sensitive,
65 language: self.language.clone(),
66 use_regex: self.use_regex,
67 search_backward: self.search_backward,
68 start_position: to_i64(start_position),
69 }
70 }
71
72 pub(crate) fn to_find_all_dto(&self, query: &str) -> frontend::document_search::FindAllDto {
73 frontend::document_search::FindAllDto {
74 query: query.into(),
75 case_sensitive: self.case_sensitive,
76 whole_word: self.whole_word,
77 diacritic_sensitive: self.diacritic_sensitive,
78 language: self.language.clone(),
79 use_regex: self.use_regex,
80 }
81 }
82}
83
84impl crate::ReplaceOptions {
85 pub(crate) fn to_replace_ranges_dto(
89 &self,
90 ranges: &[crate::ReplaceRange],
91 ) -> frontend::document_search::ReplaceRangesDto {
92 let mut positions = Vec::with_capacity(ranges.len());
93 let mut lengths = Vec::with_capacity(ranges.len());
94 let mut replacements = Vec::with_capacity(ranges.len());
95 for range in ranges {
96 positions.push(to_i64(range.position));
97 lengths.push(to_i64(range.length));
98 replacements.push(range.replacement.clone());
99 }
100 frontend::document_search::ReplaceRangesDto {
101 positions,
102 lengths,
103 replacements,
104 format_policy: self.format_policy,
105 }
106 }
107
108 pub(crate) fn to_replace_dto(
109 &self,
110 query: &str,
111 replacement: &str,
112 replace_all: bool,
113 ) -> frontend::document_search::ReplaceTextDto {
114 frontend::document_search::ReplaceTextDto {
115 query: query.into(),
116 replacement: replacement.into(),
117 case_sensitive: self.find.case_sensitive,
118 whole_word: self.find.whole_word,
119 diacritic_sensitive: self.find.diacritic_sensitive,
120 language: self.find.language.clone(),
121 use_regex: self.find.use_regex,
122 replace_all,
123 format_policy: self.format_policy,
124 }
125 }
126}
127
128pub fn find_result_to_match(dto: &frontend::document_search::FindResultDto) -> Option<FindMatch> {
129 if dto.found {
130 Some(FindMatch {
131 position: to_usize(dto.position),
132 length: to_usize(dto.length),
133 matched_text: dto.matched_text.clone(),
134 })
135 } else {
136 None
137 }
138}
139
140pub fn find_all_to_matches(dto: &frontend::document_search::FindAllResultDto) -> Vec<FindMatch> {
144 debug_assert_eq!(dto.positions.len(), dto.lengths.len());
145 debug_assert_eq!(dto.positions.len(), dto.matched_texts.len());
146 dto.positions
147 .iter()
148 .zip(dto.lengths.iter())
149 .zip(dto.matched_texts.iter())
150 .map(|((&pos, &len), text)| FindMatch {
151 position: to_usize(pos),
152 length: to_usize(len),
153 matched_text: text.clone(),
154 })
155 .collect()
156}
157
158use frontend::document_formatting::dtos as fmt_dto;
167
168fn underline_style_to_dto(v: &crate::UnderlineStyle) -> fmt_dto::UnderlineStyle {
169 match v {
170 crate::UnderlineStyle::NoUnderline => fmt_dto::UnderlineStyle::NoUnderline,
171 crate::UnderlineStyle::SingleUnderline => fmt_dto::UnderlineStyle::SingleUnderline,
172 crate::UnderlineStyle::DashUnderline => fmt_dto::UnderlineStyle::DashUnderline,
173 crate::UnderlineStyle::DotLine => fmt_dto::UnderlineStyle::DotLine,
174 crate::UnderlineStyle::DashDotLine => fmt_dto::UnderlineStyle::DashDotLine,
175 crate::UnderlineStyle::DashDotDotLine => fmt_dto::UnderlineStyle::DashDotDotLine,
176 crate::UnderlineStyle::WaveUnderline => fmt_dto::UnderlineStyle::WaveUnderline,
177 crate::UnderlineStyle::SpellCheckUnderline => fmt_dto::UnderlineStyle::SpellCheckUnderline,
178 }
179}
180
181fn vertical_alignment_to_dto(v: &crate::CharVerticalAlignment) -> fmt_dto::CharVerticalAlignment {
182 match v {
183 crate::CharVerticalAlignment::Normal => fmt_dto::CharVerticalAlignment::Normal,
184 crate::CharVerticalAlignment::SuperScript => fmt_dto::CharVerticalAlignment::SuperScript,
185 crate::CharVerticalAlignment::SubScript => fmt_dto::CharVerticalAlignment::SubScript,
186 crate::CharVerticalAlignment::Middle => fmt_dto::CharVerticalAlignment::Middle,
187 crate::CharVerticalAlignment::Bottom => fmt_dto::CharVerticalAlignment::Bottom,
188 crate::CharVerticalAlignment::Top => fmt_dto::CharVerticalAlignment::Top,
189 crate::CharVerticalAlignment::Baseline => fmt_dto::CharVerticalAlignment::Baseline,
190 }
191}
192
193fn alignment_to_dto(v: &crate::Alignment) -> fmt_dto::Alignment {
194 match v {
195 crate::Alignment::Left => fmt_dto::Alignment::Left,
196 crate::Alignment::Right => fmt_dto::Alignment::Right,
197 crate::Alignment::Center => fmt_dto::Alignment::Center,
198 crate::Alignment::Justify => fmt_dto::Alignment::Justify,
199 }
200}
201
202fn marker_to_dto(v: &crate::MarkerType) -> fmt_dto::MarkerType {
203 match v {
204 crate::MarkerType::NoMarker => fmt_dto::MarkerType::NoMarker,
205 crate::MarkerType::Unchecked => fmt_dto::MarkerType::Unchecked,
206 crate::MarkerType::Checked => fmt_dto::MarkerType::Checked,
207 }
208}
209
210impl TextFormat {
216 pub(crate) fn to_set_dto(
217 &self,
218 position: usize,
219 anchor: usize,
220 ) -> frontend::document_formatting::SetTextFormatDto {
221 frontend::document_formatting::SetTextFormatDto {
222 position: to_i64(position),
223 anchor: to_i64(anchor),
224 font_family: self.font_family.clone(),
225 font_point_size: self.font_point_size.map(|v| v as i64),
226 font_weight: self.font_weight.map(|v| v as i64),
227 font_bold: self.font_bold,
228 font_italic: self.font_italic,
229 font_underline: self.font_underline,
230 font_overline: self.font_overline,
231 font_strikeout: self.font_strikeout,
232 letter_spacing: self.letter_spacing.map(|v| v as i64),
233 word_spacing: self.word_spacing.map(|v| v as i64),
234 underline_style: self.underline_style.as_ref().map(underline_style_to_dto),
235 vertical_alignment: self
236 .vertical_alignment
237 .as_ref()
238 .map(vertical_alignment_to_dto),
239 }
240 }
241
242 pub(crate) fn to_merge_dto(
243 &self,
244 position: usize,
245 anchor: usize,
246 ) -> frontend::document_formatting::MergeTextFormatDto {
247 frontend::document_formatting::MergeTextFormatDto {
248 position: to_i64(position),
249 anchor: to_i64(anchor),
250 font_family: self.font_family.clone(),
251 font_bold: self.font_bold,
252 font_italic: self.font_italic,
253 font_underline: self.font_underline,
254 font_strikeout: self.font_strikeout,
255 }
256 }
257}
258
259impl From<&frontend::common::format_runs::CharacterFormat> for TextFormat {
262 fn from(fmt: &frontend::common::format_runs::CharacterFormat) -> Self {
263 Self {
264 font_family: fmt.font_family.clone(),
265 font_point_size: fmt.font_point_size.map(|v| v as u32),
266 font_weight: fmt.font_weight.map(|v| v as u32),
267 font_bold: fmt.font_bold,
268 font_italic: fmt.font_italic,
269 font_underline: fmt.font_underline,
270 font_overline: fmt.font_overline,
271 font_strikeout: fmt.font_strikeout,
272 letter_spacing: fmt.letter_spacing.map(|v| v as i32),
273 word_spacing: fmt.word_spacing.map(|v| v as i32),
274 underline_style: fmt.underline_style.clone(),
275 vertical_alignment: fmt.vertical_alignment.clone(),
276 anchor_href: fmt.anchor_href.clone(),
277 anchor_names: fmt.anchor_names.clone(),
278 is_anchor: fmt.is_anchor,
279 tooltip: fmt.tooltip.clone(),
280 foreground_color: None,
281 background_color: None,
282 underline_color: None,
283 }
284 }
285}
286
287impl BlockFormat {
290 pub(crate) fn to_set_dto(
291 &self,
292 position: usize,
293 anchor: usize,
294 ) -> frontend::document_formatting::SetBlockFormatDto {
295 frontend::document_formatting::SetBlockFormatDto {
296 position: to_i64(position),
297 anchor: to_i64(anchor),
298 alignment: self.alignment.as_ref().map(alignment_to_dto),
299 heading_level: self.heading_level.map(|v| v as i64),
300 indent: self.indent.map(|v| v as i64),
301 marker: self.marker.as_ref().map(marker_to_dto),
302 line_height: self.line_height.map(|v| (v * 1000.0) as i64),
303 non_breakable_lines: self.non_breakable_lines,
304 direction: self.direction.clone(),
305 background_color: self.background_color.clone(),
306 is_code_block: self.is_code_block,
307 code_language: self.code_language.clone(),
308 hyphenate: self.hyphenate,
309 language: self.language.clone(),
310 top_margin: self.top_margin.map(|v| v as i64),
311 bottom_margin: self.bottom_margin.map(|v| v as i64),
312 left_margin: self.left_margin.map(|v| v as i64),
313 right_margin: self.right_margin.map(|v| v as i64),
314 text_indent: self.text_indent.map(|v| v as i64),
315 }
316 }
317}
318
319impl From<&frontend::block::dtos::BlockDto> for BlockFormat {
320 fn from(b: &frontend::block::dtos::BlockDto) -> Self {
321 Self {
322 alignment: b.fmt_alignment.clone(),
323 top_margin: b.fmt_top_margin.map(|v| v as i32),
324 bottom_margin: b.fmt_bottom_margin.map(|v| v as i32),
325 left_margin: b.fmt_left_margin.map(|v| v as i32),
326 right_margin: b.fmt_right_margin.map(|v| v as i32),
327 heading_level: b.fmt_heading_level.map(|v| v as u8),
328 indent: b.fmt_indent.map(|v| v as u8),
329 text_indent: b.fmt_text_indent.map(|v| v as i32),
330 marker: b.fmt_marker.clone(),
331 tab_positions: b.fmt_tab_positions.iter().map(|&v| v as i32).collect(),
332 line_height: b.fmt_line_height.map(|v| v as f32 / 1000.0),
333 non_breakable_lines: b.fmt_non_breakable_lines,
334 direction: b.fmt_direction.clone(),
335 background_color: b.fmt_background_color.clone(),
336 is_code_block: b.fmt_is_code_block,
337 code_language: b.fmt_code_language.clone(),
338 hyphenate: b.fmt_hyphenate,
339 language: b.fmt_language.clone(),
340 }
341 }
342}
343
344impl FrameFormat {
347 pub(crate) fn to_set_dto(
348 &self,
349 position: usize,
350 anchor: usize,
351 frame_id: usize,
352 ) -> frontend::document_formatting::SetFrameFormatDto {
353 frontend::document_formatting::SetFrameFormatDto {
354 position: to_i64(position),
355 anchor: to_i64(anchor),
356 frame_id: to_i64(frame_id),
357 height: self.height.map(|v| v as i64),
358 width: self.width.map(|v| v as i64),
359 top_margin: self.top_margin.map(|v| v as i64),
360 bottom_margin: self.bottom_margin.map(|v| v as i64),
361 left_margin: self.left_margin.map(|v| v as i64),
362 right_margin: self.right_margin.map(|v| v as i64),
363 padding: self.padding.map(|v| v as i64),
364 border: self.border.map(|v| v as i64),
365 is_blockquote: self.is_blockquote,
366 }
367 }
368}
369
370impl ListFormat {
373 pub(crate) fn to_set_dto(
374 &self,
375 list_id: usize,
376 ) -> frontend::document_formatting::SetListFormatDto {
377 frontend::document_formatting::SetListFormatDto {
378 list_id: to_i64(list_id),
379 style: self.style.clone(),
380 indent: self.indent.map(|v| v as i64),
381 prefix: self.prefix.clone(),
382 suffix: self.suffix.clone(),
383 }
384 }
385}
386
387impl crate::flow::TableFormat {
390 pub(crate) fn to_set_dto(
391 &self,
392 table_id: usize,
393 ) -> frontend::document_formatting::SetTableFormatDto {
394 frontend::document_formatting::SetTableFormatDto {
395 table_id: to_i64(table_id),
396 border: self.border.map(|v| v as i64),
397 cell_spacing: self.cell_spacing.map(|v| v as i64),
398 cell_padding: self.cell_padding.map(|v| v as i64),
399 width: self.width.map(|v| v as i64),
400 alignment: self.alignment.as_ref().map(alignment_to_dto),
401 }
402 }
403}
404
405fn cell_vertical_alignment_to_dto(
408 v: &crate::flow::CellVerticalAlignment,
409) -> fmt_dto::CellVerticalAlignment {
410 match v {
411 crate::flow::CellVerticalAlignment::Top => fmt_dto::CellVerticalAlignment::Top,
412 crate::flow::CellVerticalAlignment::Middle => fmt_dto::CellVerticalAlignment::Middle,
413 crate::flow::CellVerticalAlignment::Bottom => fmt_dto::CellVerticalAlignment::Bottom,
414 }
415}
416
417impl crate::flow::CellFormat {
418 pub(crate) fn to_set_dto(
419 &self,
420 cell_id: usize,
421 ) -> frontend::document_formatting::SetTableCellFormatDto {
422 frontend::document_formatting::SetTableCellFormatDto {
423 cell_id: to_i64(cell_id),
424 padding: self.padding.map(|v| v as i64),
425 border: self.border.map(|v| v as i64),
426 vertical_alignment: self
427 .vertical_alignment
428 .as_ref()
429 .map(cell_vertical_alignment_to_dto),
430 background_color: self.background_color.clone(),
431 }
432 }
433}