Skip to content

Commit

Permalink
Simple block parser is back
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Dec 17, 2023
1 parent 2f30b98 commit 878452e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/blocks/simple.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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<Input<'a>>,
}

impl<'a> SimpleBlock<'a> {
/// Parse a byte-slice as a simple AsciiDoc block.
///
/// 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<Input, Self> {
let (i, inlines) = many1(non_empty_line)(i)?;

Ok((i, Self { inlines }))
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![deny(warnings)]
#![doc = include_str!("../README.md")]

// pub mod blocks;
pub mod blocks;

mod error;
pub use error::{Error, ParseResult};
Expand Down
41 changes: 27 additions & 14 deletions src/tests/blocks/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,56 @@ use nom::{
Err,
};

use crate::blocks::simple::SimpleBlock;
use crate::{blocks::simple::SimpleBlock, input::Input};

#[test]
fn empty_source() {
let expected_err: Err<Error<&str>> = 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<Error<&str>> = 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 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");
}
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(clippy::panic)]
#![allow(clippy::unwrap_used)]

// mod blocks;
mod blocks;
mod fixtures;
mod input;
mod primitives;
Expand Down

0 comments on commit 878452e

Please sign in to comment.