cloudiful-docling-convert 0.3.3

Core Rust library for Docling-backed document conversion
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
use std::path::Path;
use std::str::FromStr;

use bytes::Bytes;
use serde::{Deserialize, Serialize};

use crate::error::{PdfConvertError, Result};
use crate::models::{ChunkDocumentResponse, DoclingChunk};

mod input_kind;

pub use input_kind::InputKind;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OutputFormat {
    Json,
    Md,
    Yaml,
    Html,
    HtmlSplitPage,
    Text,
    Doctags,
    Vtt,
    Doclang,
    Dclx,
    Chunks,
}

impl OutputFormat {
    pub fn as_api_value(self) -> &'static str {
        match self {
            Self::Json => "json",
            Self::Md => "md",
            Self::Yaml => "yaml",
            Self::Html => "html",
            Self::HtmlSplitPage => "html_split_page",
            Self::Text => "text",
            Self::Doctags => "doctags",
            Self::Vtt => "vtt",
            Self::Doclang => "doclang",
            Self::Dclx => "dclx",
            Self::Chunks => "chunks",
        }
    }

    pub fn extension(self) -> &'static str {
        match self {
            Self::Chunks => "chunks.json",
            Self::Yaml | Self::HtmlSplitPage | Self::Vtt | Self::Dclx => "zip",
            _ => self.as_api_value(),
        }
    }

    pub fn is_archive(self) -> bool {
        matches!(
            self,
            Self::Yaml | Self::HtmlSplitPage | Self::Vtt | Self::Dclx
        )
    }

    pub fn is_chunk_output(self) -> bool {
        matches!(self, Self::Chunks)
    }
}

impl std::fmt::Display for OutputFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_api_value())
    }
}

impl FromStr for OutputFormat {
    type Err = PdfConvertError;

    fn from_str(value: &str) -> Result<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "json" => Ok(Self::Json),
            "md" | "markdown" => Ok(Self::Md),
            "yaml" | "yml" => Ok(Self::Yaml),
            "html" => Ok(Self::Html),
            "html_split_page" | "html-split-page" => Ok(Self::HtmlSplitPage),
            "text" | "txt" => Ok(Self::Text),
            "doctags" => Ok(Self::Doctags),
            "vtt" => Ok(Self::Vtt),
            "doclang" => Ok(Self::Doclang),
            "dclx" => Ok(Self::Dclx),
            "chunks" => Ok(Self::Chunks),
            other => Err(PdfConvertError::validation_error(
                "format",
                format!("unsupported output format: {other}"),
            )),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ChunkerKind {
    #[default]
    None,
    Hybrid,
    Hierarchical,
}

impl ChunkerKind {
    pub fn is_enabled(self) -> bool {
        !matches!(self, Self::None)
    }
}

impl std::fmt::Display for ChunkerKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = match self {
            Self::None => "none",
            Self::Hybrid => "hybrid",
            Self::Hierarchical => "hierarchical",
        };
        f.write_str(value)
    }
}

impl FromStr for ChunkerKind {
    type Err = PdfConvertError;

    fn from_str(value: &str) -> Result<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "none" | "" => Ok(Self::None),
            "hybrid" => Ok(Self::Hybrid),
            "hierarchical" => Ok(Self::Hierarchical),
            other => Err(PdfConvertError::validation_error(
                "chunker",
                format!("unsupported chunker: {other}"),
            )),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ChunkingOptions {
    pub use_markdown_tables: bool,
    pub use_markdown_images: bool,
    pub image_placeholder: String,
    pub include_raw_text: bool,
    pub max_tokens: Option<u32>,
    pub tokenizer: Option<String>,
    pub merge_peers: bool,
}

impl ChunkingOptions {
    pub fn hybrid_defaults() -> Self {
        Self {
            image_placeholder: "![IMAGE]".to_string(),
            tokenizer: Some("sentence-transformers/all-MiniLM-L6-v2".to_string()),
            merge_peers: true,
            ..Self::default()
        }
    }

    pub fn hierarchical_defaults() -> Self {
        Self {
            image_placeholder: "![IMAGE]".to_string(),
            merge_peers: true,
            ..Self::default()
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PipelineKind {
    Legacy,
    Standard,
    Vlm,
    Asr,
}

impl FromStr for PipelineKind {
    type Err = PdfConvertError;

    fn from_str(value: &str) -> Result<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "legacy" => Ok(Self::Legacy),
            "standard" => Ok(Self::Standard),
            "vlm" => Ok(Self::Vlm),
            "asr" => Ok(Self::Asr),
            other => Err(PdfConvertError::validation_error(
                "pipeline",
                format!("unsupported pipeline: {other}"),
            )),
        }
    }
}

#[derive(Debug, Clone)]
pub struct InputDocument {
    pub filename: String,
    pub media_type: String,
    pub bytes: Bytes,
    pub input_kind_override: Option<InputKind>,
}

impl InputDocument {
    pub fn new(
        filename: impl Into<String>,
        media_type: impl Into<String>,
        bytes: impl Into<Bytes>,
    ) -> Self {
        Self {
            filename: filename.into(),
            media_type: media_type.into(),
            bytes: bytes.into(),
            input_kind_override: None,
        }
    }

