fleshing out the core a bit more
This commit is contained in:
parent
e05815e550
commit
c8bb484a53
13 changed files with 497 additions and 9 deletions
172
src/rust/core/songs.rs
Normal file
172
src/rust/core/songs.rs
Normal file
|
@ -0,0 +1,172 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use color_eyre::eyre::Result;
|
||||
use tracing::{debug, error};
|
||||
|
||||
use crate::slides::TextAlignment;
|
||||
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Song {
|
||||
pub title: String,
|
||||
pub lyrics: String,
|
||||
pub author: String,
|
||||
pub ccli: String,
|
||||
pub audio: String,
|
||||
pub verse_order: String,
|
||||
pub background: String,
|
||||
pub background_type: String,
|
||||
pub text_alignment: TextAlignment,
|
||||
pub horizontal_text_alignment: String,
|
||||
pub vertical_text_alignment: String,
|
||||
pub font: String,
|
||||
pub font_size: i32,
|
||||
}
|
||||
|
||||
const KEYWORDS: [&'static str; 24] = [
|
||||
"Verse 1", "Verse 2", "Verse 3", "Verse 4",
|
||||
"Verse 5", "Verse 6", "Verse 7", "Verse 8",
|
||||
"Chorus 1", "Chorus 2", "Chorus 3", "Chorus 4",
|
||||
"Bridge 1", "Bridge 2", "Bridge 3", "Bridge 4",
|
||||
"Intro 1", "Intro 2", "Ending 1", "Ending 2",
|
||||
"Other 1", "Other 2", "Other 3", "Other 4",
|
||||
];
|
||||
|
||||
impl Song {
|
||||
pub fn get_lyrics(&self) -> Result<Vec<String>> {
|
||||
let mut lyric_list = Vec::new();
|
||||
let raw_lyrics = self.lyrics.as_str();
|
||||
let vorder: Vec<&str> =
|
||||
self.verse_order.split(' ').collect();
|
||||
let _first_item = true;
|
||||
|
||||
let mut lyric_map = HashMap::new();
|
||||
let mut verse_title = String::from("");
|
||||
let mut lyric = String::from("");
|
||||
for (i, line) in raw_lyrics.split('\n').enumerate() {
|
||||
if KEYWORDS.contains(&line) {
|
||||
if i != 0 {
|
||||
lyric_map.insert(verse_title, lyric);
|
||||
lyric = String::from("");
|
||||
verse_title = line.to_string();
|
||||
} else {
|
||||
verse_title = line.to_string();
|
||||
}
|
||||
} else {
|
||||
lyric.push_str(line);
|
||||
lyric.push('\n');
|
||||
}
|
||||
}
|
||||
lyric_map.insert(verse_title, lyric);
|
||||
|
||||
for verse in vorder {
|
||||
let mut verse_name = "";
|
||||
debug!(verse = verse);
|
||||
for word in KEYWORDS.clone() {
|
||||
let end_verse =
|
||||
verse.get(1..2).unwrap_or_default();
|
||||
let beg_verse =
|
||||
verse.get(0..1).unwrap_or_default();
|
||||
if word.starts_with(beg_verse)
|
||||
&& word.ends_with(end_verse)
|
||||
{
|
||||
verse_name = word;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Some(lyric) = lyric_map.get(verse_name) {
|
||||
if lyric.contains("\n\n") {
|
||||
let split_lyrics: Vec<&str> =
|
||||
lyric.split("\n\n").collect();
|
||||
for lyric in split_lyrics {
|
||||
if lyric.is_empty() {
|
||||
continue;
|
||||
}
|
||||
lyric_list.push(lyric.to_string());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
lyric_list.push(lyric.to_string());
|
||||
} else {
|
||||
error!("NOT WORKING!");
|
||||
};
|
||||
}
|
||||
for lyric in lyric_list.iter() {
|
||||
debug!(lyric = ?lyric)
|
||||
}
|
||||
Ok(lyric_list)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
pub fn test_song_lyrics() {
|
||||
let mut song = Song::default();
|
||||
song.lyrics = "Verse 1
|
||||
When You found me,
|
||||
I was so blind
|
||||
My sin was before me,
|
||||
I was swallowed by pride
|
||||
|
||||
Chorus 1
|
||||
But out of the darkness,
|
||||
You brought me to Your light
|
||||
You showed me new mercy
|
||||
And opened up my eyes
|
||||
|
||||
Chorus 2
|
||||
From the day
|
||||
You saved my soul
|
||||
'Til the very moment
|
||||
When I come home
|
||||
|
||||
I'll sing, I'll dance,
|
||||
My heart will overflow
|
||||
From the day
|
||||
You saved my soul
|
||||
|
||||
Verse 2
|
||||
Where brilliant light
|
||||
Is all around
|
||||
And endless joy
|
||||
Is the only sound
|
||||
|
||||
Chorus 3
|
||||
Oh, rest my heart
|
||||
Forever now
|
||||
Oh, in Your arms
|
||||
I'll always be found
|
||||
|
||||
Bridge 1
|
||||
My love is Yours
|
||||
My heart is Yours
|
||||
My life is Yours
|
||||
Forever
|
||||
|
||||
My love is Yours
|
||||
My heart is Yours
|
||||
My life is Yours
|
||||
Forever
|
||||
|
||||
Other 1
|
||||
From the Day
|
||||
I Am They
|
||||
|
||||
Other 2
|
||||
|
||||
|
||||
Ending 1
|
||||
Oh Oh Oh
|
||||
From the day
|
||||
You saved my soul".to_string();
|
||||
song.verse_order = "O1 V1 C1 C2 O2 V2 C3 C2 O2 B1 C2 C2 E1 O2".to_string();
|
||||
let lyrics = song.get_lyrics();
|
||||
match lyrics {
|
||||
Ok(lyrics) => { assert_eq!(vec!["From the Day\nI Am They", "When You found me,\nI was so blind\nMy sin was before me,\nI was swallowed by pride", "But out of the darkness,\nYou brought me to Your light\nYou showed me new mercy\nAnd opened up my eyes", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "Where brilliant light\nIs all around\nAnd endless joy\nIs the only sound", "Oh, rest my heart\nForever now\nOh, in Your arms\nI'll always be found", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "My love is Yours\nMy heart is Yours\nMy life is Yours\nForever", "My love is Yours\nMy heart is Yours\nMy life is Yours\nForever", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "Oh Oh Oh\nFrom the day\nYou saved my soul\n"], lyrics); },
|
||||
Err(e) => { assert!(false) },
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue