From cbf2a2fd966eb887aeda229c19f383dfc761cc89 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Thu, 28 Nov 2024 06:49:20 -0600 Subject: [PATCH] starting to add lisp_to_song function --- src/core/slide.rs | 2 +- src/core/songs.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) diff --git a/src/core/slide.rs b/src/core/slide.rs index 6ebfa34..10f3cb7 100644 --- a/src/core/slide.rs +++ b/src/core/slide.rs @@ -290,7 +290,7 @@ fn lisp_to_text(lisp: &Value) -> impl Into { } } -fn lisp_to_background(lisp: &Value) -> Background { +pub fn lisp_to_background(lisp: &Value) -> Background { match lisp { Value::List(list) => { if let Some(source) = list.iter().position(|v| { diff --git a/src/core/songs.rs b/src/core/songs.rs index 9228645..64a8d9e 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -1,6 +1,7 @@ use std::{collections::HashMap, path::PathBuf}; use cosmic::{executor, iced::Executor}; +use crisp::types::{Keyword, Value}; use miette::{miette, IntoDiagnostic, Result}; use serde::{Deserialize, Serialize}; use sqlx::{ @@ -9,6 +10,8 @@ use sqlx::{ }; use tracing::{debug, error}; +use crate::core::slide; + use super::{ model::Model, slide::{Background, TextAlignment}, @@ -91,6 +94,142 @@ impl FromRow<'_, SqliteRow> for Song { } } +impl From for Song { + fn from(value: Value) -> Self { + match value { + Value::List(list) => lisp_to_song(list), + _ => Self::default(), + } + } +} + +fn lisp_to_song(list: Vec) -> Song { + const DEFAULT_SONG_ID: i32 = 0; + const DEFAULT_SONG_LOCATION: usize = 0; + + // I probably shouldn't rely upon default locations for these + // It's very unlikely that users of this method of song creation + // would use them in this order, so instead, since these items are all + // options, let's just treat them as if they don't exist if there isn't + // a keyword found in the list. + const DEFAULT_BACKGROUND_LOCATION: usize = 1; + let background_position = if let Some(background) = + list.iter().position(|v| { + v == &Value::Keyword(Keyword::from("background")) + }) { + background + 1 + } else { + DEFAULT_BACKGROUND_LOCATION + }; + + let background = + if let Some(background) = list.get(background_position) { + Some(slide::lisp_to_background(background)) + } else { + None + }; + + const DEFAULT_AUTHOR_LOCATION: usize = 1; + let author_position = if let Some(author) = list + .iter() + .position(|v| v == &Value::Keyword(Keyword::from("author"))) + { + author + 1 + } else { + DEFAULT_AUTHOR_LOCATION + }; + + let author = if let Some(author) = list.get(author_position) { + Some(String::from(author)) + } else { + None + }; + + const DEFAULT_CCLI_LOCATION: usize = 1; + let ccli_position = if let Some(ccli) = list + .iter() + .position(|v| v == &Value::Keyword(Keyword::from("ccli"))) + { + ccli + 1 + } else { + DEFAULT_CCLI_LOCATION + }; + + let ccli = if let Some(ccli) = list.get(ccli_position) { + Some(i32::from(ccli)) + } else { + None + }; + + const DEFAULT_FONT_LOCATION: usize = 1; + let font_position = if let Some(font) = list + .iter() + .position(|v| v == &Value::Keyword(Keyword::from("font"))) + { + font + 1 + } else { + DEFAULT_FONT_LOCATION + }; + + let font = if let Some(font) = list.get(font_position) { + Some(String::from(font)) + } else { + None + }; + + const DEFAULT_FONT_SIZE_LOCATION: usize = 1; + let font_size_position = if let Some(font_size) = + list.iter().position(|v| { + v == &Value::Keyword(Keyword::from("font-size")) + }) { + font_size + 1 + } else { + DEFAULT_FONT_SIZE_LOCATION + }; + + let font_size = + if let Some(font_size) = list.get(font_size_position) { + Some(i32::from(font_size)) + } else { + None + }; + + const DEFAULT_TITLE_LOCATION: usize = 1; + let title_position = if let Some(title) = list + .iter() + .position(|v| v == &Value::Keyword(Keyword::from("title"))) + { + title + 1 + } else { + DEFAULT_TITLE_LOCATION + }; + + let title = if let Some(title) = list.get(title_position) { + String::from(title) + } else { + String::from("song") + }; + + const DEFAULT_VERSE_ORDER_LOCATION: usize = 1; + let verse_order_position = if let Some(verse_order) = + list.iter().position(|v| { + v == &Value::Keyword(Keyword::from("verse-order")) + }) { + verse_order + 1 + } else { + DEFAULT_VERSE_ORDER_LOCATION + }; + + let verse_order = + if let Some(verse_order) = list.get(verse_order_position) { + Some(String::from(verse_order)) + } else { + None + }; + + todo!() +} + pub async fn get_song_from_db( index: i32, db: &mut SqliteConnection,