backgrounds use a better conversion for strings

This commit is contained in:
Chris Cochrun 2024-11-20 14:04:45 -06:00
parent 684dc9d37c
commit 0669faa34f

View file

@ -46,9 +46,7 @@ pub struct Background {
impl TryFrom<String> for Background { impl TryFrom<String> for Background {
type Error = ParseError; type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> { fn try_from(value: String) -> Result<Self, Self::Error> {
let value = value.trim_start_matches("file://"); Background::try_from(value.as_str())
let path = PathBuf::from(value);
Background::try_from(path)
} }
} }
@ -81,7 +79,8 @@ impl TryFrom<PathBuf> for Background {
impl TryFrom<&str> for Background { impl TryFrom<&str> for Background {
type Error = ParseError; type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> { fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.starts_with("~") { let value = value.trim_start_matches("file://");
if value.contains("~") {
if let Some(home) = dirs::home_dir() { if let Some(home) = dirs::home_dir() {
if let Some(home) = home.to_str() { if let Some(home) = home.to_str() {
let value = value.replace("~", home); let value = value.replace("~", home);
@ -190,7 +189,6 @@ impl Slide {
impl From<Value> for Slide { impl From<Value> for Slide {
fn from(value: Value) -> Self { fn from(value: Value) -> Self {
dbg!(&value);
match value { match value {
Value::List(list) => lisp_to_slide(list), Value::List(list) => lisp_to_slide(list),
_ => Slide::default(), _ => Slide::default(),
@ -212,9 +210,7 @@ fn lisp_to_slide(lisp: Vec<Value>) -> Slide {
DEFAULT_BACKGROUND_LOCATION DEFAULT_BACKGROUND_LOCATION
}; };
dbg!(&background_position);
if let Some(background) = lisp.get(background_position) { if let Some(background) = lisp.get(background_position) {
dbg!(&background);
slide = slide.background(lisp_to_background(background)); slide = slide.background(lisp_to_background(background));
} else { } else {
slide = slide.background(Background::default()); slide = slide.background(Background::default());
@ -254,12 +250,10 @@ fn lisp_to_slide(lisp: Vec<Value>) -> Slide {
.video_loop(false) .video_loop(false)
.video_start_time(0.0) .video_start_time(0.0)
.video_end_time(0.0); .video_end_time(0.0);
dbg!(&slide);
match slide.build() { match slide.build() {
Ok(slide) => slide, Ok(slide) => slide,
Err(e) => { Err(e) => {
dbg!(&e);
error!("Shoot! Slide didn't build: {e}"); error!("Shoot! Slide didn't build: {e}");
Slide::default() Slide::default()
} }
@ -303,16 +297,14 @@ fn lisp_to_background(lisp: &Value) -> Background {
v == &Value::Keyword(Keyword::from("source")) v == &Value::Keyword(Keyword::from("source"))
}) { }) {
let source = &list[source + 1]; let source = &list[source + 1];
dbg!(&source);
match source { match source {
Value::String(s) => { Value::String(s) => {
match Background::try_from(s.as_str()) { match Background::try_from(s.as_str()) {
Ok(background) => { Ok(background) => background,
dbg!(&background);
background
}
Err(e) => { Err(e) => {
dbg!("Couldn't load background: ", e); error!(
"Couldn't load background: {e}"
);
Background::default() Background::default()
} }
} }
@ -465,7 +457,6 @@ impl Image {
mod test { mod test {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use std::fs::read_to_string; use std::fs::read_to_string;
use tracing::debug;
use super::*; use super::*;
@ -496,7 +487,6 @@ mod test {
fn test_lisp_serialize() { fn test_lisp_serialize() {
let lisp = let lisp =
read_to_string("./test_presentation.lisp").expect("oops"); read_to_string("./test_presentation.lisp").expect("oops");
println!("{lisp}");
let lisp_value = crisp::reader::read(&lisp); let lisp_value = crisp::reader::read(&lisp);
match lisp_value { match lisp_value {
Value::List(value) => { Value::List(value) => {
@ -505,7 +495,6 @@ mod test {
assert_eq!(slide, test_slide); assert_eq!(slide, test_slide);
let second_slide = Slide::from(value[1].clone()); let second_slide = Slide::from(value[1].clone());
dbg!(&second_slide);
let second_test_slide = test_second_slide(); let second_test_slide = test_second_slide();
assert_eq!(second_slide, second_test_slide) assert_eq!(second_slide, second_test_slide)
} }
@ -519,11 +508,9 @@ mod test {
.expect("Problem getting file read"); .expect("Problem getting file read");
match ron::from_str::<Vec<Slide>>(&slide) { match ron::from_str::<Vec<Slide>>(&slide) {
Ok(s) => { Ok(s) => {
dbg!(s);
assert!(true) assert!(true)
} }
Err(e) => { Err(e) => {
dbg!(e);
assert!(false) assert!(false)
} }
} }