xberg 1.3.10

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use super::*;

pub(crate) struct ParsedEmailContent {
    pub(crate) result: EmailExtractionResult,
    pub(crate) nested_messages: Vec<NestedMessagePayload>,
    /// Embedded `message/rfc822`-equivalent MSG attachments (`attach_method ==
    /// afEmbeddedMessage`), fully parsed. Flat list across all nesting depths.
    pub(crate) nested_embedded_messages: Vec<EmailExtractionResult>,
    /// Non-fatal warnings raised while parsing (e.g. a `.msg`-in-`.msg` chain
    /// that hit the configured nesting-depth cap and was left unextracted).
    pub(crate) warnings: Vec<ProcessingWarning>,
}

pub(crate) struct NestedMessagePayload {
    part_id: usize,
    pub(crate) data: Option<Bytes>,
    pub(crate) size: usize,
}

/// Parse .eml file content (RFC822 format)
pub(crate) fn parse_eml_content(data: &[u8]) -> Result<EmailExtractionResult> {
    Ok(parse_eml_content_internal(data, false, None)?.result)
}

/// Address- and header-derived fields read from a parsed `mail_parser::Message`, before
/// any body text is looked at. Bundled purely to keep [`parse_eml_content_internal`]'s
/// body shorter -- [`build_metadata`] and the final [`EmailExtractionResult`] each take
/// only the fields they need back out.
pub(super) struct EmailAddressFields {
    subject: Option<String>,
    from_email: Option<String>,
    from_name: Option<String>,
    to_emails: Vec<String>,
    cc_emails: Vec<String>,
    bcc_emails: Vec<String>,
    date: Option<String>,
    message_id: Option<String>,
    reply_to: Vec<String>,
    in_reply_to: Vec<String>,
    references: Vec<String>,
}

/// Read the sender's address and display name from the first `From:` mailbox.
pub(super) fn extract_sender_fields(message: &mail_parser::Message<'_>) -> (Option<String>, Option<String>) {
    let sender = message.from().and_then(|from| from.first());
    let from_email = sender.and_then(|address| address.address().map(str::to_string));
    let from_name = sender
        .and_then(|address| address.name().map(str::trim))
        .filter(|name| !name.is_empty())
        .map(str::to_string);
    (from_email, from_name)
}

/// Read the message date, preferring the literal `Date:` header text (see
/// `extract_raw_date_header`) over `mail_parser`'s own parsed value, and discarding a
/// parsed value that round-trips to an obviously-invalid placeholder year.
pub(super) fn extract_email_date_field(message: &mail_parser::Message<'_>, data: &[u8]) -> Option<String> {
    extract_raw_date_header(data).or_else(|| {
        message.date().and_then(|d| {
            let rfc3339 = d.to_rfc3339();
            if rfc3339.starts_with("2000-00") || rfc3339.starts_with("0000-") {
                None
            } else {
                Some(rfc3339)
            }
        })
    })
}

/// Read subject/sender/recipient/date/threading fields from a parsed message and its raw
/// bytes (`data`, needed because `extract_raw_date_header` reads the literal `Date:`
/// header text rather than `mail_parser`'s own parsed value -- see that function).
pub(super) fn extract_email_address_fields(message: &mail_parser::Message<'_>, data: &[u8]) -> EmailAddressFields {
    let subject = message.subject().map(|s| s.to_string());
    let (from_email, from_name) = extract_sender_fields(message);

    let to_emails: Vec<String> = message
        .to()
        .map(|to| {
            to.iter()
                .filter_map(|addr| addr.address().map(|email| email.to_string()))
                .collect()
        })
        .unwrap_or_default();

    let cc_emails: Vec<String> = message
        .cc()
        .map(|cc| {
            cc.iter()
                .filter_map(|addr| addr.address().map(|email| email.to_string()))
                .collect()
        })
        .unwrap_or_default();

    let bcc_emails: Vec<String> = message
        .bcc()
        .map(|bcc| {
            bcc.iter()
                .filter_map(|addr| addr.address().map(|email| email.to_string()))
                .collect()
        })
        .unwrap_or_default();

    let date = extract_email_date_field(message, data);

    let message_id = message.message_id().map(|id| id.to_string());

    let reply_to: Vec<String> = message
        .reply_to()
        .map(|addrs| {
            addrs
                .iter()
                .filter_map(|addr| addr.address().map(|email| email.to_string()))
                .collect()
        })
        .unwrap_or_default();

    let in_reply_to: Vec<String> = message
        .in_reply_to()
        .as_text_list()
        .map(|list| list.iter().map(|s| s.to_string()).collect())
        .unwrap_or_default();

    let references: Vec<String> = message
        .references()
        .as_text_list()
        .map(|list| list.iter().map(|s| s.to_string()).collect())
        .unwrap_or_default();

    EmailAddressFields {
        subject,
        from_email,
        from_name,
        to_emails,
        cc_emails,
        bcc_emails,
        date,
        message_id,
        reply_to,
        in_reply_to,
        references,
    }
}

/// Build the attachment list, resolving each `message/rfc822` nested-message attachment's
/// bytes from `nested_messages` (already capped/collected by
/// [`collect_nested_message_payloads`]) when nesting was requested, or from the part's own
/// raw bytes otherwise.
pub(super) fn build_email_attachments(
    message: &mail_parser::Message<'_>,
    nested_messages: &[NestedMessagePayload],
    include_nested_messages: bool,
) -> Vec<EmailAttachment> {
    let mut attachments = Vec::with_capacity(message.attachments().count().min(20));
    for &part_id in &message.attachments {
        let Some(attachment) = message.parts.get(part_id as usize) else {
            continue;
        };
        let filename = attachment.attachment_name().map(|s| s.to_string());

        let mime_type = if matches!(&attachment.body, mail_parser::PartType::Message(_)) {
            "message/rfc822".to_string()
        } else {
            attachment
                .content_type()
                .map(|ct| {
                    let content_type_str = format!("{}/{}", ct.ctype(), ct.subtype().unwrap_or("octet-stream"));
                    parse_content_type(&content_type_str)
                })
                .unwrap_or_else(|| "application/octet-stream".to_string())
        };

        let (data, size) = match &attachment.body {
            mail_parser::PartType::Message(nested) => {
                let raw_message = nested.raw_message();
                let data = if include_nested_messages {
                    nested_messages
                        .iter()
                        .find(|payload| payload.part_id == part_id as usize)
                        .and_then(|payload| payload.data.clone())
                } else {
                    Some(Bytes::copy_from_slice(raw_message))
                };
                (data, raw_message.len())
            }
            _ => {
                let contents = attachment.contents();
                (Some(Bytes::copy_from_slice(contents)), contents.len())
            }
        };

        let is_image = is_image_mime_type(&mime_type);

        attachments.push(EmailAttachment {
            name: filename.clone(),
            filename,
            mime_type: Some(mime_type),
            size: Some(size),
            is_image,
            data,
        });
    }
    attachments
}

/// Fold the threading headers, sender display name, raw headers, and a compact
/// pipe-delimited attachment summary into `metadata`, after [`build_metadata`] has
/// already populated its core fields.
pub(super) fn augment_email_metadata(
    metadata: &mut HashMap<String, String>,
    addresses: &EmailAddressFields,
    raw_headers: &HashMap<String, String>,
    attachments: &[EmailAttachment],
) {
    if let Some(from_name) = &addresses.from_name {
        metadata.insert("from_name".to_string(), from_name.clone());
    }

    if !addresses.reply_to.is_empty() {
        metadata.insert("reply_to".to_string(), addresses.reply_to.join(", "));
    }
    if !addresses.in_reply_to.is_empty() {
        metadata.insert("in_reply_to".to_string(), addresses.in_reply_to.join(", "));
    }
    if !addresses.references.is_empty() {
        metadata.insert("references".to_string(), addresses.references.join(", "));
    }

    for (key, value) in raw_headers {
        metadata.insert(key.clone(), value.clone());
    }

    if !attachments.is_empty() {
        let attachment_details: Vec<String> = attachments
            .iter()
            .map(|att| {
                let name = att.filename.as_deref().or(att.name.as_deref()).unwrap_or("unnamed");
                let mime = att.mime_type.as_deref().unwrap_or("application/octet-stream");
                let size = att.size.unwrap_or(0);
                format!("{}|{}|{}", name, mime, size)
            })
            .collect();
        metadata.insert("attachment_details".to_string(), attachment_details.join("; "));
    }
}

