Skip to content

Commit

Permalink
Add parser for simple block (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Dec 11, 2023
1 parent 20a5b0a commit fe610c1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/blocks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Block elements form the main structure of an AsciiDoc document, starting
//! with the document itself.
//!
//! A block element (aka block) is a discrete, line-oriented chunk of content in
//! an AsciiDoc document. Once parsed, that chunk of content becomes a block
//! element in the parsed document model. Certain blocks may contain other
//! blocks, so we say that blocks can be nested. The converter visits each block
//! in turn, in document order, converting it to a corresponding chunk of
//! output.

pub(crate) mod simple;
23 changes: 23 additions & 0 deletions src/blocks/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use nom::{multi::many1, IResult};

use crate::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>,
}

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> {
let (i, inlines) = many1(non_empty_line)(i)?;

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

pub mod blocks;

mod error;
pub use error::{Error, ParseResult};

Expand Down
1 change: 1 addition & 0 deletions src/tests/blocks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod simple;
45 changes: 45 additions & 0 deletions src/tests/blocks/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use nom::{
error::{Error, ErrorKind},
Err,
};

use crate::blocks::simple::SimpleBlock;

#[test]
fn empty_source() {
let expected_err: Err<Error<&str>> = Err::Error(Error::new("", ErrorKind::TakeTill1));

let actual_err = SimpleBlock::from_str("").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 actual_err = SimpleBlock::from_str(" ").unwrap_err();

assert_eq!(expected_err, actual_err);
}

#[test]
fn single_line() {
let expected = SimpleBlock {
inlines: vec!["abc"],
};

assert_eq!(SimpleBlock::from_str("abc"), Ok(("", expected)));
}

#[test]
fn multiple_lines() {
let expected = SimpleBlock {
inlines: vec!["abc", "def"],
};

assert_eq!(
SimpleBlock::from_str("abc\ndef\n\nghi"),
Ok(("\nghi", expected))
);
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![allow(clippy::panic)]
#![allow(clippy::unwrap_used)]

mod blocks;
mod fixtures;
mod primitives;
mod strings;

0 comments on commit fe610c1

Please sign in to comment.