use crate::ast::{TimelineDiagram, TimelineDirection, TimelineSection};
use crate::parser::common::{
PResult, attempt, consume_line, has_input, keyword, rest_of_line, skip_ws_and_comments,
};
use winnow::{Parser, combinator::alt};
pub fn timeline_diagram<'i>(input: &mut &'i str) -> PResult<'i, TimelineDiagram> {
keyword("timeline").parse_next(input)?;
skip_ws_and_comments(input)?;
let mut direction = None;
skip_ws_and_comments(input)?;
if let Some(dir) = attempt(
alt((
keyword("TD").map(|_| TimelineDirection::TD),
keyword("LR").map(|_| TimelineDirection::LR),
)),
input,
) {
direction = Some(dir);
consume_line(input)?;
}
let mut title = None;
let mut sections: Vec<TimelineSection> = Vec::new();
let mut current_section: String = String::new();
let mut explicit_section = false;
while has_input(input) {
skip_ws_and_comments(input)?;
if !has_input(input) {
break;
}
let line = rest_of_line.parse_next(input)?;
if line.is_empty() {
continue;
}
let trimmed = line.trim();
if let Some(name) = trimmed.strip_prefix("section ") {
explicit_section = true;
current_section = name.trim().to_string();
if !sections.iter().any(|s| s.name == current_section) {
sections.push(TimelineSection {
name: current_section.clone(),
events: Vec::new(),
});
}
} else if trimmed
.get(..5)
.is_some_and(|h| h.eq_ignore_ascii_case("title"))
{
let t = trimmed.get(5..).map(str::trim).unwrap_or("");
if !t.is_empty() {
title = Some(t.to_string());
}
} else if let Some(idx) = trimmed.find(':') {
let first = trimmed[..idx].trim();
let rest = &trimmed[idx + 1..];
let mut events: Vec<String> = rest
.split(':')
.map(|e| e.trim().to_string())
.filter(|e| !e.is_empty())
.collect();
if !explicit_section {
current_section = first.to_string();
sections.push(TimelineSection {
name: current_section.clone(),
events,
});
} else {
let mut all = vec![first.to_string()];
all.append(&mut events);
if let Some(sec) = sections
.iter_mut()
.find(|s: &&mut TimelineSection| s.name == current_section)
{
sec.events.extend(all);
} else {
sections.push(TimelineSection {
name: current_section.clone(),
events: all,
});
}
}
}
}
Ok(TimelineDiagram {
title,
direction,
sections,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(input: &str) -> TimelineDiagram {
let mut stream: &str = input;
let d = timeline_diagram.parse_next(&mut stream).unwrap();
assert!(stream.is_empty(), "trailing input: {:?}", stream);
d
}
#[test]
fn basic_timeline() {
let d = parse("timeline\n1950 : A : B\n2000 : C");
assert_eq!(d.sections.len(), 2);
assert_eq!(d.sections[0].name, "1950");
assert_eq!(d.sections[0].events, vec!["A", "B"]);
assert_eq!(d.sections[1].name, "2000");
assert_eq!(d.sections[1].events, vec!["C"]);
}
#[test]
fn title_and_sections() {
let d = parse(
"timeline\ntitle My History\nsection Early\n1900 : Born\nsection Later\n1950 : Retire",
);
assert_eq!(d.title.as_deref(), Some("My History"));
assert_eq!(d.sections.len(), 2);
assert_eq!(d.sections[0].name, "Early");
assert_eq!(d.sections[0].events, vec!["1900", "Born"]);
assert_eq!(d.sections[1].name, "Later");
}
#[test]
fn direction_lr() {
let d = parse("timeline LR\n2000 : X");
assert_eq!(d.direction, Some(TimelineDirection::LR));
}
#[test]
fn multi_byte_before_title_keyword_does_not_panic() {
let d = parse("timeline\nab😀 title with text\n2000 : X");
assert_eq!(d.sections.len(), 1);
assert_eq!(d.sections[0].name, "2000");
}
}