/// Gather the message's plain-text and HTML bodies (each joined from every genuine body
/// part plus any nested message's own bodies), or `None` for a body kind the message
/// carries no genuine part of -- unless `should_treat_as_html`, which forces both to be
/// gathered so [`parse_eml_content_internal`] can decide there whether one substitutes
/// for the other.
pub(super) fn extract_email_bodies(
    message: &mail_parser::Message<'_>,
    should_treat_as_html: bool,
) -> (Option<String>, Option<String>) {
    let has_genuine_text_part = has_genuine_text_body(message);
    let plain_text = if has_genuine_text_part || should_treat_as_html {
        let mut all_text = Vec::new();
        let mut i = 0;
        while let Some(text) = message.body_text(i) {
            all_text.push(text.to_string());
            i += 1;
        }
        collect_nested_message_text(message, &mut all_text);
        if all_text.is_empty() {
            None
        } else {
            Some(all_text.join("\n\n"))
        }
    } else {
        None
    };

    let has_genuine_html_part = has_genuine_html_body(message);
    let html_content = if has_genuine_html_part || should_treat_as_html {
        let mut all_html = Vec::new();
        let mut i = 0;
        while let Some(html) = message.body_html(i) {
            all_html.push(html.to_string());
            i += 1;
        }
        collect_nested_message_html(message, &mut all_html);
        if all_html.is_empty() {
            None
        } else {
            Some(all_html.join("\n\n"))
        }
    } else {
        None
    };

    (plain_text, html_content)
}

/// Resolve the final `html_content` (an HTML-typed message with no genuine HTML part
/// falls back to its plain-text body, since that is the only body it has) and the
/// plain-text `content` field derived from whichever body is present, preferring HTML.
pub(super) fn resolve_email_content(
    plain_text: &Option<String>,
    html_content: Option<String>,
    should_treat_as_html: bool,
) -> (Option<String>, String) {
    let html_content = if should_treat_as_html && html_content.is_none() && plain_text.is_some() {
        plain_text.clone()
    } else {
        html_content
    };

    let content = if let Some(html) = &html_content {
        clean_html_content(html)
    } else if let Some(plain) = plain_text {
        plain.clone()
    } else {
        String::new()
    };

    (html_content, content)
}

pub(super) fn parse_eml_content_internal(
    data: &[u8],
    include_nested_messages: bool,
    max_nested_message_bytes: Option<u64>,
) -> Result<ParsedEmailContent> {
    let data = if let Some(transcoded) = maybe_transcode_utf16(data) {
        std::borrow::Cow::Owned(transcoded)
    } else {
        std::borrow::Cow::Borrowed(data)
    };

    let message = mail_parser::MessageParser::default()
        .parse(&data)
        .ok_or_else(|| XbergError::parsing("Failed to parse EML file: invalid email format".to_string()))?;

    let addresses = extract_email_address_fields(&message, &data);
    let raw_headers = extract_raw_headers(&data);

    let should_treat_as_html = message
        .content_type()
        .and_then(|ct| ct.subtype())
        .map(|subtype| subtype.eq_ignore_ascii_case("html"))
        .unwrap_or(false);

    let (plain_text, html_content) = extract_email_bodies(&message, should_treat_as_html);
    let (html_content, content) = resolve_email_content(&plain_text, html_content, should_treat_as_html);

    let nested_messages = if include_nested_messages {
        collect_nested_message_payloads(&message, max_nested_message_bytes)
    } else {
        Vec::new()
    };

    let attachments = build_email_attachments(&message, &nested_messages, include_nested_messages);

    let mut metadata = build_metadata(EmailMetadataFields {
        subject: &addresses.subject,
        from_email: &addresses.from_email,
        to_emails: &addresses.to_emails,
        cc_emails: &addresses.cc_emails,
        bcc_emails: &addresses.bcc_emails,
        date: &addresses.date,
        message_id: &addresses.message_id,
        attachments: &attachments,
    });
    augment_email_metadata(&mut metadata, &addresses, &raw_headers, &attachments);

    let EmailAddressFields {
        subject,
        from_email,
        to_emails,
        cc_emails,
        bcc_emails,
        date,
        message_id,
        ..
    } = addresses;

    Ok(ParsedEmailContent {
        result: EmailExtractionResult {
            subject,
            from_email,
            to_emails,
            cc_emails,
            bcc_emails,
            date,
            message_id,
            plain_text,
            html_content,
            content,
            attachments,
            metadata,
        },
        nested_messages,
        nested_embedded_messages: Vec::new(),
        warnings: Vec::new(),
    })
}

