organic/src/parser/object.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

2023-03-25 10:45:43 -04:00
use nom::combinator::map;
2023-03-23 20:12:42 -04:00
use nom::combinator::not;
2023-03-23 19:53:20 -04:00
use super::error::Res;
2023-03-25 10:45:43 -04:00
use super::parser_with_context::parser_with_context;
use super::plain_text::plain_text;
2023-03-23 17:51:49 -04:00
use super::source::Source;
2023-03-23 19:53:20 -04:00
use super::Context;
2023-03-23 17:02:08 -04:00
2023-03-23 17:26:07 -04:00
#[derive(Debug)]
pub enum Object<'s> {
TextMarkup(TextMarkup<'s>),
PlainText(PlainText<'s>),
2023-03-23 17:02:08 -04:00
RegularLink(RegularLink<'s>),
}
2023-03-23 17:26:07 -04:00
#[derive(Debug)]
pub struct TextMarkup<'s> {
pub source: &'s str,
}
2023-03-23 17:26:07 -04:00
#[derive(Debug)]
pub struct PlainText<'s> {
pub source: &'s str,
}
2023-03-23 17:26:07 -04:00
#[derive(Debug)]
2023-03-23 17:02:08 -04:00
pub struct RegularLink<'s> {
pub source: &'s str,
}
impl<'s> Source<'s> for Object<'s> {
fn get_source(&'s self) -> &'s str {
match self {
Object::TextMarkup(obj) => obj.source,
Object::PlainText(obj) => obj.source,
2023-03-23 17:02:08 -04:00
Object::RegularLink(obj) => obj.source,
}
}
}
2023-03-23 19:53:20 -04:00
pub fn standard_set_object<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, Object<'s>> {
2023-03-23 20:12:42 -04:00
not(|i| context.check_exit_matcher(i))(input)?;
2023-03-25 10:45:43 -04:00
let plain_text_matcher = parser_with_context!(plain_text)(context);
map(plain_text_matcher, Object::PlainText)(input)
}