pdf_oxide 0.3.22

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! XFA (XML Forms Architecture) parser.
//!
//! This module parses XFA form data from PDF documents. XFA is an XML-based
//! form specification used in some PDFs, particularly government and financial forms.
//!
//! # XFA Packet Structure
//!
//! XFA forms contain several XML packets:
//! - **template**: Form structure and field definitions
//! - **datasets**: Form data values
//! - **config**: Configuration settings
//! - **form**: Runtime form state
//!
//! # Limitations
//!
//! This parser supports static XFA conversion only:
//! - Extracts field definitions and values
//! - Does NOT support dynamic XFA features (scripts, calculations)
//! - Does NOT support complex layouts (tables, grids)

use crate::error::{Error, Result};
use quick_xml::events::Event;
use quick_xml::Reader;
use std::collections::HashMap;

/// US Letter page width in points (8.5 inches × 72 points/inch).
const LETTER_WIDTH: f32 = 612.0;
/// US Letter page height in points (11 inches × 72 points/inch).
const LETTER_HEIGHT: f32 = 792.0;

/// XFA field type extracted from template.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XfaFieldType {
    /// Text field (textEdit)
    Text,
    /// Numeric field (numericEdit)
    Numeric,
    /// Date field (dateTimeEdit)
    DateTime,
    /// Checkbox (checkButton)
    Checkbox,
    /// Radio button group (choiceList with appearance=minimal)
    RadioGroup,
    /// Drop-down list (choiceList)
    DropDown,
    /// List box (choiceList with appearance=full)
    ListBox,
    /// Button (button)
    Button,
    /// Signature field (signature)
    Signature,
    /// Image field (imageEdit)
    Image,
    /// Barcode field (barcode)
    Barcode,
    /// Unknown field type
    Unknown(String),
}

impl XfaFieldType {
    /// Parse from XFA UI element name.
    pub fn from_xfa_name(name: &str) -> Self {
        match name {
            "textEdit" => Self::Text,
            "numericEdit" => Self::Numeric,
            "dateTimeEdit" => Self::DateTime,
            "checkButton" => Self::Checkbox,
            "choiceList" => Self::DropDown, // Default, may be ListBox based on appearance
            "button" => Self::Button,
            "signature" => Self::Signature,
            "imageEdit" => Self::Image,
            "barcode" => Self::Barcode,
            _ => Self::Unknown(name.to_string()),
        }
    }
}

/// An option in a choice field (dropdown, list box, radio group).
#[derive(Debug, Clone)]
pub struct XfaOption {
    /// Display text
    pub text: String,
    /// Value when selected
    pub value: String,
}

/// A field extracted from XFA template.
#[derive(Debug, Clone)]
pub struct XfaField {
    /// Field name
    pub name: String,
    /// Full path/binding (e.g., "topmostSubform[0].Page1[0].field1[0]")
    pub binding: String,
    /// Field type
    pub field_type: XfaFieldType,
    /// Tooltip/caption
    pub tooltip: Option<String>,
    /// Caption text
    pub caption: Option<String>,
    /// Default value
    pub default_value: Option<String>,
    /// Current value from datasets
    pub value: Option<String>,
    /// Options for choice fields
    pub options: Vec<XfaOption>,
    /// Is field required
    pub required: bool,
    /// Is field read-only
    pub readonly: bool,
    /// Maximum length (for text fields)
    pub max_length: Option<u32>,
    /// Width hint
    pub width: Option<f32>,
    /// Height hint
    pub height: Option<f32>,
    /// X position hint
    pub x: Option<f32>,
    /// Y position hint
    pub y: Option<f32>,
}

impl XfaField {
    /// Create a new XFA field.
    pub fn new(name: impl Into<String>, binding: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            binding: binding.into(),
            field_type: XfaFieldType::Text,
            tooltip: None,
            caption: None,
            default_value: None,
            value: None,
            options: Vec::new(),
            required: false,
            readonly: false,
            max_length: None,
            width: None,
            height: None,
            x: None,
            y: None,
        }
    }
}

