organic/src/parser/object.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

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