    pub fn with_input_kind(mut self, input_kind: InputKind) -> Self {
        self.input_kind_override = Some(input_kind);
        self
    }

    pub fn from_path_and_bytes(path: &Path, bytes: impl Into<Bytes>) -> Result<Self> {
        let filename = path
            .file_name()
            .and_then(|name| name.to_str())
            .ok_or_else(|| {
                PdfConvertError::validation_error(
                    "input_path",
                    format!("path '{}' does not have a valid file name", path.display()),
                )
            })?;
        let kind = InputKind::from_path(path).ok_or_else(|| {
            PdfConvertError::validation_error(
                "input_path",
                format!("unsupported file type for '{}'", path.display()),
            )
        })?;

        Ok(Self::new(
            filename,
            kind.canonical_media_type(filename, None),
            bytes,
        ))
    }

    pub fn from_path_and_bytes_with_kind(
        path: &Path,
        bytes: impl Into<Bytes>,
        input_kind: InputKind,
    ) -> Result<Self> {
        let filename = path
            .file_name()
            .and_then(|name| name.to_str())
            .ok_or_else(|| {
                PdfConvertError::validation_error(
                    "input_path",
                    format!("path '{}' does not have a valid file name", path.display()),
                )
            })?;

        Ok(Self::new(
            filename,
            input_kind.canonical_media_type(filename, None),
            bytes,
        )
        .with_input_kind(input_kind))
    }

