Compare commits

..

2 Commits

Author SHA1 Message Date
a867f65a26 Modified test 2025-10-11 14:41:05 -04:00
43b9c512a1 Added code to copy song 2025-10-11 14:40:46 -04:00
4 changed files with 70 additions and 44 deletions

2
Cargo.lock generated
View File

@@ -142,7 +142,7 @@ checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
[[package]] [[package]]
name = "icarus_models" name = "icarus_models"
version = "0.6.0" version = "0.5.6"
dependencies = [ dependencies = [
"josekit", "josekit",
"rand", "rand",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "icarus_models" name = "icarus_models"
version = "0.6.0" version = "0.5.6"
edition = "2024" edition = "2024"
rust-version = "1.88" rust-version = "1.88"
description = "models used for the icarus project" description = "models used for the icarus project"

View File

@@ -1,12 +1,12 @@
use std::io::{Read, Write}; use std::io::{Read, Write};
use rand::Rng;
use serde::{Deserialize, Serialize};
use crate::constants; use crate::constants;
use crate::init; use crate::init;
use crate::types; use crate::types;
use rand::Rng;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)] #[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
pub struct Song { pub struct Song {
#[serde(skip_serializing_if = "init::is_uuid_nil")] #[serde(skip_serializing_if = "init::is_uuid_nil")]
@@ -170,35 +170,29 @@ impl Song {
// TODO: Add function to remove file from the filesystem // TODO: Add function to remove file from the filesystem
} }
/// I/O operations for songs /// Copies a song using the source song's data
pub mod io { pub fn copy_song(song_source: &Song, song_target: &mut Song) -> Result<(), std::io::Error> {
/// Copies a song using the source song's data match song_target.song_path() {
pub fn copy_song( Ok(songpath) => {
song_source: &super::Song, let p = std::path::Path::new(&songpath);
song_target: &mut super::Song, if p.exists() {
) -> Result<(), std::io::Error> { Err(std::io::Error::other(
match song_target.song_path() { "Cannot copy song over to one that already exists",
Ok(songpath) => { ))
let p = std::path::Path::new(&songpath); } else {
if p.exists() { if song_target.data.is_empty() {
Err(std::io::Error::other( song_target.data = song_source.data.clone();
"Cannot copy song over to one that already exists",
))
} else { } else {
if song_target.data.is_empty() { song_target.data.clear();
song_target.data = song_source.data.clone(); song_target.data = song_source.data.clone();
} else { }
song_target.data.clear();
song_target.data = song_source.data.clone();
}
match song_target.save_to_filesystem() { match song_target.save_to_filesystem() {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(err) => Err(err), Err(err) => Err(err),
}
} }
} }
Err(err) => Err(err),
} }
Err(err) => Err(err),
} }
} }

View File

@@ -30,12 +30,16 @@ mod utils {
#[cfg(test)] #[cfg(test)]
mod song_tests { mod song_tests {
use std::fs::File;
use std::io::Write;
use tempfile::tempdir; use tempfile::tempdir;
use crate::utils;
use icarus_models::song; use icarus_models::song;
use icarus_models::types; use icarus_models::types;
use crate::utils;
#[test] #[test]
fn test_song_to_data() { fn test_song_to_data() {
println!("Test"); println!("Test");
@@ -99,22 +103,50 @@ mod song_tests {
song.directory = utils::get_tests_directory(); song.directory = utils::get_tests_directory();
song.filename = String::from("track01.flac"); song.filename = String::from("track01.flac");
let mut song_cpy = song.clone(); match song.song_path() {
let temp_dir = tempdir().expect("Failed to create temp dir"); Ok(songpath) => match utils::extract_data_from_file(&songpath) {
song_cpy.directory = match temp_dir.path().to_str() { Ok(buffer) => {
Some(s) => String::from(s), let mut song_cpy = song.clone();
None => String::new(), let temp_dir = tempdir().expect("Failed to create temp dir");
}; song_cpy.directory = match temp_dir.path().to_str() {
Some(s) => String::from(s),
None => String::new(),
};
assert_eq!(song.directory.is_empty(), false); assert_eq!(song.directory.is_empty(), false);
song_cpy.filename = song.generate_filename(types::MusicTypes::FlacExtension, true); song_cpy.filename =
println!("Directory: {:?}", song_cpy.directory); song.generate_filename(types::MusicTypes::FlacExtension, true);
println!("File to be created: {:?}", song_cpy.filename); println!("Directory: {:?}", song_cpy.directory);
println!("File to be created: {:?}", song_cpy.filename);
match song::copy_song(&song, &mut song_cpy) { let path = match song_cpy.song_path() {
Ok(_) => {} Ok(s_path) => s_path,
Err(err) => {
assert!(false, "Error: {:?}", err);
String::new()
}
};
match File::create(path) {
Ok(mut file_cpy) => match file_cpy.write_all(&buffer) {
Ok(success) => {
println!("Success: {:?}", success);
}
Err(err) => {
assert!(false, "Error saving file: {:?}", err);
}
},
Err(err) => {
assert!(false, "Error: {:?}", err);
}
};
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
},
Err(err) => { Err(err) => {
assert!(false, "Error copying song: Error: {err:?}") assert!(false, "Error extracting song data: {:?}", err);
} }
} }
} }