/// A page extracted from XFA template.
#[derive(Debug, Clone)]
pub struct XfaPage {
    /// Page name
    pub name: String,
    /// Fields on this page
    pub fields: Vec<XfaField>,
    /// Width in points
    pub width: f32,
    /// Height in points
    pub height: f32,
}

impl Default for XfaPage {
    fn default() -> Self {
        Self {
            name: String::new(),
            fields: Vec::new(),
            width: LETTER_WIDTH,
            height: LETTER_HEIGHT,
        }
    }
}

/// Parsed XFA form.
#[derive(Debug, Clone, Default)]
pub struct XfaForm {
    /// Form name/title
    pub name: Option<String>,
    /// Pages in the form
    pub pages: Vec<XfaPage>,
    /// All fields (flattened)
    pub fields: Vec<XfaField>,
    /// Field values from datasets (binding -> value)
    pub dataset_values: HashMap<String, String>,
    /// Raw template XML (for debugging)
    pub template_xml: Option<String>,
    /// Raw datasets XML (for debugging)
    pub datasets_xml: Option<String>,
}

impl XfaForm {
    /// Get a field by name.
    pub fn get_field(&self, name: &str) -> Option<&XfaField> {
        self.fields.iter().find(|f| f.name == name)
    }

    /// Get a field by binding path.
    pub fn get_field_by_binding(&self, binding: &str) -> Option<&XfaField> {
        self.fields.iter().find(|f| f.binding == binding)
    }

    /// Get total number of fields.
    pub fn field_count(&self) -> usize {
        self.fields.len()
    }
}

/// XFA parser.
pub struct XfaParser {
    /// Parsed form
    form: XfaForm,
    /// Current parsing context stack (element names)
    context_stack: Vec<String>,
    /// Current binding path parts
    binding_parts: Vec<String>,
}

impl Default for XfaParser {
    fn default() -> Self {
        Self::new()
    }
}

impl XfaParser {
    /// Create a new parser.
    pub fn new() -> Self {
        Self {
            form: XfaForm::default(),
            context_stack: Vec::new(),
            binding_parts: Vec::new(),
        }
    }

    /// Parse XFA data from raw bytes.
    ///
    /// The XFA data may be a single XML stream or an array of packets.
    pub fn parse(&mut self, xfa_data: &[u8]) -> Result<XfaForm> {
        // Try to parse as XML
        let xml_str = String::from_utf8_lossy(xfa_data);
        self.form.template_xml = Some(xml_str.to_string());

        // Check if this is a complete XDP (XFA Data Package)
        if xml_str.contains("<xdp:xdp") || xml_str.contains("<xfa:datasets") {
            self.parse_xdp(&xml_str)?;
        } else if xml_str.contains("<template") {
            // Just a template packet
            self.parse_template(&xml_str)?;
        } else if xml_str.contains("<xfa:data") || xml_str.contains("<data>") {
            // Just a datasets packet
            self.parse_datasets(&xml_str)?;
        }

        // Apply dataset values to fields
        self.apply_dataset_values();

        Ok(std::mem::take(&mut self.form))
    }

    /// Parse XFA packets from a PDF XFA array.
    ///
    /// XFA in PDFs can be an array: [name1, stream1, name2, stream2, ...]
    pub fn parse_packets(&mut self, packets: &[(String, Vec<u8>)]) -> Result<XfaForm> {
        for (name, data) in packets {
            let xml_str = String::from_utf8_lossy(data);

            match name.as_str() {
                "template" => {
                    self.form.template_xml = Some(xml_str.to_string());
                    self.parse_template(&xml_str)?;
                },
                "datasets" => {
                    self.form.datasets_xml = Some(xml_str.to_string());
                    self.parse_datasets(&xml_str)?;
                },
                // Other packets (config, form, etc.) are not needed for static conversion
                _ => {},
            }
        }

        // Apply dataset values to fields
        self.apply_dataset_values();

        Ok(std::mem::take(&mut self.form))
    }

