aboutsummaryrefslogtreecommitdiffstats
path: root/src/reader.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2024-06-27 20:22:16 +0100
committerLibravatar cel 🌸 <cel@blos.sm>2024-06-27 20:22:16 +0100
commit1f0103cbecc6a4dfe3f34fb6441d4d491b385142 (patch)
tree6551f950332e5c913f33720e92c88cdf67ed5832 /src/reader.rs
parentc08b4504ab326203b2c11abe566e518b6466613a (diff)
downloadpeanuts-1f0103cbecc6a4dfe3f34fb6441d4d491b385142.tar.gz
peanuts-1f0103cbecc6a4dfe3f34fb6441d4d491b385142.tar.bz2
peanuts-1f0103cbecc6a4dfe3f34fb6441d4d491b385142.zip
WIP: stream parsing
Diffstat (limited to 'src/reader.rs')
-rw-r--r--src/reader.rs46
1 files changed, 39 insertions, 7 deletions
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<R> {
- stream: R,
+ inner: R,
// holds which tags we are in atm over depth
depth: Vec<Name>,
namespaces: Vec<(usize, Namespace)>,
}
+impl<R> Reader<R> {
+ pub fn new(reader: R) -> Self {
+ Self {
+ inner: reader,
+ depth: Vec::new(),
+ namespaces: Vec::new(),
+ }
+ }
+}
+
impl<R> Reader<R>
where
- R: AsyncBufRead,
+ R: AsyncBufReadExt + Unpin,
{
- // pub async fn read(&self) -> Result<impl From<Element>, 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<crate::xml::Element<'s>, 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<impl From<Element>, Error> {
// todo!()
// }