pub(super) fn collect_nested_message_payloads(
    message: &mail_parser::Message<'_>,
    max_nested_message_bytes: Option<u64>,
) -> Vec<NestedMessagePayload> {
    use mail_parser::PartType;

    message
        .parts
        .iter()
        .enumerate()
        .filter_map(|(part_id, part)| match &part.body {
            PartType::Message(nested) if !nested.raw_message().is_empty() => {
                let raw_message = nested.raw_message();
                let data = max_nested_message_bytes
                    .is_none_or(|cap| raw_message.len() as u64 <= cap)
                    .then(|| Bytes::copy_from_slice(raw_message));
                Some(NestedMessagePayload {
                    part_id,
                    data,
                    size: raw_message.len(),
                })
            }
            _ => None,
        })
        .collect()
}

/// Check whether a message has at least one genuine `text/plain` body part.
///
/// `mail-parser`'s `body_text()` auto-converts HTML to plain text using a naive
/// tag-stripper that doesn't remove `<script>` or `<style>` content. This helper
/// inspects the actual `PartType` of each `text_body` entry to determine if a
/// real `text/plain` part exists. For HTML-only messages (all text_body entries
/// are `PartType::Html`), callers should use `clean_html_content()` instead.
pub(super) fn has_genuine_text_body(message: &mail_parser::Message<'_>) -> bool {
    use mail_parser::PartType;
    for &part_id in &message.text_body {
        if let Some(part) = message.parts.get(part_id as usize)
            && matches!(&part.body, PartType::Text(_))
        {
            return true;
        }
    }
    false
}

/// Check whether a message has at least one genuine `text/html` body part.
///
/// `mail-parser`'s `body_html()` auto-converts text/plain bodies into HTML when
/// no real HTML part exists. This helper inspects each `html_body` entry's
/// `PartType` so callers can avoid treating plain-text emails as HTML.
pub(super) fn has_genuine_html_body(message: &mail_parser::Message<'_>) -> bool {
    use mail_parser::PartType;
    for &part_id in &message.html_body {
        if let Some(part) = message.parts.get(part_id as usize)
            && matches!(&part.body, PartType::Html(_))
        {
            return true;
        }
    }
    false
}

/// Recursively collect plain text from nested `message/rfc822` sub-messages.
///
/// In `multipart/digest` emails, each part is itself an RFC822 message stored as
/// `PartType::Message`. The top-level `body_text()` won't return these; we must
/// recurse into the nested `Message` to extract their text bodies.
pub(super) fn collect_nested_message_text(message: &mail_parser::Message<'_>, out: &mut Vec<String>) {
    use mail_parser::PartType;
    for part in &message.parts {
        if let PartType::Message(sub_msg) = &part.body {
            let mut i = 0;
            while let Some(text) = sub_msg.body_text(i) {
                out.push(text.to_string());
                i += 1;
            }
            collect_nested_message_text(sub_msg, out);
        }
    }
}

/// Recursively collect HTML from nested `message/rfc822` sub-messages.
pub(super) fn collect_nested_message_html(message: &mail_parser::Message<'_>, out: &mut Vec<String>) {
    use mail_parser::PartType;
    for part in &message.parts {
        if let PartType::Message(sub_msg) = &part.body {
            let mut i = 0;
            while let Some(html) = sub_msg.body_html(i) {
                out.push(html.to_string());
                i += 1;
            }
            collect_nested_message_html(sub_msg, out);
        }
    }
}