    /// Parse a complete XDP document.
    fn parse_xdp(&mut self, xml: &str) -> Result<()> {
        // Extract template section
        if let Some(template_start) = xml.find("<template") {
            if let Some(template_end) = xml.find("</template>") {
                let template_xml = &xml[template_start..template_end + 11];
                self.parse_template(template_xml)?;
            }
        }

        // Extract datasets section
        if let Some(data_start) = xml.find("<xfa:datasets") {
            if let Some(data_end) = xml.find("</xfa:datasets>") {
                let datasets_xml = &xml[data_start..data_end + 15];
                self.form.datasets_xml = Some(datasets_xml.to_string());
                self.parse_datasets(datasets_xml)?;
            }
        }

        Ok(())
    }

    /// Parse the template packet.
    fn parse_template(&mut self, xml: &str) -> Result<()> {
        let mut reader = Reader::from_str(xml);
        reader.config_mut().trim_text(true);

        let mut current_field: Option<XfaField> = None;
        let mut current_page: Option<XfaPage> = None;
        let mut in_items = false;
        let mut current_option_text = String::new();
        let mut current_option_value = String::new();

        loop {
            match reader.read_event() {
                Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                    let local_name = String::from_utf8_lossy(e.local_name().as_ref()).to_string();

                    match local_name.as_str() {
                        "subform" => {
                            // Check if this is a page-level subform
                            let name = self.get_attribute(e, "name");
                            if let Some(name) = name {
                                self.binding_parts.push(format!("{}[0]", name));

                                // Check for page-like subforms
                                if name.contains("Page") || current_page.is_none() {
                                    current_page = Some(XfaPage {
                                        name,
                                        ..Default::default()
                                    });
                                }
                            }
                        },
                        "field" => {
                            let name = self.get_attribute(e, "name").unwrap_or_default();
                            let mut binding = self.binding_parts.join(".");
                            if !binding.is_empty() {
                                binding.push('.');
                            }
                            binding.push_str(&format!("{}[0]", name));

                            let mut field = XfaField::new(&name, binding);

                            // Get dimensions from attributes
                            if let Some(w) = self.get_attribute(e, "w") {
                                field.width = self.parse_dimension(&w);
                            }
                            if let Some(h) = self.get_attribute(e, "h") {
                                field.height = self.parse_dimension(&h);
                            }
                            if let Some(x) = self.get_attribute(e, "x") {
                                field.x = self.parse_dimension(&x);
                            }
                            if let Some(y) = self.get_attribute(e, "y") {
                                field.y = self.parse_dimension(&y);
                            }

                            current_field = Some(field);
                        },
                        "textEdit" | "numericEdit" | "dateTimeEdit" | "checkButton"
                        | "choiceList" | "button" | "signature" | "imageEdit" | "barcode" => {
                            if let Some(ref mut field) = current_field {
                                field.field_type = XfaFieldType::from_xfa_name(&local_name);
                            }
                        },
                        "items" => {
                            in_items = true;
                        },
                        "text" => {
                            // Will capture text content
                        },
                        "caption" => {
                            // Caption element
                        },
                        "toolTip" => {
                            // Tooltip element
                        },
                        _ => {},
                    }

                    self.context_stack.push(local_name);
                },
                Ok(Event::Text(e)) => {
                    let text = e.xml_content().unwrap_or_default().to_string();

                    if let Some(parent) = self.context_stack.last() {
                        if in_items && parent == "text" {
                            current_option_text = text.clone();
                            current_option_value = text;
                        } else if let Some(ref mut field) = current_field {
                            match parent.as_str() {
                                "caption" | "text" => {
                                    if field.caption.is_none()
                                        && !in_items
                                        && self.context_stack.iter().any(|s| s == "caption")
                                    {
                                        field.caption = Some(text);
                                    }
                                },
                                "toolTip" => {
                                    field.tooltip = Some(text);
                                },
                                "value" => {
                                    if field.default_value.is_none() {
                                        field.default_value = Some(text);
                                    }
                                },
                                _ => {},
                            }
                        }
                    }
                },
                Ok(Event::End(ref e)) => {
                    let local_name = String::from_utf8_lossy(e.local_name().as_ref()).to_string();

                    match local_name.as_str() {
                        "subform" => {
                            self.binding_parts.pop();
                        },
                        "field" => {
                            if let Some(field) = current_field.take() {
                                self.form.fields.push(field.clone());
                                if let Some(ref mut page) = current_page {
                                    page.fields.push(field);
                                }
                            }
                        },
                        "items" => {
                            in_items = false;
                        },
                        "text" => {
                            if in_items {
                                if let Some(ref mut field) = current_field {
                                    field.options.push(XfaOption {
                                        text: current_option_text.clone(),
                                        value: current_option_value.clone(),
                                    });
                                }
                                current_option_text.clear();
                                current_option_value.clear();
                            }
                        },
                        _ => {},
                    }

                    self.context_stack.pop();
                },
                Ok(Event::Eof) => break,
                Err(e) => {
                    return Err(Error::InvalidPdf(format!("XFA template parse error: {}", e)));
                },
                _ => {},
            }
        }