    pub fn kind(&self) -> Result<InputKind> {
        if let Some(input_kind) = self.input_kind_override {
            return Ok(input_kind);
        }

        InputKind::from_filename_and_media_type(&self.filename, Some(&self.media_type)).ok_or_else(
            || {
                let reason = if InputKind::requires_explicit_override(
                    &self.filename,
                    Some(&self.media_type),
                ) {
                    format!(
                        "ambiguous input type for '{}' ({}); provide an explicit input_format override",
                        self.filename, self.media_type
                    )
                } else {
                    format!(
                        "unsupported input type for '{}' ({})",
                        self.filename, self.media_type
                    )
                };
                PdfConvertError::validation_error("input", reason)
            },
        )
    }
}

#[derive(Debug, Clone)]
pub struct ConvertRequest {
    pub input: InputDocument,
    pub output_formats: Vec<OutputFormat>,
    pub options: ConvertOptions,
}

impl ConvertRequest {
    pub fn validate(&self) -> Result<InputKind> {
        let kind = self.input.kind()?;
        match (&self.options, kind) {
            (ConvertOptions::Pdf(_), InputKind::Pdf)
            | (ConvertOptions::Text(_), InputKind::Text) => {}
            (ConvertOptions::Generic(_), _) if kind.uses_generic_convert_options() => {}
            (_, InputKind::Pdf) => {
                return Err(PdfConvertError::validation_error(
                    "options",
                    "PDF input requires PdfConvertOptions",
                ));
            }
            (_, InputKind::Text) => {
                return Err(PdfConvertError::validation_error(
                    "options",
                    "txt input requires TextConvertOptions",
                ));
            }
            _ => {
                return Err(PdfConvertError::validation_error(
                    "options",
                    "non-pdf, non-text input requires GenericFileConvertOptions",
                ));
            }
        }

        if self.output_formats.is_empty() {
            return Err(PdfConvertError::validation_error(
                "output_formats",
                "at least one output format is required",
            ));
        }

        let chunker = match &self.options {
            ConvertOptions::Pdf(options) | ConvertOptions::Generic(options) => options.chunker,
            ConvertOptions::Text(_) => ChunkerKind::None,
        };

        if chunker.is_enabled() && self.output_formats.iter().any(|format| format.is_archive()) {
            return Err(PdfConvertError::validation_error(
                "output_formats",
                "native chunking cannot be combined with archive outputs",
            ));
        }

        if self
            .output_formats
            .iter()
            .any(|format| format.is_chunk_output())
            && !chunker.is_enabled()
        {
            return Err(PdfConvertError::validation_error(
                "chunker",
                "chunks output requires hybrid or hierarchical chunking",
            ));
        }

        Ok(kind)
    }
}

#[derive(Debug, Clone)]
pub enum ConvertOptions {
    Pdf(PdfConvertOptions),
    Generic(GenericFileConvertOptions),
    Text(TextConvertOptions),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteConvertOptions {
    pub chunker: ChunkerKind,
    pub chunking: ChunkingOptions,
    pub pipeline: Option<PipelineKind>,
    /// Preset ID for picture description, forwarded to Docling Serve as
    /// `picture_description_preset`. Mutually exclusive with the legacy
    /// `picture_description_custom_config` emitted when a VLM bundle is
    /// configured; leaving this `None` preserves the legacy custom VLM
    /// behaviour.
    pub picture_description_preset: Option<String>,
}

impl Default for RemoteConvertOptions {
    fn default() -> Self {
        Self {
            chunker: ChunkerKind::None,
            chunking: ChunkingOptions::hybrid_defaults(),
            pipeline: None,
            picture_description_preset: None,
        }
    }
}

pub type PdfConvertOptions = RemoteConvertOptions;
pub type GenericFileConvertOptions = RemoteConvertOptions;

#[derive(Debug, Clone)]
pub struct TextConvertOptions {
    pub normalize_line_endings: bool,
    pub trim_utf8_bom: bool,
}

impl Default for TextConvertOptions {
    fn default() -> Self {
        Self {
            normalize_line_endings: true,
            trim_utf8_bom: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvertedDocumentMetadata {
    pub input_kind: InputKind,
    pub media_type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvertedDocument {
    pub filename: String,
    pub markdown: Option<String>,
    pub text: Option<String>,
    pub json: Option<serde_json::Value>,
    pub html: Option<String>,
    pub doctags: Option<String>,
    pub doclang: Option<String>,
    pub chunks: Vec<DoclingChunk>,
    pub chunk_response: Option<ChunkDocumentResponse>,
    #[serde(skip)]
    pub archive: Option<Vec<u8>>,
    pub metadata: ConvertedDocumentMetadata,
    pub errors: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct FileConvertRequest {
    pub request: ConvertRequest,
    pub output_dir: std::path::PathBuf,
    pub selected_output: OutputFormat,
    pub overwrite: bool,
}

#[derive(Debug, Clone)]
pub struct ConvertedFile {
    pub document: ConvertedDocument,
    pub output_paths: Vec<std::path::PathBuf>,
}

pub fn supported_input_kind(path: &Path) -> bool {
    InputKind::from_path(path).is_some()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn output_format_supports_native_and_archive_outputs() {
        for (value, format) in [
            ("md", OutputFormat::Md),
            ("markdown", OutputFormat::Md),
            ("json", OutputFormat::Json),
            ("yaml", OutputFormat::Yaml),
            ("yml", OutputFormat::Yaml),
            ("html", OutputFormat::Html),
            ("html_split_page", OutputFormat::HtmlSplitPage),
            ("html-split-page", OutputFormat::HtmlSplitPage),
            ("text", OutputFormat::Text),
            ("txt", OutputFormat::Text),
            ("doctags", OutputFormat::Doctags),
            ("vtt", OutputFormat::Vtt),
            ("doclang", OutputFormat::Doclang),
            ("dclx", OutputFormat::Dclx),
            ("chunks", OutputFormat::Chunks),
        ] {
            assert_eq!(value.parse::<OutputFormat>().unwrap(), format);
        }

        assert_eq!(OutputFormat::Chunks.extension(), "chunks.json");
        for format in [
            OutputFormat::Yaml,
            OutputFormat::HtmlSplitPage,
            OutputFormat::Vtt,
            OutputFormat::Dclx,
        ] {
            assert_eq!(format.extension(), "zip");
            assert!(format.is_archive());
        }
    }

    #[test]
    fn chunking_rejects_archive_outputs() {
        let request = ConvertRequest {
            input: InputDocument::new("a.pdf", "application/pdf", Bytes::from_static(b"%PDF")),
            output_formats: vec![OutputFormat::Yaml],
            options: ConvertOptions::Pdf(RemoteConvertOptions {
                chunker: ChunkerKind::Hybrid,
                ..RemoteConvertOptions::default()
            }),
        };

        assert!(
            request
                .validate()
                .unwrap_err()
                .to_string()
                .contains("archive")
        );
    }

    #[test]
    fn chunks_require_a_native_chunker() {
        let request = ConvertRequest {
            input: InputDocument::new("a.pdf", "application/pdf", Bytes::from_static(b"%PDF")),
            output_formats: vec![OutputFormat::Chunks],
            options: ConvertOptions::Pdf(RemoteConvertOptions::default()),
        };

        assert!(
            request
                .validate()
                .unwrap_err()
                .to_string()
                .contains("requires")
        );
    }

    #[test]
    fn ambiguous_xml_requires_explicit_override() {
        let error = InputDocument::new(
            "paper.xml",
            "application/xml",
            Bytes::from_static(b"<article />"),
        )
        .kind()
        .unwrap_err();

        assert!(error.to_string().contains("explicit input_format override"));
    }

    #[test]
    fn remote_convert_options_default_omits_picture_description_preset() {
        let options = RemoteConvertOptions::default();
        assert!(
            options.picture_description_preset.is_none(),
            "default RemoteConvertOptions must not carry a picture_description_preset so the legacy custom VLM bundle is preserved"
        );
    }

    #[test]
    fn remote_convert_options_round_trip_picture_description_preset() {
        let options = RemoteConvertOptions {
            chunker: ChunkerKind::None,
            chunking: ChunkingOptions::default(),
            pipeline: None,
            picture_description_preset: Some("granite_vision".to_string()),
        };
        assert_eq!(
            options.picture_description_preset.as_deref(),
            Some("granite_vision")
        );
    }
}