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;
#[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<String>;
pub type Plan = Vec<String>;
pub struct Song {
pub name: String,
pub verses: BTreeMap<String, Verse>,
pub other_maps: BTreeMap<String, SongMap>,
pub default_map: SongMap,
pub other_plans: BTreeMap<String, Plan>,
pub default_plan: Plan,
}
impl Song {
pub fn song_map(&self, map_name: Option<String>) -> &Vec<String> {
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<String>) -> &Vec<String> {
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 {