This commit is contained in:
Daniel Flanagan 2024-07-06 10:12:21 -05:00
parent 3803df7221
commit 00944b5db9

View file

@ -6,26 +6,37 @@ mod song {
use regex::Regex; use regex::Regex;
#[derive(Debug, PartialEq, Eq)]
pub struct Verse { pub struct Verse {
// pub background: String, // url // pub background: String, // url
pub content: String, pub content: String,
} }
impl Verse {
fn new(content: String) -> Self {
Self { content }
}
}
/// Sequence of verse names. /// Sequence of verse names.
pub type SongMap = Vec<String>; pub type Plan = Vec<String>;
pub struct Song { pub struct Song {
pub name: String, pub name: String,
pub verses: BTreeMap<String, Verse>, pub verses: BTreeMap<String, Verse>,
pub other_maps: BTreeMap<String, SongMap>, pub other_plans: BTreeMap<String, Plan>,
pub default_map: SongMap, pub default_plan: Plan,
} }
impl Song { impl Song {
pub fn song_map(&self, map_name: Option<String>) -> &Vec<String> { pub fn plan(&self, plan_name: Option<String>) -> &Vec<String> {
map_name plan_name
.map(|map_name| self.other_maps.get(&map_name).unwrap_or(&self.default_map)) .map(|plan_name| {
.unwrap_or(&self.default_map) 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()]); hunks.push_back(&s[last_end..m.start()]);
last_end = m.end(); last_end = m.end();
} }
hunks.push_back(&s[last_end..s.len() - 1]); 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();
@ -68,26 +79,27 @@ mod song {
// process verses // process verses
for hunk in hunks { for hunk in hunks {
let mut verse_contents = hunk; let mut verse_contents = hunk;
let first_line = &hunk[0..hunk.find('\n').unwrap_or(hunk.len())]; let end_i = hunk.find('\n').unwrap_or(hunk.len());
let verse_name: String = if let Some(i) = first_line.find(':') { let verse_name: String = if let Some(i) = &hunk[0..end_i].find(':') {
String::from(&first_line[0..i]) verse_contents = &hunk[end_i + 1..];
String::from(&hunk[0..*i])
} else { } 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 { Ok(Self {
name, name,
verses, verses,
other_maps: BTreeMap::new(), other_plans: BTreeMap::new(),
default_map: vec![], default_plan: vec![],
}) })
} }
} }
mod test { mod test {
use super::Song; use super::{Song, Verse};
#[test] #[test]
fn parses_simple_song() { fn parses_simple_song() {
@ -98,17 +110,25 @@ mod song {
.unwrap(); .unwrap();
assert_eq!(song.name, "Song Title"); 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.verses.len(), 1);
assert_eq!(song.default_plan.len(), 1);
assert_eq!(song.default_plan[0], "Generated Verse 1");
} }
} }
} }
mod display { mod display {
use super::song::{Song, SongMap}; use super::song::{Plan, Song};
pub struct PlaylistEntry { pub struct PlaylistEntry {
pub song: Song, pub song: Song,
pub map: SongMap, pub map: Plan,
} }
pub struct PlaylistVerseRef { pub struct PlaylistVerseRef {