From 878452e012224c9f88ec823c5261e76d508803b8 Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Sun, 17 Dec 2023 14:07:08 -0800 Subject: [PATCH] Simple block parser is back --- src/blocks/simple.rs | 6 +++--- src/lib.rs | 2 +- src/tests/blocks/simple.rs | 41 +++++++++++++++++++++++++------------- src/tests/mod.rs | 2 +- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/blocks/simple.rs b/src/blocks/simple.rs index 7298a0a..563b88c 100644 --- a/src/blocks/simple.rs +++ b/src/blocks/simple.rs @@ -1,13 +1,13 @@ use nom::{multi::many1, IResult}; -use crate::primitives::non_empty_line; +use crate::{input::Input, primitives::non_empty_line}; /// A block that's treated as contiguous lines of paragraph text (and subject to /// normal substitutions) (e.g., a paragraph block). #[derive(Clone, Debug, Eq, PartialEq)] pub struct SimpleBlock<'a> { /// Lines that were found. - pub inlines: Vec<&'a str>, + pub inlines: Vec>, } impl<'a> SimpleBlock<'a> { @@ -15,7 +15,7 @@ impl<'a> SimpleBlock<'a> { /// /// Returns a tuple of the remaining input and the simple block. #[allow(dead_code)] // TEMPORARY - pub fn from_str(i: &'a str) -> IResult<&str, Self> { + pub(crate) fn parse(i: Input<'a>) -> IResult { let (i, inlines) = many1(non_empty_line)(i)?; Ok((i, Self { inlines })) diff --git a/src/lib.rs b/src/lib.rs index 3a50b2a..55ef50e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ #![deny(warnings)] #![doc = include_str!("../README.md")] -// pub mod blocks; +pub mod blocks; mod error; pub use error::{Error, ParseResult}; diff --git a/src/tests/blocks/simple.rs b/src/tests/blocks/simple.rs index 7ad1062..aa8c96b 100644 --- a/src/tests/blocks/simple.rs +++ b/src/tests/blocks/simple.rs @@ -3,22 +3,22 @@ use nom::{ Err, }; -use crate::blocks::simple::SimpleBlock; +use crate::{blocks::simple::SimpleBlock, input::Input}; #[test] fn empty_source() { - let expected_err: Err> = Err::Error(Error::new("", ErrorKind::TakeTill1)); + let expected_err = Err::Error(Error::new(Input::new("", true), ErrorKind::TakeTill1)); - let actual_err = SimpleBlock::from_str("").unwrap_err(); + let actual_err = SimpleBlock::parse(Input::new("", true)).unwrap_err(); assert_eq!(expected_err, actual_err); } #[test] fn only_spaces() { - let expected_err: Err> = Err::Error(Error::new(" ", ErrorKind::TakeTill1)); + let expected_err = Err::Error(Error::new(Input::new(" ", true), ErrorKind::TakeTill1)); - let actual_err = SimpleBlock::from_str(" ").unwrap_err(); + let actual_err = SimpleBlock::parse(Input::new(" ", true)).unwrap_err(); assert_eq!(expected_err, actual_err); } @@ -26,20 +26,33 @@ fn only_spaces() { #[test] fn single_line() { let expected = SimpleBlock { - inlines: vec!["abc"], + inlines: vec![Input::new("abc", true)], }; - assert_eq!(SimpleBlock::from_str("abc"), Ok(("", expected))); + let (rem, block) = SimpleBlock::parse(Input::new("abc", true)).unwrap(); + + assert_eq!(rem.line(), 1); + assert_eq!(rem.col(), 4); + assert_eq!(*rem.data(), ""); + + assert_eq!(block, expected); } #[test] fn multiple_lines() { - let expected = SimpleBlock { - inlines: vec!["abc", "def"], - }; + let (rem, block) = SimpleBlock::parse(Input::new("abc\ndef", true)).unwrap(); + + assert_eq!(rem.line(), 2); + assert_eq!(rem.col(), 4); + assert_eq!(*rem.data(), ""); + + assert_eq!(block.inlines.len(), 2); + + assert_eq!(block.inlines[0].line(), 1); + assert_eq!(block.inlines[0].col(), 1); + assert_eq!(*block.inlines[0].data(), "abc"); - assert_eq!( - SimpleBlock::from_str("abc\ndef\n\nghi"), - Ok(("\nghi", expected)) - ); + assert_eq!(block.inlines[1].line(), 2); + assert_eq!(block.inlines[1].col(), 1); + assert_eq!(*block.inlines[1].data(), "def"); } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 9574f04..714b1ba 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -5,7 +5,7 @@ #![allow(clippy::panic)] #![allow(clippy::unwrap_used)] -// mod blocks; +mod blocks; mod fixtures; mod input; mod primitives;