        // Add the last page if any
        if let Some(page) = current_page {
            if !page.fields.is_empty() {
                self.form.pages.push(page);
            }
        }

        Ok(())
    }

    /// Parse the datasets packet.
    fn parse_datasets(&mut self, xml: &str) -> Result<()> {
        let mut reader = Reader::from_str(xml);
        reader.config_mut().trim_text(true);

        let mut path_stack: Vec<String> = Vec::new();
        let mut current_text = String::new();

        loop {
            match reader.read_event() {
                Ok(Event::Start(ref e)) => {
                    let local_name = String::from_utf8_lossy(e.local_name().as_ref()).to_string();

                    // Skip XFA namespace elements
                    if local_name != "xfa" && local_name != "datasets" && local_name != "data" {
                        path_stack.push(format!("{}[0]", local_name));
                    }
                },
                Ok(Event::Text(e)) => {
                    current_text = e.xml_content().unwrap_or_default().to_string();
                },
                Ok(Event::End(ref e)) => {
                    let local_name = String::from_utf8_lossy(e.local_name().as_ref()).to_string();

                    if local_name != "xfa" && local_name != "datasets" && local_name != "data" {
                        // Store value if we have text
                        if !current_text.is_empty() && !path_stack.is_empty() {
                            let binding = path_stack.join(".");
                            self.form
                                .dataset_values
                                .insert(binding, current_text.clone());
                        }
                        current_text.clear();
                        path_stack.pop();
                    }
                },
                Ok(Event::Eof) => break,
                Err(e) => {
                    return Err(Error::InvalidPdf(format!("XFA datasets parse error: {}", e)));
                },
                _ => {},
            }
        }

        Ok(())
    }

    /// Apply dataset values to fields.
    fn apply_dataset_values(&mut self) {
        for field in &mut self.form.fields {
            // Try exact binding match
            if let Some(value) = self.form.dataset_values.get(&field.binding) {
                field.value = Some(value.clone());
            } else {
                // Try partial match (XFA bindings can be complex)
                for (binding, value) in &self.form.dataset_values {
                    if binding.ends_with(&format!(".{}[0]", field.name)) {
                        field.value = Some(value.clone());
                        break;
                    }
                }
            }
        }

        // Also update page field values
        for page in &mut self.form.pages {
            for field in &mut page.fields {
                if let Some(value) = self.form.dataset_values.get(&field.binding) {
                    field.value = Some(value.clone());
                } else {
                    for (binding, value) in &self.form.dataset_values {
                        if binding.ends_with(&format!(".{}[0]", field.name)) {
                            field.value = Some(value.clone());
                            break;
                        }
                    }
                }
            }
        }
    }

    /// Get an attribute value from an element.
    fn get_attribute<'a>(
        &self,
        e: &'a quick_xml::events::BytesStart<'a>,
        name: &str,
    ) -> Option<String> {
        for attr in e.attributes().flatten() {
            if attr.key.as_ref() == name.as_bytes() {
                return Some(String::from_utf8_lossy(&attr.value).to_string());
            }
        }
        None
    }

    /// Parse a dimension value (e.g., "72pt", "1in", "2.5cm").
    fn parse_dimension(&self, value: &str) -> Option<f32> {
        let value = value.trim();

        if let Some(stripped) = value.strip_suffix("pt") {
            stripped.parse().ok()
        } else if let Some(stripped) = value.strip_suffix("in") {
            stripped.parse::<f32>().ok().map(|v| v * 72.0)
        } else if let Some(stripped) = value.strip_suffix("cm") {
            stripped.parse::<f32>().ok().map(|v| v * 72.0 / 2.54)
        } else if let Some(stripped) = value.strip_suffix("mm") {
            stripped.parse::<f32>().ok().map(|v| v * 72.0 / 25.4)
        } else {
            // Assume points
            value.parse().ok()
        }
    }
}

