text-document-io 1.11.1

Import/export for text-document: plain text, Markdown, HTML, LaTeX, DOCX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Generated by Qleany v1.5.1 from feature_controller.tera

use crate::ExportDjotDto;
use crate::ExportDocxDto;
use crate::ExportDocxResultDto;
use crate::ExportEpubDto;
use crate::ExportEpubResultDto;
use crate::ExportHtmlDto;
use crate::ExportLatexDto;
use crate::ExportLatexResultDto;
use crate::ExportMarkdownDto;
use crate::ExportOdtDto;
use crate::ExportOdtResultDto;
use crate::ExportPdfDto;
use crate::ExportPdfResultDto;
use crate::ExportPlainTextDto;
use crate::ImportDjotDto;
use crate::ImportDjotResultDto;
use crate::ImportHtmlDto;
use crate::ImportHtmlResultDto;
use crate::ImportMarkdownDto;
use crate::ImportMarkdownResultDto;
use crate::ImportPlainTextDto;
use crate::units_of_work::export_djot_uow::ExportDjotUnitOfWorkFactory;
use crate::units_of_work::export_docx_uow::ExportDocxUnitOfWorkFactory;
use crate::units_of_work::export_epub_uow::ExportEpubUnitOfWorkFactory;
use crate::units_of_work::export_html_uow::ExportHtmlUnitOfWorkFactory;
use crate::units_of_work::export_latex_uow::ExportLatexUnitOfWorkFactory;
use crate::units_of_work::export_markdown_uow::ExportMarkdownUnitOfWorkFactory;
use crate::units_of_work::export_odt_uow::ExportOdtUnitOfWorkFactory;
#[cfg(feature = "pdf")]
use crate::units_of_work::export_pdf_uow::ExportPdfUnitOfWorkFactory;
use crate::units_of_work::export_plain_text_uow::ExportPlainTextUnitOfWorkFactory;
use crate::units_of_work::import_djot_uow::ImportDjotUnitOfWorkFactory;
use crate::units_of_work::import_html_uow::ImportHtmlUnitOfWorkFactory;
use crate::units_of_work::import_markdown_uow::ImportMarkdownUnitOfWorkFactory;
use crate::units_of_work::import_plain_text_uow::ImportPlainTextUnitOfWorkFactory;
use crate::use_cases::export_djot_uc::ExportDjotUseCase;
use crate::use_cases::export_docx_uc::ExportDocxUseCase;
use crate::use_cases::export_epub_uc::ExportEpubUseCase;
use crate::use_cases::export_html_uc::ExportHtmlUseCase;
use crate::use_cases::export_latex_uc::ExportLatexUseCase;
use crate::use_cases::export_markdown_uc::ExportMarkdownUseCase;
use crate::use_cases::export_odt_uc::ExportOdtUseCase;
#[cfg(feature = "pdf")]
use crate::use_cases::export_pdf_uc::ExportPdfUseCase;
use crate::use_cases::export_plain_text_uc::ExportPlainTextUseCase;
use crate::use_cases::import_djot_uc::ImportDjotUseCase;
use crate::use_cases::import_html_uc::ImportHtmlUseCase;
use crate::use_cases::import_markdown_uc::ImportMarkdownUseCase;
use crate::use_cases::import_plain_text_uc::ImportPlainTextUseCase;
use anyhow::Result;
use common::event::{Event, Origin};
use common::parser_tools::DjotExportOptions;

use common::event::DocumentIoEvent::ExportDjot;
use common::event::DocumentIoEvent::ExportHtml;
use common::event::DocumentIoEvent::ExportLatex;
use common::event::DocumentIoEvent::ExportMarkdown;
use common::event::DocumentIoEvent::ExportPlainText;
use common::event::DocumentIoEvent::ImportDjot;
use common::event::DocumentIoEvent::ImportPlainText;

use common::long_operation::{LongOperation, LongOperationManager, OperationProgress};
use common::{database::db_context::DbContext, event::EventHub};
use std::sync::Arc;

