diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 11 | ||||
-rw-r--r-- | src/parser.rs | 77 | ||||
-rw-r--r-- | src/reader.rs | 34 | ||||
-rw-r--r-- | src/writer.rs | 12 |
4 files changed, 72 insertions, 62 deletions
@@ -3,14 +3,3 @@ mod error; mod parser; mod reader; mod writer; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/src/parser.rs b/src/parser.rs index 518aad4..07d48c6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,10 +2,7 @@ use std::char; use nom::{ branch::alt, - bytes::{ - complete::take_until, - streaming::{is_a, tag, take}, - }, + bytes::streaming::{is_a, tag, take, take_till, take_until}, character::{ complete::one_of, streaming::{char, digit1, none_of, satisfy}, @@ -19,11 +16,6 @@ use nom::{ // parser: parses tokens from lexer into events -enum Misc<'s> { - Comment(Comment<'s>), - PI(PI<'s>), -} - type Comment<'s> = &'s str; struct PI<'s> { @@ -34,46 +26,44 @@ struct PI<'s> { enum ContentItem<'s> { CharData(&'s str), Element(Element<'s>), - Reference(Reference<'s>), - CDSect(CDSect<'s>), + // Reference(Reference<'s>), + // CDSect(CDSect<'s>), } type Content<'s> = Option<Vec<ContentItem<'s>>>; -struct Element<'s> { - name: &'s str, - attributes: Vec<Attribute<'s>>, - content: Content<'s>, -} - struct Attribute<'s> { key: &'s str, value: &'s str, } -// type VersionNum<'s> = &'s str; /// Contains only latin characters or dash after first char type EncName<'s> = &'s str; -// struct XMLDecl<'s> { -// version_info: VersionNum<'s>, -// encoding_decl: Option<EncName<'s>>, -// sd_decl: Option<bool>, -// } - struct DoctypeDecl<'s> { name: &'s str, - // TODO + // TODO: doctype declaration parsing } - +/// pub fn doctypedecl(input: &str) -> IResult<&str, DoctypeDecl> { todo!() } +struct Element<'s> { + name: &'s str, + attributes: Vec<Attribute<'s>>, + content: Content<'s>, +} +/// Element pub fn element(input: &str) -> IResult<&str, Element> { todo!() } +enum Misc<'s> { + Comment(Comment<'s>), + PI(PI<'s>), +} +/// Misc pub fn misc(input: &str) -> IResult<&str, Misc> { todo!() } @@ -210,7 +200,7 @@ pub fn pubid_char(input: &str) -> IResult<&str, PubidChar> { type CharData<'s> = &'s str; /// [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) pub fn char_data(input: &str) -> IResult<&str, CharData> { - take_until()(input) + recognize(take_until("]]>").and_then(take_till(|c| c == '<' || c == '&')))(input) } type Prolog<'s> = ( @@ -229,23 +219,23 @@ pub fn prolog(input: &str) -> IResult<&str, Prolog> { struct XMLDecl { version_info: VersionInfo, - encoding_decl: Option<EncodingDecl>, - sd_decl: Option<SDDecl>, + // encoding_decl: Option<EncodingDecl>, + // sd_decl: Option<SDDecl>, } /// [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' pub fn xml_decl(input: &str) -> IResult<&str, XMLDecl> { // (VersionInfo, Option<EncodingDecl>, Option<SDDecl>) - let (leftover, (version_info, encoding_decl, sd_decl)) = delimited( + let (leftover, (version_info /* encoding_decl, sd_decl */,)) = delimited( tag("<?xml"), - tuple((version_info, opt(encoding_decl), opt(sd_decl))), + tuple((version_info /* opt(encoding_decl), opt(sd_decl) */,)), tag("?>"), )(input)?; Ok(( leftover, XMLDecl { version_info, - encoding_decl, - sd_decl, + // encoding_decl, + // sd_decl, }, )) } @@ -282,3 +272,24 @@ pub fn version_num(input: &str) -> IResult<&str, VersionNum> { )), )(input) } + +pub fn reference(input: &str) -> IResult<&str, char> { + todo!() +} + +pub fn pe_reference(input: &str) -> IResult<&str, char> { + todo!() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_char_data() { + assert_eq!(Ok(("&def]]>ghi", "abc")), char_data("abc&def]]>ghi")); + assert_eq!(Ok(("]]>ghi", "abcdef")), char_data("abcdef]]>ghi")); + assert_eq!(Ok(("&defghi", "abc")), char_data("abc&defghi")); + assert_eq!(Ok(("", "abcdefghi")), char_data("abcdefghi")); + } +} diff --git a/src/reader.rs b/src/reader.rs index 26e540e..6e622f4 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -18,21 +18,25 @@ impl<R> Reader<R> where R: AsyncBufRead, { - pub async fn read(&self) -> Result<impl From<Element>, Error> { - let buf = self.stream.poll_fill_buf().await?; - todo!() - } - pub async fn read_start(&self) -> Result<impl From<Element>, Error> {} - pub async fn read_end(&self) -> Result<(), Error> {} + // pub async fn read(&self) -> Result<impl From<Element>, Error> { + // let buf = self.stream.poll_fill_buf().await?; + // todo!() + // } + // pub async fn read_start(&self) -> Result<impl From<Element>, Error> { + // todo!() + // } + // pub async fn read_end(&self) -> Result<(), Error> { + // todo!() + // } } -impl<R: AsyncBufRead> Stream for Reader<R> { - type Item = impl From<Element>; +// impl<R: AsyncBufRead> Stream for Reader<R> { +// type Item = impl From<Element>; - async fn poll_next( - self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> std::task::Poll<Option<Self::Item>> { - todo!() - } -} +// async fn poll_next( +// self: std::pin::Pin<&mut Self>, +// cx: &mut std::task::Context<'_>, +// ) -> std::task::Poll<Option<Self::Item>> { +// todo!() +// } +// } diff --git a/src/writer.rs b/src/writer.rs index d7fc037..456a5a1 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -12,9 +12,15 @@ pub struct Writer<W> { } impl<W: AsyncWrite> Writer<W> { - pub async fn write(&self, element: impl Into<Element>) -> Result<(), Error> {} - pub async fn write_start(&self, element: impl Into<Element>) -> Result<(), Error> {} - pub async fn write_end(&self) -> Result<(), Error> {} + pub async fn write(&self, element: impl Into<Element>) -> Result<(), Error> { + todo!() + } + pub async fn write_start(&self, element: impl Into<Element>) -> Result<(), Error> { + todo!() + } + pub async fn write_end(&self) -> Result<(), Error> { + todo!() + } } impl<W: AsyncWrite, E: Into<Element>> Sink<E> for Writer<W> { |