From 00944b5db9ee37ab398e6b3168d87e12f1e10aad Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 6 Jul 2024 10:12:21 -0500 Subject: [PATCH] WIP --- src/model.rs | 56 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/src/model.rs b/src/model.rs index 2c8f2c2..d6f0ea1 100644 --- a/src/model.rs +++ b/src/model.rs @@ -6,26 +6,37 @@ mod song { use regex::Regex; + #[derive(Debug, PartialEq, Eq)] pub struct Verse { // pub background: String, // url pub content: String, } + impl Verse { + fn new(content: String) -> Self { + Self { content } + } + } + /// Sequence of verse names. - pub type SongMap = Vec; + pub type Plan = Vec; pub struct Song { pub name: String, pub verses: BTreeMap, - pub other_maps: BTreeMap, - pub default_map: SongMap, + pub other_plans: BTreeMap, + pub default_plan: Plan, } impl Song { - pub fn song_map(&self, map_name: Option) -> &Vec { - map_name - .map(|map_name| self.other_maps.get(&map_name).unwrap_or(&self.default_map)) - .unwrap_or(&self.default_map) + pub fn plan(&self, plan_name: Option) -> &Vec { + plan_name + .map(|plan_name| { + self.other_plans + .get(&plan_name) + .unwrap_or(&self.default_plan) + }) + .unwrap_or(&self.default_plan) } } @@ -52,7 +63,7 @@ mod song { hunks.push_back(&s[last_end..m.start()]); last_end = m.end(); } - hunks.push_back(&s[last_end..s.len() - 1]); + hunks.push_back(&s[last_end..s.len()]); // process header let mut header_lines = hunks.pop_front().unwrap().lines(); @@ -68,26 +79,27 @@ mod song { // process verses for hunk in hunks { let mut verse_contents = hunk; - let first_line = &hunk[0..hunk.find('\n').unwrap_or(hunk.len())]; - let verse_name: String = if let Some(i) = first_line.find(':') { - String::from(&first_line[0..i]) + let end_i = hunk.find('\n').unwrap_or(hunk.len()); + let verse_name: String = if let Some(i) = &hunk[0..end_i].find(':') { + verse_contents = &hunk[end_i + 1..]; + String::from(&hunk[0..*i]) } else { - format!("Generated Verse {}", verses.len()).to_owned() + format!("Generated Verse {}", verses.len() + 1).to_owned() }; - verses.insert(verse_name, verse_content); + verses.insert(verse_name, Verse::new(verse_contents.to_owned())); } Ok(Self { name, verses, - other_maps: BTreeMap::new(), - default_map: vec![], + other_plans: BTreeMap::new(), + default_plan: vec![], }) } } mod test { - use super::Song; + use super::{Song, Verse}; #[test] fn parses_simple_song() { @@ -98,17 +110,25 @@ mod song { .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.len(), 1); + assert_eq!(song.default_plan[0], "Generated Verse 1"); } } } mod display { - use super::song::{Song, SongMap}; + use super::song::{Plan, Song}; pub struct PlaylistEntry { pub song: Song, - pub map: SongMap, + pub map: Plan, } pub struct PlaylistVerseRef {