pub fn import_plain_text(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    dto: &ImportPlainTextDto,
) -> Result<()> {
    let uow_context = ImportPlainTextUnitOfWorkFactory::new(db_context, event_hub);
    let mut uc = ImportPlainTextUseCase::new(Box::new(uow_context));
    uc.execute(dto)?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ImportPlainText),
        ids: vec![],
        data: None,
    });
    Ok(())
}

pub fn export_plain_text(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
) -> Result<ExportPlainTextDto> {
    export_plain_text_with(
        db_context,
        event_hub,
        common::parser_tools::PlainTextExportOptions::addressable(),
    )
}

/// [`export_plain_text`], with the presentation options a written-out `.txt` file wants.
///
/// Separate entry point rather than flags on the default one, because the default one's
/// output is load-bearing: it is pinned to the document's addressable text, so anything
/// that indents or inserts would move every search offset after it.
pub fn export_plain_text_with(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    options: common::parser_tools::PlainTextExportOptions,
) -> Result<ExportPlainTextDto> {
    let uow_context = ExportPlainTextUnitOfWorkFactory::new(db_context);
    let mut uc = ExportPlainTextUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute(options)?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportPlainText),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn import_markdown(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ImportMarkdownDto,
) -> Result<String> {
    let uow_context = ImportMarkdownUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportMarkdownUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

pub fn get_import_markdown_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_import_markdown_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ImportMarkdownResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ImportMarkdownResultDto
    let result_dto: ImportMarkdownResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

pub fn export_markdown(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
) -> Result<ExportMarkdownDto> {
    export_markdown_with(
        db_context,
        event_hub,
        common::parser_tools::MarkdownExportOptions::default(),
    )
}

/// [`export_markdown`] with the presentation opt-ins. Separate for the same reason as
/// its plain-text sibling: the default output stays plain Markdown, with no raw HTML in
/// it that nobody asked for.
pub fn export_markdown_with(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    options: common::parser_tools::MarkdownExportOptions,
) -> Result<ExportMarkdownDto> {
    let uow_context = ExportMarkdownUnitOfWorkFactory::new(db_context);
    let mut uc = ExportMarkdownUseCase::new(Box::new(uow_context), options);
    let return_dto = uc.execute()?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportMarkdown),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn import_djot(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ImportDjotDto,
) -> Result<String> {
    let uow_context = ImportDjotUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportDjotUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

/// Import Djot **synchronously**, on the calling thread.
///
/// [`import_djot`] hands the use case to the `LongOperationManager`, which spawns a
/// thread for it. That is right for a UI import (it must not block the frame loop),
/// and wrong for a *batch* caller — a backend that has to parse and rewrite hundreds
/// of documents in one operation would spawn hundreds of threads to do work it wants
/// to do inline anyway.
///
/// The use case itself is thread-agnostic: it only needs a progress sink and a cancel
/// flag. So this hands it a no-op sink and a never-set flag and calls it directly. No
/// thread, no operation id, no polling — the result is simply returned.
///
/// (Cancellation is the caller's business here: a synchronous caller can just stop
/// calling. The flag exists to satisfy the `LongOperation` signature, not to be used.)
pub fn import_djot_sync(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    dto: &ImportDjotDto,
) -> Result<ImportDjotResultDto> {
    let uow_context = ImportDjotUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportDjotUseCase::new(Box::new(uow_context), dto);
    let result = uc.execute(
        Box::new(|_progress| {}),
        Arc::new(std::sync::atomic::AtomicBool::new(false)),
    )?;
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ImportDjot),
        ids: vec![],
        data: None,
    });
    Ok(result)
}

pub fn get_import_djot_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_import_djot_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ImportDjotResultDto>> {
    let result_json = long_operation_manager.get_operation_result(operation_id);
    if result_json.is_none() {
        return Ok(None);
    }
    let result_dto: ImportDjotResultDto = serde_json::from_str(&result_json.unwrap())?;
    Ok(Some(result_dto))
}