/// Check if data appears to be XFA content.
pub fn is_xfa_data(data: &[u8]) -> bool {
    let s = String::from_utf8_lossy(data);
    s.contains("<template") || s.contains("<xfa:") || s.contains("<xdp:xdp")
}

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

    #[test]
    fn test_xfa_field_type_from_name() {
        assert_eq!(XfaFieldType::from_xfa_name("textEdit"), XfaFieldType::Text);
        assert_eq!(XfaFieldType::from_xfa_name("numericEdit"), XfaFieldType::Numeric);
        assert_eq!(XfaFieldType::from_xfa_name("checkButton"), XfaFieldType::Checkbox);
        assert_eq!(XfaFieldType::from_xfa_name("choiceList"), XfaFieldType::DropDown);
        assert!(matches!(XfaFieldType::from_xfa_name("unknown"), XfaFieldType::Unknown(_)));
    }

    #[test]
    fn test_xfa_field_new() {
        let field = XfaField::new("firstName", "form.page1.firstName[0]");

        assert_eq!(field.name, "firstName");
        assert_eq!(field.binding, "form.page1.firstName[0]");
        assert_eq!(field.field_type, XfaFieldType::Text);
        assert!(field.value.is_none());
    }

    #[test]
    fn test_xfa_page_default() {
        let page = XfaPage::default();

        assert!(page.name.is_empty());
        assert!(page.fields.is_empty());
        assert_eq!(page.width, 612.0);
        assert_eq!(page.height, 792.0);
    }

    #[test]
    fn test_xfa_form_get_field() {
        let mut form = XfaForm::default();
        form.fields.push(XfaField::new("test", "binding"));

        assert!(form.get_field("test").is_some());
        assert!(form.get_field("nonexistent").is_none());
    }

    #[test]
    fn test_xfa_form_get_field_by_binding() {
        let mut form = XfaForm::default();
        form.fields.push(XfaField::new("test", "root.test[0]"));

        assert!(form.get_field_by_binding("root.test[0]").is_some());
        assert!(form.get_field_by_binding("other").is_none());
    }

    #[test]
    fn test_parser_dimension_parsing() {
        let parser = XfaParser::new();

        assert_eq!(parser.parse_dimension("72pt"), Some(72.0));
        assert_eq!(parser.parse_dimension("1in"), Some(72.0));
        assert_eq!(parser.parse_dimension("2.54cm"), Some(72.0));
        assert_eq!(parser.parse_dimension("25.4mm"), Some(72.0));
        assert_eq!(parser.parse_dimension("100"), Some(100.0));
    }

    #[test]
    fn test_is_xfa_data() {
        assert!(is_xfa_data(b"<template xmlns=\"http://www.xfa.org/schema/xfa-template/2.8/\">"));
        assert!(is_xfa_data(b"<xfa:datasets>"));
        assert!(is_xfa_data(b"<xdp:xdp xmlns:xdp=\"http://ns.adobe.com/xdp/\">"));
        assert!(!is_xfa_data(b"<html><body>Not XFA</body></html>"));
        assert!(!is_xfa_data(b"Random data"));
    }

    #[test]
    fn test_parse_simple_template() {
        let template = r#"<?xml version="1.0"?>
<template xmlns="http://www.xfa.org/schema/xfa-template/3.0/">
    <subform name="form1">
        <field name="firstName" w="200pt" h="20pt">
            <ui>
                <textEdit/>
            </ui>
            <caption>
                <value><text>First Name</text></value>
            </caption>
        </field>
        <field name="lastName">
            <ui>
                <textEdit/>
            </ui>
        </field>
    </subform>
</template>"#;

        let mut parser = XfaParser::new();
        let form = parser.parse(template.as_bytes()).unwrap();

        assert_eq!(form.fields.len(), 2);
        assert_eq!(form.fields[0].name, "firstName");
        assert_eq!(form.fields[0].field_type, XfaFieldType::Text);
        assert_eq!(form.fields[0].width, Some(200.0));
        assert_eq!(form.fields[0].height, Some(20.0));
        assert_eq!(form.fields[1].name, "lastName");
    }

    #[test]
    fn test_parse_datasets() {
        let datasets = r#"<?xml version="1.0"?>
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
    <xfa:data>
        <form1>
            <firstName>John</firstName>
            <lastName>Doe</lastName>
        </form1>
    </xfa:data>
</xfa:datasets>"#;

        let mut parser = XfaParser::new();

        // First add some fields
        parser
            .form
            .fields
            .push(XfaField::new("firstName", "form1[0].firstName[0]"));
        parser
            .form
            .fields
            .push(XfaField::new("lastName", "form1[0].lastName[0]"));

        // Parse datasets and apply values
        parser.parse_datasets(datasets).unwrap();
        parser.apply_dataset_values();

        assert_eq!(parser.form.fields[0].value, Some("John".to_string()));
        assert_eq!(parser.form.fields[1].value, Some("Doe".to_string()));
    }

    #[test]
    fn test_parse_field_with_options() {
        let template = r#"<?xml version="1.0"?>
<template>
    <subform name="form1">
        <field name="country">
            <ui>
                <choiceList/>
            </ui>
            <items>
                <text>United States</text>
                <text>Canada</text>
                <text>Mexico</text>
            </items>
        </field>
    </subform>
</template>"#;

        let mut parser = XfaParser::new();
        let form = parser.parse(template.as_bytes()).unwrap();

        assert_eq!(form.fields.len(), 1);
        assert_eq!(form.fields[0].name, "country");
        assert_eq!(form.fields[0].field_type, XfaFieldType::DropDown);
        assert_eq!(form.fields[0].options.len(), 3);
        assert_eq!(form.fields[0].options[0].text, "United States");
    }

    #[test]
    fn test_parse_checkbox_field() {
        let template = r#"<?xml version="1.0"?>
<template>
    <subform name="form1">
        <field name="agree">
            <ui>
                <checkButton/>
            </ui>
            <caption>
                <value><text>I agree to the terms</text></value>
            </caption>
        </field>
    </subform>
</template>"#;

        let mut parser = XfaParser::new();
        let form = parser.parse(template.as_bytes()).unwrap();

        assert_eq!(form.fields.len(), 1);
        assert_eq!(form.fields[0].name, "agree");
        assert_eq!(form.fields[0].field_type, XfaFieldType::Checkbox);
    }

    #[test]
    fn test_xfa_option() {
        let option = XfaOption {
            text: "Display Text".to_string(),
            value: "actual_value".to_string(),
        };

        assert_eq!(option.text, "Display Text");
        assert_eq!(option.value, "actual_value");
    }
}