2023-04-22 01:54:08 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::bytes::complete::tag_no_case;
|
2023-09-16 01:28:40 +00:00
|
|
|
use nom::character::complete::space0;
|
|
|
|
use nom::character::complete::space1;
|
2023-10-03 00:25:08 +00:00
|
|
|
use nom::combinator::map;
|
2023-09-14 22:14:26 +00:00
|
|
|
use nom::multi::many1;
|
2023-04-22 01:54:08 +00:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
|
2023-08-23 04:30:26 +00:00
|
|
|
use super::org_source::OrgSource;
|
2023-09-14 22:14:26 +00:00
|
|
|
use super::timestamp::timestamp;
|
2023-09-08 20:30:40 +00:00
|
|
|
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
2023-09-14 22:14:26 +00:00
|
|
|
use super::util::org_line_ending;
|
|
|
|
use crate::context::parser_with_context;
|
2023-09-03 16:23:18 +00:00
|
|
|
use crate::context::RefContext;
|
2023-04-22 01:33:23 +00:00
|
|
|
use crate::error::Res;
|
2023-04-22 01:54:08 +00:00
|
|
|
use crate::parser::util::get_consumed;
|
|
|
|
use crate::parser::util::start_of_line;
|
2023-09-03 16:23:18 +00:00
|
|
|
use crate::types::Planning;
|
2023-10-03 00:25:08 +00:00
|
|
|
use crate::types::Timestamp;
|
2023-04-22 01:33:23 +00:00
|
|
|
|
2023-08-11 00:04:59 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-11 17:13:28 +00:00
|
|
|
pub(crate) fn planning<'b, 'g, 'r, 's>(
|
2023-09-08 20:30:40 +00:00
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
2023-08-23 04:30:26 +00:00
|
|
|
input: OrgSource<'s>,
|
|
|
|
) -> Res<OrgSource<'s>, Planning<'s>> {
|
2023-08-24 23:29:00 +00:00
|
|
|
start_of_line(input)?;
|
2023-09-16 01:28:40 +00:00
|
|
|
let (remaining, _leading_whitespace) = space0(input)?;
|
2023-10-03 00:25:08 +00:00
|
|
|
let (remaining, planning_parameters) =
|
2023-09-14 22:14:26 +00:00
|
|
|
many1(parser_with_context!(planning_parameter)(context))(remaining)?;
|
2023-09-16 01:28:40 +00:00
|
|
|
let (remaining, _trailing_ws) = tuple((space0, org_line_ending))(remaining)?;
|
2023-04-22 01:54:08 +00:00
|
|
|
|
2023-09-08 20:30:40 +00:00
|
|
|
let (remaining, _trailing_ws) =
|
|
|
|
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
2023-04-22 01:54:08 +00:00
|
|
|
let source = get_consumed(input, remaining);
|
|
|
|
|
2023-10-03 00:25:08 +00:00
|
|
|
let mut scheduled = None;
|
|
|
|
let mut deadline = None;
|
|
|
|
let mut closed = None;
|
|
|
|
|
|
|
|
for (timestamp_type, timestamp) in planning_parameters.into_iter() {
|
|
|
|
match timestamp_type {
|
|
|
|
PlanningTimestampType::Scheduled => {
|
|
|
|
scheduled = Some(timestamp);
|
|
|
|
}
|
|
|
|
PlanningTimestampType::Deadline => {
|
|
|
|
deadline = Some(timestamp);
|
|
|
|
}
|
|
|
|
PlanningTimestampType::Closed => {
|
|
|
|
closed = Some(timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 04:30:26 +00:00
|
|
|
Ok((
|
|
|
|
remaining,
|
|
|
|
Planning {
|
|
|
|
source: source.into(),
|
2023-10-03 00:25:08 +00:00
|
|
|
scheduled,
|
|
|
|
deadline,
|
|
|
|
closed,
|
2023-08-23 04:30:26 +00:00
|
|
|
},
|
|
|
|
))
|
2023-04-22 01:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 02:45:25 +00:00
|
|
|
#[derive(Debug)]
|
2023-10-03 00:25:08 +00:00
|
|
|
enum PlanningTimestampType {
|
|
|
|
Scheduled,
|
|
|
|
Deadline,
|
|
|
|
Closed,
|
|
|
|
}
|
|
|
|
|
2023-08-11 00:04:59 +00:00
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
2023-09-14 22:14:26 +00:00
|
|
|
fn planning_parameter<'b, 'g, 'r, 's>(
|
|
|
|
context: RefContext<'b, 'g, 'r, 's>,
|
|
|
|
input: OrgSource<'s>,
|
2023-10-03 00:25:08 +00:00
|
|
|
) -> Res<OrgSource<'s>, (PlanningTimestampType, Timestamp<'s>)> {
|
|
|
|
let (remaining, planning_type) = alt((
|
|
|
|
map(tag_no_case("DEADLINE"), |_| PlanningTimestampType::Deadline),
|
|
|
|
map(tag_no_case("SCHEDULED"), |_| {
|
|
|
|
PlanningTimestampType::Scheduled
|
|
|
|
}),
|
|
|
|
map(tag_no_case("CLOSED"), |_| PlanningTimestampType::Closed),
|
2023-04-22 01:54:08 +00:00
|
|
|
))(input)?;
|
2023-09-16 01:28:40 +00:00
|
|
|
let (remaining, _gap) = tuple((tag(":"), space1))(remaining)?;
|
2023-10-03 00:25:08 +00:00
|
|
|
let (remaining, timestamp) = timestamp(context, remaining)?;
|
|
|
|
Ok((remaining, (planning_type, timestamp)))
|
2023-04-22 01:33:23 +00:00
|
|
|
}
|