formatting and adding video model
This commit is contained in:
parent
de34c6818a
commit
887fc733e7
8 changed files with 196 additions and 84 deletions
|
@ -1,15 +1,18 @@
|
|||
use std::{collections::HashMap, mem::replace, path::PathBuf};
|
||||
|
||||
use color_eyre::eyre::{eyre, Report, Result};
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use sqlx::{query, Connection, SqliteConnection};
|
||||
use tracing::{debug, error};
|
||||
|
||||
use crate::{model::{Model, Models}, slides::{Background, TextAlignment}};
|
||||
use crate::{
|
||||
model::{Model, Modeling},
|
||||
slides::{Background, TextAlignment},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Song {
|
||||
pub title: String,
|
||||
pub lyrics: String,
|
||||
pub lyrics: Option<String>,
|
||||
pub author: String,
|
||||
pub ccli: String,
|
||||
pub audio: PathBuf,
|
||||
|
@ -22,15 +25,14 @@ pub struct Song {
|
|||
}
|
||||
|
||||
const VERSE_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",
|
||||
"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 Models for Model<Song> {
|
||||
impl Modeling for Model<Song> {
|
||||
type Item = Song;
|
||||
fn add_item(&mut self, item: Self::Item) -> Result<()> {
|
||||
self.items.push(item);
|
||||
|
@ -41,12 +43,20 @@ impl Models for Model<Song> {
|
|||
todo!()
|
||||
}
|
||||
|
||||
fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()> {
|
||||
if let Some(current_song) = self.items.get_mut(index as usize) {
|
||||
fn update_item(
|
||||
&mut self,
|
||||
item: Self::Item,
|
||||
index: i32,
|
||||
) -> Result<()> {
|
||||
if let Some(current_song) = self.items.get_mut(index as usize)
|
||||
{
|
||||
let _old_song = replace(current_song, item);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(eyre!("Song doesn't exist in model. Id was {}", index))
|
||||
Err(eyre!(
|
||||
"Song doesn't exist in model. Id was {}",
|
||||
index
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,41 +68,18 @@ impl Models for Model<Song> {
|
|||
self.items.remove(index as usize);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Model<Song> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
items: vec![],
|
||||
db: {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
let mut data = dirs::data_local_dir().unwrap();
|
||||
data.push("lumina");
|
||||
data.push("library-db.sqlite3");
|
||||
let mut db_url = String::from("sqlite://");
|
||||
db_url.push_str(data.to_str().unwrap());
|
||||
rt.block_on(async {
|
||||
SqliteConnection::connect(&db_url).await.expect("problems")
|
||||
})
|
||||
}
|
||||
}
|
||||
fn insert_item(
|
||||
&mut self,
|
||||
item: Self::Item,
|
||||
index: i32,
|
||||
) -> Result<()> {
|
||||
self.items.insert(index as usize, item);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Model<Song> {
|
||||
pub fn get_song(&self, id: i32) -> Option<&Song> {
|
||||
self.items.get(id as usize)
|
||||
}
|
||||
|
||||
pub fn remove_song(&mut self, id: i32) -> Result<(), Report> {
|
||||
if let Some(_song) = self.items.get(id as usize) {
|
||||
let _song = self.items.remove(id as usize);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(eyre!("Song doesn't exist in model. Id was {}", id))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_from_db(&mut self) {
|
||||
// static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3";
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
|
@ -101,9 +88,9 @@ impl Model<Song> {
|
|||
match result {
|
||||
Ok(s) => {
|
||||
for song in s.into_iter() {
|
||||
self.add_song(Song {
|
||||
let _ = self.add_item(Song {
|
||||
title: song.title,
|
||||
lyrics: song.lyrics,
|
||||
lyrics: Some(song.lyrics),
|
||||
author: song.author,
|
||||
ccli: song.ccli,
|
||||
audio: song.audio.into(),
|
||||
|
@ -133,9 +120,9 @@ impl Model<Song> {
|
|||
impl Song {
|
||||
pub fn get_lyrics(&self) -> Result<Vec<String>> {
|
||||
let mut lyric_list = Vec::new();
|
||||
let raw_lyrics = self.lyrics.as_str();
|
||||
let verse_order =
|
||||
self.verse_order.clone();
|
||||
if let Some(raw_lyrics) = self.lyrics.clone() {
|
||||
let raw_lyrics = raw_lyrics.as_str();
|
||||
let verse_order = self.verse_order.clone();
|
||||
|
||||
let mut lyric_map = HashMap::new();
|
||||
let mut verse_title = String::from("");
|
||||
|
@ -191,14 +178,13 @@ impl Song {
|
|||
for lyric in lyric_list.iter() {
|
||||
debug!(lyric = ?lyric)
|
||||
}
|
||||
Ok(lyric_list)
|
||||
Ok(lyric_list)
|
||||
} else {
|
||||
Err(eyre!("There are no lyrics"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
@ -206,7 +192,8 @@ mod test {
|
|||
#[test]
|
||||
pub fn test_song_lyrics() {
|
||||
let mut song = Song::default();
|
||||
song.lyrics = "Verse 1
|
||||
song.lyrics = Some(
|
||||
"Verse 1
|
||||
When You found me,
|
||||
I was so blind
|
||||
My sin was before me,
|
||||
|
@ -262,12 +249,23 @@ 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().split(' ').map(|s| s.to_string()).collect();
|
||||
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()
|
||||
.split(' ')
|
||||
.map(|s| s.to_string())
|
||||
.collect();
|
||||
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, "{:?}", e) },
|
||||
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, "{:?}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -275,13 +273,12 @@ You saved my soul".to_string();
|
|||
pub fn test_db_and_model() {
|
||||
let mut song_model = Model::default();
|
||||
song_model.load_from_db();
|
||||
if let Some(song) = song_model.get_song(3) {
|
||||
if let Some(song) = song_model.get_item(3) {
|
||||
let test_song = test_song();
|
||||
assert_eq!(test_song.title, song.title);
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -292,10 +289,12 @@ You saved my soul".to_string();
|
|||
song_model.load_from_db();
|
||||
|
||||
match song_model.update_item(song, 2) {
|
||||
Ok(()) => assert_eq!(&cloned_song, song_model.get_song(2).unwrap()),
|
||||
Ok(()) => assert_eq!(
|
||||
&cloned_song,
|
||||
song_model.get_item(2).unwrap()
|
||||
),
|
||||
Err(e) => assert!(false, "{:?}", e),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn test_song() -> Song {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue