use tokio::io::{AsyncRead, AsyncReadExt};
use crate::Result;
use crate::{parse_bytes, ParseOptions};
use sup_xml_tree::dom::Document;
pub async fn parse_async<R: AsyncRead + Unpin>(mut reader: R) -> Result<Document> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await.map_err(|e| crate::XmlError::new(
crate::ErrorDomain::Io,
crate::ErrorLevel::Error,
format!("io error reading async source: {e}"),
))?;
parse_bytes(&bytes, &ParseOptions::default())
}
pub async fn parse_async_with<R: AsyncRead + Unpin>(
mut reader: R, opts: &ParseOptions,
) -> Result<Document> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await.map_err(|e| crate::XmlError::new(
crate::ErrorDomain::Io,
crate::ErrorLevel::Error,
format!("io error reading async source: {e}"),
))?;
parse_bytes(&bytes, opts)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(flavor = "current_thread")]
async fn parse_from_cursor() {
let bytes = b"<r><a>1</a></r>".to_vec();
let doc = parse_async(&bytes[..]).await.expect("parse");
assert_eq!(doc.root().name(), "r");
}
#[tokio::test(flavor = "current_thread")]
async fn malformed_input_errors() {
let bytes = b"<r><unclosed>".to_vec();
let r = parse_async(&bytes[..]).await;
assert!(r.is_err());
}
}