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 use_regex: self.use_regex,
65 search_backward: self.search_backward,
66 start_position: to_i64(start_position),
67 }
68 }
69
70 pub(crate) fn to_find_all_dto(&self, query: &str) -> frontend::document_search::FindAllDto {
71 frontend::document_search::FindAllDto {
72 query: query.into(),
73 case_sensitive: self.case_sensitive,
74 whole_word: self.whole_word,
75 use_regex: self.use_regex,
76 }
77 }
78
79 pub(crate) fn to_replace_dto(
80 &self,
81 query: &str,
82 replacement: &str,
83 replace_all: bool,
84 ) -> frontend::document_search::ReplaceTextDto {
85 frontend::document_search::ReplaceTextDto {
86 query: query.into(),
87 replacement: replacement.into(),
88 case_sensitive: self.case_sensitive,
89 whole_word: self.whole_word,
90 use_regex: self.use_regex,
91 replace_all,
92 }
93 }
94}
95
96pub fn find_result_to_match(dto: &frontend::document_search::FindResultDto) -> Option<FindMatch> {
97 if dto.found {
98 Some(FindMatch {
99 position: to_usize(dto.position),
100 length: to_usize(dto.length),
101 })
102 } else {
103 None
104 }
105}
106
107pub fn find_all_to_matches(dto: &frontend::document_search::FindAllResultDto) -> Vec<FindMatch> {
108 dto.positions
109 .iter()
110 .zip(dto.lengths.iter())
111 .map(|(&pos, &len)| FindMatch {
112 position: to_usize(pos),
113 length: to_usize(len),
114 })
115 .collect()
116}
117
118use frontend::document_formatting::dtos as fmt_dto;
127
128fn underline_style_to_dto(v: &crate::UnderlineStyle) -> fmt_dto::UnderlineStyle {
129 match v {
130 crate::UnderlineStyle::NoUnderline => fmt_dto::UnderlineStyle::NoUnderline,
131 crate::UnderlineStyle::SingleUnderline => fmt_dto::UnderlineStyle::SingleUnderline,
132 crate::UnderlineStyle::DashUnderline => fmt_dto::UnderlineStyle::DashUnderline,
133 crate::UnderlineStyle::DotLine => fmt_dto::UnderlineStyle::DotLine,
134 crate::UnderlineStyle::DashDotLine => fmt_dto::UnderlineStyle::DashDotLine,
135 crate::UnderlineStyle::DashDotDotLine => fmt_dto::UnderlineStyle::DashDotDotLine,
136 crate::UnderlineStyle::WaveUnderline => fmt_dto::UnderlineStyle::WaveUnderline,
137 crate::UnderlineStyle::SpellCheckUnderline => fmt_dto::UnderlineStyle::SpellCheckUnderline,
138 }
139}
140
141fn vertical_alignment_to_dto(v: &crate::CharVerticalAlignment) -> fmt_dto::CharVerticalAlignment {
142 match v {
143 crate::CharVerticalAlignment::Normal => fmt_dto::CharVerticalAlignment::Normal,
144 crate::CharVerticalAlignment::SuperScript => fmt_dto::CharVerticalAlignment::SuperScript,
145 crate::CharVerticalAlignment::SubScript => fmt_dto::CharVerticalAlignment::SubScript,
146 crate::CharVerticalAlignment::Middle => fmt_dto::CharVerticalAlignment::Middle,
147 crate::CharVerticalAlignment::Bottom => fmt_dto::CharVerticalAlignment::Bottom,
148 crate::CharVerticalAlignment::Top => fmt_dto::CharVerticalAlignment::Top,
149 crate::CharVerticalAlignment::Baseline => fmt_dto::CharVerticalAlignment::Baseline,
150 }
151}
152
153fn alignment_to_dto(v: &crate::Alignment) -> fmt_dto::Alignment {
154 match v {
155 crate::Alignment::Left => fmt_dto::Alignment::Left,
156 crate::Alignment::Right => fmt_dto::Alignment::Right,
157 crate::Alignment::Center => fmt_dto::Alignment::Center,
158 crate::Alignment::Justify => fmt_dto::Alignment::Justify,
159 }
160}
161
162fn marker_to_dto(v: &crate::MarkerType) -> fmt_dto::MarkerType {
163 match v {
164 crate::MarkerType::NoMarker => fmt_dto::MarkerType::NoMarker,
165 crate::MarkerType::Unchecked => fmt_dto::MarkerType::Unchecked,
166 crate::MarkerType::Checked => fmt_dto::MarkerType::Checked,
167 }
168}
169
170impl TextFormat {
176 pub(crate) fn to_set_dto(
177 &self,
178 position: usize,
179 anchor: usize,
180 ) -> frontend::document_formatting::SetTextFormatDto {
181 frontend::document_formatting::SetTextFormatDto {
182 position: to_i64(position),
183 anchor: to_i64(anchor),
184 font_family: self.font_family.clone(),
185 font_point_size: self.font_point_size.map(|v| v as i64),
186 font_weight: self.font_weight.map(|v| v as i64),
187 font_bold: self.font_bold,
188 font_italic: self.font_italic,
189 font_underline: self.font_underline,
190 font_overline: self.font_overline,
191 font_strikeout: self.font_strikeout,
192 letter_spacing: self.letter_spacing.map(|v| v as i64),
193 word_spacing: self.word_spacing.map(|v| v as i64),
194 underline_style: self.underline_style.as_ref().map(underline_style_to_dto),
195 vertical_alignment: self
196 .vertical_alignment
197 .as_ref()
198 .map(vertical_alignment_to_dto),
199 }
200 }
201
202 pub(crate) fn to_merge_dto(
203 &self,
204 position: usize,
205 anchor: usize,
206 ) -> frontend::document_formatting::MergeTextFormatDto {
207 frontend::document_formatting::MergeTextFormatDto {
208 position: to_i64(position),
209 anchor: to_i64(anchor),
210 font_family: self.font_family.clone(),
211 font_bold: self.font_bold,
212 font_italic: self.font_italic,
213 font_underline: self.font_underline,
214 }
215 }
216}
217
218impl From<&frontend::inline_element::dtos::InlineElementDto> for TextFormat {
221 fn from(el: &frontend::inline_element::dtos::InlineElementDto) -> Self {
222 Self {
223 font_family: el.fmt_font_family.clone(),
224 font_point_size: el.fmt_font_point_size.map(|v| v as u32),
225 font_weight: el.fmt_font_weight.map(|v| v as u32),
226 font_bold: el.fmt_font_bold,
227 font_italic: el.fmt_font_italic,
228 font_underline: el.fmt_font_underline,
229 font_overline: el.fmt_font_overline,
230 font_strikeout: el.fmt_font_strikeout,
231 letter_spacing: el.fmt_letter_spacing.map(|v| v as i32),
232 word_spacing: el.fmt_word_spacing.map(|v| v as i32),
233 underline_style: el.fmt_underline_style.clone(),
234 vertical_alignment: el.fmt_vertical_alignment.clone(),
235 anchor_href: el.fmt_anchor_href.clone(),
236 anchor_names: el.fmt_anchor_names.clone(),
237 is_anchor: el.fmt_is_anchor,
238 tooltip: el.fmt_tooltip.clone(),
239 foreground_color: None,
240 background_color: None,
241 underline_color: None,
242 }
243 }
244}
245
246impl BlockFormat {
249 pub(crate) fn to_set_dto(
250 &self,
251 position: usize,
252 anchor: usize,
253 ) -> frontend::document_formatting::SetBlockFormatDto {
254 frontend::document_formatting::SetBlockFormatDto {
255 position: to_i64(position),
256 anchor: to_i64(anchor),
257 alignment: self.alignment.as_ref().map(alignment_to_dto),
258 heading_level: self.heading_level.map(|v| v as i64),
259 indent: self.indent.map(|v| v as i64),
260 marker: self.marker.as_ref().map(marker_to_dto),
261 line_height: self.line_height.map(|v| (v * 1000.0) as i64),
262 non_breakable_lines: self.non_breakable_lines,
263 direction: self.direction.clone(),
264 background_color: self.background_color.clone(),
265 }
266 }
267}
268
269impl From<&frontend::block::dtos::BlockDto> for BlockFormat {
270 fn from(b: &frontend::block::dtos::BlockDto) -> Self {
271 Self {
272 alignment: b.fmt_alignment.clone(),
273 top_margin: b.fmt_top_margin.map(|v| v as i32),
274 bottom_margin: b.fmt_bottom_margin.map(|v| v as i32),
275 left_margin: b.fmt_left_margin.map(|v| v as i32),
276 right_margin: b.fmt_right_margin.map(|v| v as i32),
277 heading_level: b.fmt_heading_level.map(|v| v as u8),
278 indent: b.fmt_indent.map(|v| v as u8),
279 text_indent: b.fmt_text_indent.map(|v| v as i32),
280 marker: b.fmt_marker.clone(),
281 tab_positions: b.fmt_tab_positions.iter().map(|&v| v as i32).collect(),
282 line_height: b.fmt_line_height.map(|v| v as f32 / 1000.0),
283 non_breakable_lines: b.fmt_non_breakable_lines,
284 direction: b.fmt_direction.clone(),
285 background_color: b.fmt_background_color.clone(),
286 }
287 }
288}
289
290impl FrameFormat {
293 pub(crate) fn to_set_dto(
294 &self,
295 position: usize,
296 anchor: usize,
297 frame_id: usize,
298 ) -> frontend::document_formatting::SetFrameFormatDto {
299 frontend::document_formatting::SetFrameFormatDto {
300 position: to_i64(position),
301 anchor: to_i64(anchor),
302 frame_id: to_i64(frame_id),
303 height: self.height.map(|v| v as i64),
304 width: self.width.map(|v| v as i64),
305 top_margin: self.top_margin.map(|v| v as i64),
306 bottom_margin: self.bottom_margin.map(|v| v as i64),
307 left_margin: self.left_margin.map(|v| v as i64),
308 right_margin: self.right_margin.map(|v| v as i64),
309 padding: self.padding.map(|v| v as i64),
310 border: self.border.map(|v| v as i64),
311 }
312 }
313}
314
315impl ListFormat {
318 pub(crate) fn to_set_dto(
319 &self,
320 list_id: usize,
321 ) -> frontend::document_formatting::SetListFormatDto {
322 frontend::document_formatting::SetListFormatDto {
323 list_id: to_i64(list_id),
324 style: self.style.clone(),
325 indent: self.indent.map(|v| v as i64),
326 prefix: self.prefix.clone(),
327 suffix: self.suffix.clone(),
328 }
329 }
330}
331
332impl crate::flow::TableFormat {
335 pub(crate) fn to_set_dto(
336 &self,
337 table_id: usize,
338 ) -> frontend::document_formatting::SetTableFormatDto {
339 frontend::document_formatting::SetTableFormatDto {
340 table_id: to_i64(table_id),
341 border: self.border.map(|v| v as i64),
342 cell_spacing: self.cell_spacing.map(|v| v as i64),
343 cell_padding: self.cell_padding.map(|v| v as i64),
344 width: self.width.map(|v| v as i64),
345 alignment: self.alignment.as_ref().map(alignment_to_dto),
346 }
347 }
348}
349
350fn cell_vertical_alignment_to_dto(
353 v: &crate::flow::CellVerticalAlignment,
354) -> fmt_dto::CellVerticalAlignment {
355 match v {
356 crate::flow::CellVerticalAlignment::Top => fmt_dto::CellVerticalAlignment::Top,
357 crate::flow::CellVerticalAlignment::Middle => fmt_dto::CellVerticalAlignment::Middle,
358 crate::flow::CellVerticalAlignment::Bottom => fmt_dto::CellVerticalAlignment::Bottom,
359 }
360}
361
362impl crate::flow::CellFormat {
363 pub(crate) fn to_set_dto(
364 &self,
365 cell_id: usize,
366 ) -> frontend::document_formatting::SetTableCellFormatDto {
367 frontend::document_formatting::SetTableCellFormatDto {
368 cell_id: to_i64(cell_id),
369 padding: self.padding.map(|v| v as i64),
370 border: self.border.map(|v| v as i64),
371 vertical_alignment: self
372 .vertical_alignment
373 .as_ref()
374 .map(cell_vertical_alignment_to_dto),
375 background_color: self.background_color.clone(),
376 }
377 }
378}