Skip to main content

Module async_api

Module async_api 

Source
Expand description

Async XML API. Async API for XML conversion with Tokio

This module provides asynchronous versions of all XML conversion functions, enabling high-performance concurrent I/O operations with Tokio.

§Features

  • Async File I/O: Read/write XML files without blocking
  • Async Streaming: Stream large XML files incrementally
  • Concurrency: Process multiple files concurrently
  • Backpressure: Built-in flow control for streaming

§Examples

§Async file conversion

use hedl_xml::async_api::{from_xml_file_async, to_xml_file_async};
use hedl_xml::{FromXmlConfig, ToXmlConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read XML file asynchronously
    let doc = from_xml_file_async("input.xml", &FromXmlConfig::default()).await?;

    // Process document...

    // Write XML file asynchronously
    to_xml_file_async(&doc, "output.xml", &ToXmlConfig::default()).await?;

    Ok(())
}

§Async streaming for large files

use hedl_xml::async_api::from_xml_stream_async;
use hedl_xml::streaming::StreamConfig;
use tokio::fs::File;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("large.xml").await?;
    let config = StreamConfig::default();

    let mut stream = from_xml_stream_async(file, &config).await?;

    while let Some(result) = stream.next().await {
        match result {
            Ok(item) => println!("Processed: {}", item.key),
            Err(e) => eprintln!("Error: {}", e),
        }
    }

    Ok(())
}

§Concurrent processing

use hedl_xml::async_api::from_xml_file_async;
use hedl_xml::FromXmlConfig;
use tokio::task;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = FromXmlConfig::default();

    // Process multiple files concurrently
    let handles: Vec<_> = vec!["file1.xml", "file2.xml", "file3.xml"]
        .into_iter()
        .map(|path| {
            let config = config.clone();
            task::spawn(async move {
                from_xml_file_async(path, &config).await
            })
        })
        .collect();

    // Wait for all to complete
    for handle in handles {
        let doc = handle.await??;
        // Process document...
    }

    Ok(())
}

Structs§

AsyncXmlStream
Async streaming XML parser

Functions§

from_xml_async
Parse XML string asynchronously (runs on tokio threadpool)
from_xml_file_async
Read and parse an XML file asynchronously
from_xml_files_concurrent
Process multiple XML files concurrently
from_xml_reader_async
Parse XML from an async reader
from_xml_stream_async
Create an async streaming XML parser
to_xml_async
Convert HEDL to XML string asynchronously (runs on tokio threadpool)
to_xml_file_async
Write a HEDL document to an XML file asynchronously
to_xml_files_concurrent
Write multiple documents to XML files concurrently
to_xml_writer_async
Write XML to an async writer