/// Maximum number of bytes scanned for the Date header when no header/body
/// separator is found in the message.
const DATE_HEADER_SCAN_CAP: usize = 8192;

/// Maximum number of bytes scanned for additional raw headers when no
/// header/body separator is found in the message.
const RAW_HEADER_SCAN_CAP: usize = 16384;

/// Find the byte offset of the end of the header section (the blank line
/// separating headers from body), falling back to `cap` bytes if no
/// separator is found.
///
/// Operates purely on bytes so the returned offset is always safe to use as
/// a slice bound on `data` (byte-slice indexing never panics on non-UTF-8
/// boundaries, unlike `&str` indexing).
pub(super) fn find_header_section_end(data: &[u8], cap: usize) -> usize {
    if let Some(pos) = memchr::memmem::find(data, b"\r\n\r\n") {
        return pos;
    }
    if let Some(pos) = memchr::memmem::find(data, b"\n\n") {
        return pos;
    }
    data.len().min(cap)
}

/// Extract the raw Date header value from email bytes.
///
/// Scans for `Date:` in the header section (before the blank line that separates
/// headers from body) and returns the raw value, handling continuation lines.
///
/// The header section is decoded with `String::from_utf8_lossy` scoped to just
/// the header bytes, so a non-UTF-8 body (or a non-UTF-8 byte sequence split by
/// the scan cap) never causes header parsing to be skipped or to panic.
pub(super) fn extract_raw_date_header(data: &[u8]) -> Option<String> {
    let header_end = find_header_section_end(data, DATE_HEADER_SCAN_CAP);
    let headers = String::from_utf8_lossy(&data[..header_end]);
    let headers = headers.as_ref();

    let mut date_value = None;
    for line in headers.lines() {
        if let Some(val) = line.strip_prefix("Date:").or_else(|| line.strip_prefix("date:")) {
            date_value = Some(val.trim().to_string());
        } else if date_value.is_some() && (line.starts_with(' ') || line.starts_with('\t')) {
            if let Some(ref mut dv) = date_value {
                dv.push(' ');
                dv.push_str(line.trim());
            }
        } else if date_value.is_some() {
            break;
        }
    }

    date_value.filter(|s| !s.is_empty())
}

/// Extract additional raw headers from email bytes.
///
/// Scans for Content-Type, MIME-Version, X-Mailer, User-Agent, List-Id,
/// and List-Unsubscribe headers in the header section.
///
/// The header section is decoded with `String::from_utf8_lossy` scoped to just
/// the header bytes, so a non-UTF-8 body (or a non-UTF-8 byte sequence split by
/// the scan cap) never causes header parsing to be skipped or to panic.
pub(super) fn extract_raw_headers(data: &[u8]) -> HashMap<String, String> {
    let mut headers = HashMap::new();

    let header_end = find_header_section_end(data, RAW_HEADER_SCAN_CAP);
    let header_section = String::from_utf8_lossy(&data[..header_end]);
    let header_section = header_section.as_ref();

    let target_headers: &[(&str, &str)] = &[
        ("content-type:", "content_type"),
        ("mime-version:", "mime_version"),
        ("x-mailer:", "x_mailer"),
        ("user-agent:", "user_agent"),
        ("list-id:", "list_id"),
        ("list-unsubscribe:", "list_unsubscribe"),
    ];

    let mut current_key: Option<&str> = None;
    let mut current_value = String::new();

    for line in header_section.lines() {
        if line.starts_with(' ') || line.starts_with('\t') {
            if current_key.is_some() {
                current_value.push(' ');
                current_value.push_str(line.trim());
            }
            continue;
        }

        if let Some(key) = current_key {
            if !current_value.is_empty() {
                headers.insert(key.to_string(), current_value.clone());
            }
            current_key = None;
            current_value.clear();
        }

        let line_lower = line.to_lowercase();
        for &(prefix, meta_key) in target_headers {
            if line_lower.starts_with(prefix) {
                current_key = Some(meta_key);
                current_value = line[prefix.len()..].trim().to_string();
                break;
            }
        }
    }

    if let Some(key) = current_key
        && !current_value.is_empty()
    {
        headers.insert(key.to_string(), current_value);
    }

    headers
}