From 1f0103cbecc6a4dfe3f34fb6441d4d491b385142 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Thu, 27 Jun 2024 20:22:16 +0100 Subject: WIP: stream parsing --- src/reader.rs | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'src/reader.rs') diff --git a/src/reader.rs b/src/reader.rs index 6e622f4..2785c88 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1,5 +1,7 @@ use futures::Stream; -use tokio::io::AsyncBufRead; +use nom::Err; +use std::str; +use tokio::io::AsyncBufReadExt; use crate::{ element::{Element, Name, Namespace}, @@ -8,20 +10,50 @@ use crate::{ /// streaming reader that tracks depth and available namespaces at current depth pub struct Reader { - stream: R, + inner: R, // holds which tags we are in atm over depth depth: Vec, namespaces: Vec<(usize, Namespace)>, } +impl Reader { + pub fn new(reader: R) -> Self { + Self { + inner: reader, + depth: Vec::new(), + namespaces: Vec::new(), + } + } +} + impl Reader where - R: AsyncBufRead, + R: AsyncBufReadExt + Unpin, { - // pub async fn read(&self) -> Result, Error> { - // let buf = self.stream.poll_fill_buf().await?; - // todo!() - // } + /// reads entire next prolog, element, or misc + pub async fn read<'s>(&'s mut self) -> Result, Error> { + let element; + let len; + loop { + let buf = self.inner.fill_buf().await?; + let input = str::from_utf8(buf)?; + match crate::xml::element(input) { + Ok((rest, e)) => { + element = e; + len = buf.len() - rest.len(); + break; + } + Err(e) => match e { + Err::Incomplete(_) => (), + e => return Err(Error::ParseError(input.to_owned())), + }, + } + } + self.inner.consume(len); + + // Ok(element) + todo!() + } // pub async fn read_start(&self) -> Result, Error> { // todo!() // } -- cgit