From 0d799f7ee341b0af25bafb7abd9a5c646ac9298b Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Thu, 12 Feb 2026 15:48:58 -0600 Subject: [PATCH] some work on the saving system --- src/core/file.rs | 434 +++++++++++++++++++++++------------------------ 1 file changed, 213 insertions(+), 221 deletions(-) diff --git a/src/core/file.rs b/src/core/file.rs index 4b93d14..4a8ee11 100644 --- a/src/core/file.rs +++ b/src/core/file.rs @@ -2,7 +2,7 @@ use crate::core::{ kinds::ServiceItemKind, service_items::ServiceItem, slide::Background, }; -use miette::{IntoDiagnostic, Result}; +use miette::{IntoDiagnostic, Result, miette}; use std::{ fs::{self, File}, io::Write, @@ -10,10 +10,10 @@ use std::{ path::{Path, PathBuf}, }; use tar::Builder; -use tracing::error; +use tracing::{debug, error}; use zstd::Encoder; -pub async fn save( +pub fn save( list: Vec, path: impl AsRef, ) -> Result<()> { @@ -31,6 +31,7 @@ pub async fn save( temp_dir.push(s); fs::create_dir_all(&temp_dir).into_diagnostic()?; let service_file = temp_dir.join("serviceitems.ron"); + dbg!(&service_file); fs::File::create(&service_file).into_diagnostic()?; match fs::File::options() .read(true) @@ -39,9 +40,18 @@ pub async fn save( { Ok(mut f) => { f.write(ron.as_bytes()).into_diagnostic()?; + match tar.append_file("serviceitems.ron", &mut f) { + Ok(_) => (), + Err(e) => { + error!(?e); + dbg!(&e); + return Err(miette!("PROBS: {e}")); + } + } } Err(e) => { error!("There were problems making a file i guess: {e}"); + return Err(miette!("There was a problem: {e}")); } } // let list list.iter_mut().map(|item| { @@ -90,11 +100,13 @@ pub async fn save( "Audio file couldn't be added to temp_dir", )); if let Ok(file) = file.strip_prefix("file://") { - fs::File::create(&audio_file).into_diagnostic()?; - fs::copy(file, audio_file).into_diagnostic()?; + let mut file = + fs::File::open(&audio_file).into_diagnostic()?; + tar.append_file("", &mut file).into_diagnostic()?; } else { - fs::File::create(&audio_file).into_diagnostic()?; - fs::copy(file, audio_file).into_diagnostic()?; + let mut file = + fs::File::open(&audio_file).into_diagnostic()?; + tar.append_file("", &mut file).into_diagnostic()?; } } if let Some(file) = background { @@ -103,23 +115,33 @@ pub async fn save( "Background file couldn't be added to temp_dir", )); if let Ok(file) = file.path.strip_prefix("file://") { - fs::File::create(&background_file) + let mut file = fs::File::open(&background_file) .into_diagnostic()?; - fs::copy(file, background_file).into_diagnostic()?; + tar.append_file("", &mut file).into_diagnostic()?; } else { - fs::File::create(&background_file) - .into_diagnostic()?; - fs::copy(file.path, background_file) + let mut file = fs::File::open(&background_file) .into_diagnostic()?; + tar.append_file("", &mut file).into_diagnostic()?; } } } - tar.append_dir_all(path, temp_dir).into_diagnostic()?; - tar.finish().into_diagnostic() -} - -async fn clear_temp_dir(_temp_dir: &Path) -> Result<()> { - todo!() + // match tar.append_dir_all(path, &temp_dir) { + // Ok(_) => (), + // Err(e) => { + // error!(?e); + // dbg!(&e); + // return Err(miette!("tar error: {e}")); + // } + // } + match tar.finish() { + Ok(_) => (), + Err(e) => { + error!(?e); + dbg!(&e); + return Err(miette!("tar error: {e}")); + } + } + fs::remove_dir_all(temp_dir).into_diagnostic() } fn process_service_items(items: &Vec) -> Result { @@ -190,217 +212,187 @@ fn process_service_items(items: &Vec) -> Result { // Ok(json) // } -// #[cfg(test)] -// mod test { -// use std::path::PathBuf; +#[cfg(test)] +mod test { + use std::path::PathBuf; -// use super::*; -// use fs::canonicalize; -// use pretty_assertions::assert_eq; -// use sqlx::Connection; -// use tracing::debug; + use crate::core::{slide::Slide, songs::Song}; -// async fn get_db() -> SqliteConnection { -// 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()); -// SqliteConnection::connect(&db_url).await.expect("problems") -// } + use super::*; + use pretty_assertions::assert_eq; + use sqlx::{Connection, SqliteConnection}; -// #[tokio::test(flavor = "current_thread")] -// async fn test_process_song() { -// let mut db = get_db().await; -// let result = process_song(7, &mut db).await; -// let json_song_file = PathBuf::from("./test/test_song.json"); -// if let Ok(path) = canonicalize(json_song_file) { -// debug!(file = ?&path); -// if let Ok(s) = fs::read_to_string(path) { -// debug!(s); -// match result { -// Ok(json) => assert_eq!(json.to_string(), s), -// Err(e) => panic!( -// "There was an error in processing the song: {e}" -// ), -// } -// } else { -// panic!("String wasn't read from file"); -// } -// } else { -// panic!("Cannot find absolute path to test_song.json"); -// } -// } + async fn get_db() -> SqliteConnection { + let db_url = String::from("sqlite://./test.db"); + SqliteConnection::connect(&db_url).await.expect("problems") + } -// #[tokio::test(flavor = "current_thread")] -// async fn test_process_image() { -// let mut db = get_db().await; -// let result = process_image(3, &mut db).await; -// let json_image_file = PathBuf::from("./test/test_image.json"); -// if let Ok(path) = canonicalize(json_image_file) { -// debug!(file = ?&path); -// if let Ok(s) = fs::read_to_string(path) { -// debug!(s); -// match result { -// Ok(json) => assert_eq!(json.to_string(), s), -// Err(e) => panic!( -// "There was an error in processing the image: {e}" -// ), -// } -// } else { -// panic!("String wasn't read from file"); -// } -// } else { -// panic!("Cannot find absolute path to test_image.json"); -// } -// } + // #[tokio::test(flavor = "current_thread")] + // async fn test_process_song() { + // let mut db = get_db().await; + // let result = process_song(7, &mut db).await; + // let json_song_file = PathBuf::from("./test/test_song.json"); + // if let Ok(path) = canonicalize(json_song_file) { + // debug!(file = ?&path); + // if let Ok(s) = fs::read_to_string(path) { + // debug!(s); + // match result { + // Ok(json) => assert_eq!(json.to_string(), s), + // Err(e) => panic!( + // "There was an error in processing the song: {e}" + // ), + // } + // } else { + // panic!("String wasn't read from file"); + // } + // } else { + // panic!("Cannot find absolute path to test_song.json"); + // } + // } -// #[tokio::test(flavor = "current_thread")] -// async fn test_process_video() { -// let mut db = get_db().await; -// let result = process_video(73, &mut db).await; -// let json_video_file = PathBuf::from("./test/test_video.json"); -// if let Ok(path) = canonicalize(json_video_file) { -// debug!(file = ?&path); -// if let Ok(s) = fs::read_to_string(path) { -// debug!(s); -// match result { -// Ok(json) => assert_eq!(json.to_string(), s), -// Err(e) => panic!( -// "There was an error in processing the video: {e}" -// ), -// } -// } else { -// panic!("String wasn't read from file"); -// } -// } else { -// panic!("Cannot find absolute path to test_video.json"); -// } -// } + // #[tokio::test(flavor = "current_thread")] + // async fn test_process_image() { + // let mut db = get_db().await; + // let result = process_image(3, &mut db).await; + // let json_image_file = PathBuf::from("./test/test_image.json"); + // if let Ok(path) = canonicalize(json_image_file) { + // debug!(file = ?&path); + // if let Ok(s) = fs::read_to_string(path) { + // debug!(s); + // match result { + // Ok(json) => assert_eq!(json.to_string(), s), + // Err(e) => panic!( + // "There was an error in processing the image: {e}" + // ), + // } + // } else { + // panic!("String wasn't read from file"); + // } + // } else { + // panic!("Cannot find absolute path to test_image.json"); + // } + // } -// #[tokio::test(flavor = "current_thread")] -// async fn test_process_presentation() { -// let mut db = get_db().await; -// let result = process_presentation(54, &mut db).await; -// let json_presentation_file = -// PathBuf::from("./test/test_presentation.json"); -// if let Ok(path) = canonicalize(json_presentation_file) { -// debug!(file = ?&path); -// if let Ok(s) = fs::read_to_string(path) { -// debug!(s); -// match result { -// Ok(json) => assert_eq!(json.to_string(), s), -// Err(e) => panic!( -// "There was an error in processing the presentation: {e}" -// ), -// } -// } else { -// panic!("String wasn't read from file"); -// } -// } else { -// panic!( -// "Cannot find absolute path to test_presentation.json" -// ); -// } -// } + // #[tokio::test(flavor = "current_thread")] + // async fn test_process_video() { + // let mut db = get_db().await; + // let result = process_video(73, &mut db).await; + // let json_video_file = PathBuf::from("./test/test_video.json"); + // if let Ok(path) = canonicalize(json_video_file) { + // debug!(file = ?&path); + // if let Ok(s) = fs::read_to_string(path) { + // debug!(s); + // match result { + // Ok(json) => assert_eq!(json.to_string(), s), + // Err(e) => panic!( + // "There was an error in processing the video: {e}" + // ), + // } + // } else { + // panic!("String wasn't read from file"); + // } + // } else { + // panic!("Cannot find absolute path to test_video.json"); + // } + // } -// fn get_items() -> Vec { -// let items = vec![ -// ServiceItem { -// database_id: 7, -// kind: ServiceItemKind::Song, -// id: 0, -// }, -// ServiceItem { -// database_id: 54, -// kind: ServiceItemKind::Presentation(PresKind::Html), -// id: 0, -// }, -// ServiceItem { -// database_id: 73, -// kind: ServiceItemKind::Video, -// id: 0, -// }, -// ]; -// items -// } + // #[tokio::test(flavor = "current_thread")] + // async fn test_process_presentation() { + // let mut db = get_db().await; + // let result = process_presentation(54, &mut db).await; + // let json_presentation_file = + // PathBuf::from("./test/test_presentation.json"); + // if let Ok(path) = canonicalize(json_presentation_file) { + // debug!(file = ?&path); + // if let Ok(s) = fs::read_to_string(path) { + // debug!(s); + // match result { + // Ok(json) => assert_eq!(json.to_string(), s), + // Err(e) => panic!( + // "There was an error in processing the presentation: {e}" + // ), + // } + // } else { + // panic!("String wasn't read from file"); + // } + // } else { + // panic!( + // "Cannot find absolute path to test_presentation.json" + // ); + // } + // } -// #[tokio::test] -// async fn test_service_items() { -// let mut db = get_db().await; -// let items = get_items(); -// let json_item_file = -// PathBuf::from("./test/test_service_items.json"); -// let result = process_service_items(&items, &mut db).await; -// if let Ok(path) = canonicalize(json_item_file) { -// if let Ok(s) = fs::read_to_string(path) { -// match result { -// Ok(strings) => assert_eq!(strings.to_string(), s), -// Err(e) => panic!("There was an error: {e}"), -// } -// } -// } -// } + fn get_items() -> Vec { + let items = vec![ServiceItem { + database_id: 7, + kind: ServiceItemKind::Song(Song::default()), + id: 0, + title: "Glory Glory".into(), + slides: vec![Slide::default()], + }]; + items + } -// // #[tokio::test] -// // async fn test_save() { -// // let path = PathBuf::from("~/dev/lumina/src/rust/core/test.pres"); -// // let list = get_items(); -// // match save(list, path).await { -// // Ok(_) => assert!(true), -// // Err(e) => panic!("There was an error: {e}"), -// // } -// // } + // #[tokio::test] + // async fn test_service_items() { + // let mut db = get_db().await; + // let items = get_items(); + // let json_item_file = + // PathBuf::from("./test/test_service_items.json"); + // let result = process_service_items(&items, &mut db).await; + // if let Ok(path) = canonicalize(json_item_file) { + // if let Ok(s) = fs::read_to_string(path) { + // match result { + // Ok(strings) => assert_eq!(strings.to_string(), s), + // Err(e) => panic!("There was an error: {e}"), + // } + // } + // } + // } -// #[tokio::test] -// async fn test_store() { -// let path = PathBuf::from( -// "/home/chris/dev/lumina/src/rust/core/test.pres", -// ); -// let save_file = match File::create(path) { -// Ok(f) => f, -// Err(e) => panic!("Couldn't create save_file: {e}"), -// }; -// let mut db = get_db().await; -// let list = get_items(); -// if let Ok(json) = process_service_items(&list, &mut db).await -// { -// println!("{:?}", json); -// match store_service_items( -// &list, &mut db, &save_file, &json, -// ) -// .await -// { -// Ok(_) => assert!(true), -// Err(e) => panic!("There was an error: {e}"), -// } -// } else { -// panic!("There was an error getting the json value"); -// } -// } + // #[tokio::test] + // async fn test_save() { + // let path = PathBuf::from("~/dev/lumina/src/rust/core/test.pres"); + // let list = get_items(); + // match save(list, path).await { + // Ok(_) => assert!(true), + // Err(e) => panic!("There was an error: {e}"), + // } + // } -// // #[tokio::test] -// // async fn test_things() { -// // let mut temp_dir = dirs::data_dir().unwrap(); -// // temp_dir.push("lumina"); -// // let mut s: String = -// // iter::repeat_with(fastrand::alphanumeric) -// // .take(5) -// // .collect(); -// // s.insert_str(0, "temp_"); -// // temp_dir.push(s); -// // let _ = fs::create_dir_all(&temp_dir); -// // let mut db = get_db().await; -// // let service_file = temp_dir.join("serviceitems.json"); -// // let list = get_items(); -// // if let Ok(json) = process_service_items(&list, &mut db).await { -// // let _ = fs::File::create(&service_file); -// // match fs::write(service_file, json.to_string()) { -// // Ok(_) => assert!(true), -// // Err(e) => panic!("There was an error: {e}"), -// // } -// // } else { -// // panic!("There was an error getting the json value"); -// // } -// // } -// } + #[test] + fn test_save() { + let path = + PathBuf::from("/home/chris/dev/lumina-iced/test.pres"); + let list = get_items(); + match save(list, path) { + Ok(_) => assert!(true), + Err(e) => assert!(false, "{e}"), + } + } + + // #[tokio::test] + // async fn test_things() { + // let mut temp_dir = dirs::data_dir().unwrap(); + // temp_dir.push("lumina"); + // let mut s: String = + // iter::repeat_with(fastrand::alphanumeric) + // .take(5) + // .collect(); + // s.insert_str(0, "temp_"); + // temp_dir.push(s); + // let _ = fs::create_dir_all(&temp_dir); + // let mut db = get_db().await; + // let service_file = temp_dir.join("serviceitems.json"); + // let list = get_items(); + // if let Ok(json) = process_service_items(&list, &mut db).await { + // let _ = fs::File::create(&service_file); + // match fs::write(service_file, json.to_string()) { + // Ok(_) => assert!(true), + // Err(e) => panic!("There was an error: {e}"), + // } + // } else { + // panic!("There was an error getting the json value"); + // } + // } +}