Parses plans

This commit is contained in:
Daniel Flanagan 2024-07-06 18:10:00 -05:00
parent df4cd420ad
commit 9bf90e32b1

View file

@ -72,16 +72,28 @@ mod song {
hunks.push_back(&s[last_end..s.len()]); hunks.push_back(&s[last_end..s.len()]);
// process header // process header
let mut header_lines = hunks.pop_front().unwrap().lines(); let mut header_lines = hunks.pop_front().unwrap().lines().map(|s| s.trim());
let name = header_lines.next().unwrap().trim().to_owned(); let name = header_lines.next().unwrap().trim().to_owned();
let mut other_plans = BTreeMap::new(); let mut other_plans = BTreeMap::new();
for (line_number, line) in header_lines.enumerate() { for (line_number, line) in header_lines.enumerate() {
if line.starts_with("map(") { if line.starts_with("plan(") {
match line.find(":") { if let Some(end) = line.find(")") {
Some(i) => {} match line[end..].find(":") {
Some(i) => {
let plan_name = &line[5..end];
let entries: VecDeque<String> = line[(end + i + 1)..]
.trim()
.split(',')
.map(|s| s.trim().to_owned())
.collect();
other_plans.insert(plan_name.to_owned(), entries);
}
None => { None => {
return Err(SongParseError::InvalidMetadata(SourceRef { line_number })) return Err(SongParseError::InvalidMetadata(SourceRef {
line_number,
}));
}
} }
} }
} }
@ -117,6 +129,8 @@ mod song {
} }
mod test { mod test {
use std::collections::VecDeque;
use super::{Song, Verse}; use super::{Song, Verse};
#[test] #[test]
@ -138,6 +152,36 @@ mod song {
assert_eq!(song.default_plan[0], "Generated Verse 1"); assert_eq!(song.default_plan[0], "Generated Verse 1");
assert_eq!(song.default_plan.len(), 1); assert_eq!(song.default_plan.len(), 1);
} }
#[test]
fn parses_song_with_plan() {
let song: Song = r#"Song Title
plan(another_plan): Generated Verse 1, Generated Verse 1, Generated Verse 1
A verse"#
.parse()
.unwrap();
assert_eq!(song.name, "Song Title");
assert_eq!(
song.verses.get("Generated Verse 1"),
Some(&Verse {
content: "A verse".to_owned()
})
);
assert_eq!(song.verses.len(), 1);
assert_eq!(song.default_plan[0], "Generated Verse 1");
assert_eq!(song.default_plan.len(), 1);
dbg!(&song.other_plans);
assert_eq!(
song.other_plans.get("another_plan"),
Some(&VecDeque::from(vec![
"Generated Verse 1".to_owned(),
"Generated Verse 1".to_owned(),
"Generated Verse 1".to_owned()
]))
);
}
} }
} }