From 887fc733e7e0b5538a3d92da191bd984b2a3c5c9 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Mon, 7 Oct 2024 11:43:45 -0500 Subject: [PATCH] formatting and adding video model --- src/rust/core/images.rs | 2 +- src/rust/core/kinds.rs | 4 +- src/rust/core/lib.rs | 8 +-- src/rust/core/model.rs | 68 ++++++++++++++++-- src/rust/core/presentations.rs | 1 - src/rust/core/slides.rs | 18 +++-- src/rust/core/songs.rs | 123 ++++++++++++++++----------------- src/rust/core/videos.rs | 56 ++++++++++++++- 8 files changed, 196 insertions(+), 84 deletions(-) diff --git a/src/rust/core/images.rs b/src/rust/core/images.rs index 8d2cf08..67ea561 100644 --- a/src/rust/core/images.rs +++ b/src/rust/core/images.rs @@ -1,5 +1,5 @@ pub struct Image { - title: String + title: String, } #[cfg(test)] diff --git a/src/rust/core/kinds.rs b/src/rust/core/kinds.rs index b5b3be8..7c45a57 100644 --- a/src/rust/core/kinds.rs +++ b/src/rust/core/kinds.rs @@ -53,7 +53,9 @@ impl From for String { ServiceItemKind::Song => "song".to_owned(), ServiceItemKind::Video => "video".to_owned(), ServiceItemKind::Image => "image".to_owned(), - ServiceItemKind::Presentation(_) => "presentation".to_owned(), + ServiceItemKind::Presentation(_) => { + "presentation".to_owned() + } ServiceItemKind::Content => "content".to_owned(), } } diff --git a/src/rust/core/lib.rs b/src/rust/core/lib.rs index 5e046d1..b505e4d 100644 --- a/src/rust/core/lib.rs +++ b/src/rust/core/lib.rs @@ -1,8 +1,8 @@ -pub mod service_items; +pub mod images; pub mod kinds; +pub mod model; +pub mod presentations; +pub mod service_items; pub mod slides; pub mod songs; pub mod videos; -pub mod presentations; -pub mod images; -pub mod model; diff --git a/src/rust/core/model.rs b/src/rust/core/model.rs index cec6f9f..c3bfccf 100644 --- a/src/rust/core/model.rs +++ b/src/rust/core/model.rs @@ -1,15 +1,62 @@ use color_eyre::eyre::Result; use sqlx::{Connection, SqliteConnection}; -use crate::songs::Song; - #[derive(Debug)] pub struct Model { pub items: Vec, pub db: SqliteConnection, } -pub trait Models { +// impl Modeling for Model { +// type Item = T; +// fn add_item(&mut self, item: Self::Item) -> Result<()> { +// self.items.push(item); +// Ok(()) +// } + +// fn add_to_db(&mut self, item: Self::Item) -> Result<()> { +// todo!() +// } + +// fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()> { +// todo!() +// } + +// fn remove_item(&mut self, index: i32) -> Result<()> { +// todo!() +// } + +// fn get_item(&self, index: i32) -> Option<&Self::Item> { +// todo!() +// } + +// fn insert_item(&mut self, item: Self::Item, index: i32) -> Result<()> { +// todo!() +// } +// } + +impl Default for Model { + 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") + }) + }, + } + } +} + +pub trait Modeling { type Item; fn setup_db() -> SqliteConnection { @@ -20,15 +67,26 @@ pub trait Models { 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") + SqliteConnection::connect(&db_url) + .await + .expect("problems") }) } fn add_item(&mut self, item: Self::Item) -> Result<()>; fn add_to_db(&mut self, item: Self::Item) -> Result<()>; - fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()>; + fn update_item( + &mut self, + item: Self::Item, + index: i32, + ) -> Result<()>; fn remove_item(&mut self, index: i32) -> Result<()>; fn get_item(&self, index: i32) -> Option<&Self::Item>; + fn insert_item( + &mut self, + item: Self::Item, + index: i32, + ) -> Result<()>; } #[cfg(test)] diff --git a/src/rust/core/presentations.rs b/src/rust/core/presentations.rs index 0327de7..9b1fde2 100644 --- a/src/rust/core/presentations.rs +++ b/src/rust/core/presentations.rs @@ -1,4 +1,3 @@ - #[derive(Debug, Clone, Default, PartialEq, Eq)] pub enum PresKind { Html, diff --git a/src/rust/core/slides.rs b/src/rust/core/slides.rs index 2ac04a8..31d2639 100644 --- a/src/rust/core/slides.rs +++ b/src/rust/core/slides.rs @@ -2,7 +2,10 @@ use std::path::PathBuf; use color_eyre::eyre::Result; -use crate::{images::Image, kinds::ServiceItemKind, presentations::Presentation, songs::Song, videos::Video}; +use crate::{ + images::Image, kinds::ServiceItemKind, + presentations::Presentation, songs::Song, videos::Video, +}; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum TextAlignment { @@ -22,7 +25,7 @@ pub enum TextAlignment { pub enum Background { #[default] Image, - Video + Video, } impl From for Background { @@ -100,10 +103,11 @@ impl From for Slide { } impl SlideModel { - pub fn add_song_to_end(&mut self,song: Song) -> Result<()> { + pub fn add_song_to_end(&mut self, song: Song) -> Result<()> { let lyrics = song.get_lyrics()?; - let mut slides: Vec = lyrics.iter().map(|lyric| { - Slide { + let mut slides: Vec = lyrics + .iter() + .map(|lyric| Slide { background: song.background.clone(), text: lyric.to_owned(), font: song.font.clone(), @@ -111,8 +115,8 @@ impl SlideModel { kind: ServiceItemKind::Song, text_alignment: song.text_alignment, ..Default::default() - } - }).collect(); + }) + .collect(); self.slides.append(&mut slides); Ok(()) } diff --git a/src/rust/core/songs.rs b/src/rust/core/songs.rs index f48f81a..b9c3e13 100644 --- a/src/rust/core/songs.rs +++ b/src/rust/core/songs.rs @@ -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, 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 { +impl Modeling for Model { type Item = Song; fn add_item(&mut self, item: Self::Item) -> Result<()> { self.items.push(item); @@ -41,12 +43,20 @@ impl Models for Model { 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 { self.items.remove(index as usize); Ok(()) } -} -impl Default for Model { - 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 { - 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 { 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 { impl Song { pub fn get_lyrics(&self) -> Result> { 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 { diff --git a/src/rust/core/videos.rs b/src/rust/core/videos.rs index a53e845..559a11b 100644 --- a/src/rust/core/videos.rs +++ b/src/rust/core/videos.rs @@ -1,3 +1,8 @@ +use crate::model::Model; +use crate::model::Modeling; +use color_eyre::eyre::eyre; +use color_eyre::eyre::Result; +use std::mem::replace; use std::path::PathBuf; #[derive(Clone, Debug, Default, PartialEq)] @@ -9,9 +14,54 @@ pub struct Video { looping: bool, } -#[derive(Clone, Debug, Default, PartialEq)] -pub struct VideoModel { - videos: Vec