From 59f061ecca7ca8d7d4d0994b132bfc4615142a39 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 13 Jul 2023 18:30:18 -0400 Subject: [PATCH] Define pre and post matchers for plain link. --- src/parser/plain_link.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/parser/plain_link.rs b/src/parser/plain_link.rs index 4df5d7f..c95bb9c 100644 --- a/src/parser/plain_link.rs +++ b/src/parser/plain_link.rs @@ -1,10 +1,45 @@ +use nom::branch::alt; +use nom::character::complete::none_of; +use nom::combinator::eof; +use nom::combinator::recognize; + use super::Context; +use crate::error::CustomError; +use crate::error::MyError; use crate::error::Res; use crate::parser::object::PlainLink; +use crate::parser::util::get_one_before; use crate::parser::util::not_yet_implemented; +use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; #[tracing::instrument(ret, level = "debug")] pub fn plain_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainLink<'s>> { not_yet_implemented()?; todo!(); } + +#[tracing::instrument(ret, level = "debug")] +pub fn pre<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { + let document_root = context.get_document_root().unwrap(); + let preceding_character = get_one_before(document_root, input) + .map(|slice| slice.chars().next()) + .flatten(); + match preceding_character { + // If None, we are at the start of the file which is fine + None => {} + Some(x) if !WORD_CONSTITUENT_CHARACTERS.contains(x) => {} + Some(_) => { + // Not at start of line, cannot be a heading + return Err(nom::Err::Error(CustomError::MyError(MyError( + "Not a valid pre character for plain link.", + )))); + } + }; + Ok((input, ())) +} + +#[tracing::instrument(ret, level = "debug")] +pub fn post<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()> { + let (remaining, _) = alt((eof, recognize(none_of(WORD_CONSTITUENT_CHARACTERS))))(input)?; + Ok((remaining, ())) +}