organic/src/parser/diary_sexp.rs

69 lines
2.0 KiB
Rust
Raw Normal View History

use nom::bytes::complete::is_not;
2023-04-21 20:26:49 -04:00
use nom::bytes::complete::tag;
2023-10-05 03:46:14 -04:00
use nom::combinator::recognize;
use nom::multi::many0;
2023-04-21 20:53:55 -04:00
use nom::sequence::tuple;
2023-04-21 20:26:49 -04:00
use super::affiliated_keyword::parse_affiliated_keywords;
use super::keyword::affiliated_keyword;
use super::org_source::OrgSource;
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
2023-10-05 03:46:14 -04:00
use super::util::org_line_ending;
use crate::context::parser_with_context;
2023-09-03 12:23:18 -04:00
use crate::context::RefContext;
2023-04-21 20:22:31 -04:00
use crate::error::Res;
2023-04-21 20:26:49 -04:00
use crate::parser::util::get_consumed;
use crate::parser::util::start_of_line;
2023-09-03 12:23:18 -04:00
use crate::types::DiarySexp;
use crate::types::Keyword;
2023-04-21 20:22:31 -04:00
2023-10-09 18:00:48 -04:00
#[cfg_attr(
feature = "tracing",
2023-10-14 19:02:35 -04:00
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
2023-10-09 18:00:48 -04:00
)]
pub(crate) fn diary_sexp<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, DiarySexp<'s>>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
start_of_line(remaining)?;
let (remaining, value) = recognize(tuple((tag("%%("), is_not("\r\n"))))(remaining)?;
2023-10-05 03:46:14 -04:00
let (remaining, _eol) = org_line_ending(remaining)?;
2023-04-21 20:53:55 -04:00
let (remaining, _trailing_ws) =
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
2023-04-21 20:26:49 -04:00
let source = get_consumed(input, remaining);
Ok((
remaining,
DiarySexp {
source: source.into(),
affiliated_keywords: parse_affiliated_keywords(
context.get_global_settings(),
affiliated_keywords,
),
2023-10-05 03:46:14 -04:00
value: Into::<&str>::into(value),
},
))
2023-04-21 20:22:31 -04:00
}
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
)]
pub(crate) fn detect_diary_sexp<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
tuple((start_of_line, tag("%%(")))(remaining)?;
Ok((input, ()))
}