pub fn export_djot(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    options: &DjotExportOptions,
) -> Result<ExportDjotDto> {
    let uow_context = ExportDjotUnitOfWorkFactory::new(db_context);
    let mut uc = ExportDjotUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute(options)?;
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportDjot),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn import_html(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ImportHtmlDto,
) -> Result<String> {
    let uow_context = ImportHtmlUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportHtmlUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

pub fn get_import_html_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_import_html_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ImportHtmlResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ImportHtmlResultDto
    let result_dto: ImportHtmlResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

pub fn export_html(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Result<ExportHtmlDto> {
    export_html_with_options(db_context, event_hub, Default::default())
}

/// HTML export with an explicit image policy (reference / data URI / omit).
pub fn export_html_with_options(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    options: common::parser_tools::HtmlExportOptions,
) -> Result<ExportHtmlDto> {
    let uow_context = ExportHtmlUnitOfWorkFactory::new(db_context);
    let mut uc = ExportHtmlUseCase::new(Box::new(uow_context), options);
    let return_dto = uc.execute()?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportHtml),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn export_latex(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    dto: &ExportLatexDto,
) -> Result<ExportLatexResultDto> {
    let uow_context = ExportLatexUnitOfWorkFactory::new(db_context);
    let mut uc = ExportLatexUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute(dto)?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportLatex),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn export_docx(
    db_context: &DbContext,
    _event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ExportDocxDto,
) -> Result<String> {
    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

/// Build the in-memory DOCX document for `db_context` without writing a file.
///
/// This runs the exact same builder used by [`export_docx`] but returns the
/// assembled [`docx_rs::Docx`] instead of packing it to disk, so callers can
/// inspect the produced structure. Intended for tests; the type is re-exported
/// as [`crate::docx_rs`] so callers can name it.
#[doc(hidden)]
pub fn build_docx_document(db_context: &DbContext, dto: &ExportDocxDto) -> Result<docx_rs::Docx> {
    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
    let (docx, _paragraph_count) = uc.build_document()?;
    Ok(docx)
}

/// As [`build_docx_document`], but also runs the post-`build()` raw-XML comment patch
/// (`w15:done`, `w:initials`, the uid attribute) and returns the packable
/// [`docx_rs::XMLDocx`] instead of the pre-`build()` [`docx_rs::Docx`]. Intended for tests
/// asserting on those three fields, none of which the bare builder struct exposes — they only
/// exist in the packed XML bytes. Also used to produce a real, on-disk `.docx` for the
/// golden-fixture LibreOffice conversion test, since `docx_rs::Docx::build().pack(..)` alone
/// would skip the patch entirely.
#[doc(hidden)]
pub fn build_docx_xml_document(
    db_context: &DbContext,
    dto: &ExportDocxDto,
) -> Result<docx_rs::XMLDocx> {
    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
    uc.build_document_xml()
}

pub fn get_export_docx_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_export_docx_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ExportDocxResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ExportDocxResultDto
    let result_dto: ExportDocxResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

pub fn export_epub(
    db_context: &DbContext,
    _event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ExportEpubDto,
) -> Result<String> {
    let uow_context = ExportEpubUnitOfWorkFactory::new(db_context);
    let uc = ExportEpubUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

/// Build the packaged EPUB bytes for `db_context` without writing a file.
///
/// This runs the exact same builder used by [`export_epub`] but returns the assembled `.epub`
/// zip archive as bytes instead of writing it to disk, so callers (tests, notably) can inspect
/// the produced package directly — e.g. with the `zip` crate.
#[doc(hidden)]
pub fn build_epub_document(db_context: &DbContext, dto: &ExportEpubDto) -> Result<Vec<u8>> {
    let uow_context = ExportEpubUnitOfWorkFactory::new(db_context);
    let uc = ExportEpubUseCase::new(Box::new(uow_context), dto);
    let (epub_bytes, _chapter_count) = uc.build_document()?;
    Ok(epub_bytes)
}

pub fn get_export_epub_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_export_epub_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ExportEpubResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ExportEpubResultDto
    let result_dto: ExportEpubResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

pub fn export_odt(
    db_context: &DbContext,
    _event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ExportOdtDto,
) -> Result<String> {
    let uow_context = ExportOdtUnitOfWorkFactory::new(db_context);
    let uc = ExportOdtUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

/// Build the packaged ODT bytes for `db_context` without writing a file.
///
/// This runs the exact same builder used by [`export_odt`] but returns the assembled `.odt` zip
/// archive as bytes instead of writing it to disk, so callers (tests, notably) can inspect the
/// produced package directly — the ODT analog of [`build_epub_document`].
#[doc(hidden)]
pub fn build_odt_document(db_context: &DbContext, dto: &ExportOdtDto) -> Result<Vec<u8>> {
    let uow_context = ExportOdtUnitOfWorkFactory::new(db_context);
    let uc = ExportOdtUseCase::new(Box::new(uow_context), dto);
    let (odt_bytes, _paragraph_count) = uc.build_document()?;
    Ok(odt_bytes)
}

pub fn get_export_odt_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_export_odt_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ExportOdtResultDto>> {
    let result_json = long_operation_manager.get_operation_result(operation_id);
    if result_json.is_none() {
        return Ok(None);
    }
    let result_dto: ExportOdtResultDto = serde_json::from_str(&result_json.unwrap())?;
    Ok(Some(result_dto))
}

// ─── PDF export (via embedded Typst) — dual-cfg ────────────────────────────
//
// `export_pdf`/`get_export_pdf_progress`/`get_export_pdf_result` exist with the SAME signature
// regardless of whether the `pdf` cargo feature is enabled on `text-document-io`. This is what
// lets `frontend`'s command wrappers and `public_api`'s `to_pdf`/`to_pdf_with_options` call these
// symbols unconditionally (no `#[cfg]` in that consumer code) — the feature only decides which
// body is compiled: a real `LongOperation`-backed export, or an immediate "unsupported" error.
// Cargo feature unification means any crate in the build graph enabling `pdf` turns it on
// globally for that build, so a stable always-present API surface that fails at *runtime* is
// strictly better here than two divergent source trees.

#[cfg(feature = "pdf")]
pub fn export_pdf(
    db_context: &DbContext,
    _event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ExportPdfDto,
) -> Result<String> {
    let uow_context = ExportPdfUnitOfWorkFactory::new(db_context);
    let uc = ExportPdfUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

#[cfg(not(feature = "pdf"))]
pub fn export_pdf(
    _db_context: &DbContext,
    _event_hub: &Arc<EventHub>,
    _long_operation_manager: &mut LongOperationManager,
    _dto: &ExportPdfDto,
) -> Result<String> {
    Err(anyhow::anyhow!(
        "PDF export requires the `pdf` cargo feature on the `text-document-io` crate"
    ))
}

/// Build the PDF bytes for `db_context` without writing a file.
///
/// This runs the exact same builder used by [`export_pdf`] but returns the compiled PDF bytes
/// instead of writing them to disk, so callers (tests, notably) can inspect the produced document
/// directly.
#[cfg(feature = "pdf")]
#[doc(hidden)]
pub fn build_pdf_document(db_context: &DbContext, dto: &ExportPdfDto) -> Result<Vec<u8>> {
    let uow_context = ExportPdfUnitOfWorkFactory::new(db_context);
    let uc = ExportPdfUseCase::new(Box::new(uow_context), dto);
    let (pdf_bytes, _page_count) = uc.build_document()?;
    Ok(pdf_bytes)
}

#[cfg(feature = "pdf")]
pub fn get_export_pdf_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

#[cfg(not(feature = "pdf"))]
pub fn get_export_pdf_progress(
    _long_operation_manager: &LongOperationManager,
    _operation_id: &str,
) -> Option<OperationProgress> {
    None
}

#[cfg(feature = "pdf")]
pub fn get_export_pdf_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ExportPdfResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ExportPdfResultDto
    let result_dto: ExportPdfResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

#[cfg(not(feature = "pdf"))]
pub fn get_export_pdf_result(
    _long_operation_manager: &LongOperationManager,
    _operation_id: &str,
) -> Result<Option<ExportPdfResultDto>> {
    Ok(None)
}