diff --git a/src/song.rs b/src/song.rs index 7e56349..bbe9f06 100644 --- a/src/song.rs +++ b/src/song.rs @@ -153,6 +153,7 @@ impl Song { filename } + /// Saves the song to the filesystem using the song's data pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> { match self.song_path() { Ok(song_path) => match std::fs::File::create(&song_path) { @@ -169,4 +170,29 @@ impl Song { // TODO: Add function to remove file from the filesystem } -// TODO: Add function to copy song +/// Copies a song using the source song's data +pub fn copy_song(song_source: &Song, song_target: &mut Song) -> Result<(), std::io::Error> { + match song_target.song_path() { + Ok(songpath) => { + let p = std::path::Path::new(&songpath); + if p.exists() { + Err(std::io::Error::other( + "Cannot copy song over to one that already exists", + )) + } else { + if song_target.data.is_empty() { + 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() { + Ok(_) => Ok(()), + Err(err) => Err(err), + } + } + } + Err(err) => Err(err), + } +}