spring_batch_rs/item/xml/mod.rs
1/// XML support for reading and writing structured data.
2///
3/// This module provides components for reading data from XML files and writing data to XML files
4/// as part of a batch processing pipeline. The implementation uses `quick-xml` for efficient
5/// XML parsing and serialization.
6///
7/// # Features
8///
9/// - Read XML documents with support for complex nested structures
10/// - Write data to XML files with customizable root and item tags
11/// - Support for XML attributes via serde's `#[serde(rename = "@attribute_name")]`
12/// - Automatic type inference for tag names
13///
14/// # Examples
15///
16/// ## Reading from XML
17///
18/// ```
19/// use spring_batch_rs::item::xml::XmlItemReaderBuilder;
20/// use spring_batch_rs::core::item::ItemReader;
21/// use serde::Deserialize;
22/// use std::io::Cursor;
23///
24/// // Define a data structure with XML attributes and nested elements
25/// #[derive(Debug, Deserialize)]
26/// struct Product {
27/// #[serde(rename = "@id")]
28/// id: String,
29/// #[serde(rename = "@available")]
30/// available: bool,
31/// name: String,
32/// price: f64,
33/// #[serde(default)]
34/// description: Option<String>,
35/// }
36///
37/// // Sample XML data
38/// let xml_data = r#"
39/// <catalog>
40/// <product id="P001" available="true">
41/// <name>Wireless Headphones</name>
42/// <price>79.99</price>
43/// <description>Noise-cancelling wireless headphones with 20hr battery life</description>
44/// </product>
45/// <product id="P002" available="false">
46/// <name>USB-C Cable</name>
47/// <price>12.99</price>
48/// </product>
49/// </catalog>
50/// "#;
51///
52/// // Create a reader from our XML
53/// let cursor = Cursor::new(xml_data);
54/// let reader = XmlItemReaderBuilder::<Product>::new()
55/// .tag("product")
56/// .from_reader(cursor);
57///
58/// // Read and process the products
59/// let mut products = Vec::new();
60/// while let Some(product) = reader.read().unwrap() {
61/// products.push(product);
62/// }
63///
64/// // Verify results
65/// assert_eq!(products.len(), 2);
66/// assert_eq!(products[0].id, "P001");
67/// assert_eq!(products[0].name, "Wireless Headphones");
68/// assert_eq!(products[0].price, 79.99);
69/// assert!(products[0].available);
70/// assert!(products[0].description.is_some());
71///
72/// assert_eq!(products[1].id, "P002");
73/// assert_eq!(products[1].name, "USB-C Cable");
74/// assert_eq!(products[1].price, 12.99);
75/// assert!(!products[1].available);
76/// assert!(products[1].description.is_none());
77/// ```
78///
79/// ## Writing to XML
80///
81/// ```
82/// use spring_batch_rs::item::xml::xml_writer::XmlItemWriterBuilder;
83/// use spring_batch_rs::core::item::ItemWriter;
84/// use serde::Serialize;
85/// use std::io::Cursor;
86///
87/// // Define a data structure for serialization
88/// #[derive(Serialize)]
89/// struct Product {
90/// #[serde(rename = "@id")]
91/// id: String,
92/// #[serde(rename = "@in_stock")]
93/// in_stock: bool,
94/// name: String,
95/// price: f64,
96/// categories: Vec<String>,
97/// }
98///
99/// // Create some products
100/// let products = vec![
101/// Product {
102/// id: "P001".to_string(),
103/// in_stock: true,
104/// name: "Smartphone".to_string(),
105/// price: 599.99,
106/// categories: vec!["Electronics".to_string(), "Mobile".to_string()],
107/// },
108/// Product {
109/// id: "P002".to_string(),
110/// in_stock: false,
111/// name: "Laptop".to_string(),
112/// price: 1299.99,
113/// categories: vec!["Electronics".to_string(), "Computers".to_string()],
114/// },
115/// ];
116///
117/// // Create a writer with a memory buffer
118/// let buffer = Cursor::new(Vec::new());
119/// let writer = XmlItemWriterBuilder::<Product>::new()
120/// .root_tag("catalog")
121/// .item_tag("product")
122/// .from_writer(buffer);
123///
124/// // Write the products to XML
125/// writer.open().unwrap();
126/// writer.write(&products).unwrap();
127/// writer.close().unwrap();
128///
129/// // The resulting XML would look similar to:
130/// // <catalog>
131/// // <product id="P001" in_stock="true">
132/// // <name>Smartphone</name>
133/// // <price>599.99</price>
134/// // <categories>Electronics</categories>
135/// // <categories>Mobile</categories>
136/// // </product>
137/// // <product id="P002" in_stock="false">
138/// // <name>Laptop</name>
139/// // <price>1299.99</price>
140/// // <categories>Electronics</categories>
141/// // <categories>Computers</categories>
142/// // </product>
143/// // </catalog>
144/// ```
145pub mod xml_reader;
146pub mod xml_writer;
147
148pub use xml_reader::{XmlItemReader, XmlItemReaderBuilder};
149pub use xml_writer::XmlItemWriter;
150pub use xml_writer::XmlItemWriterBuilder;