Remove TryInto<SongData> for Song

These pre-defined conversion traits are meant to be fast, and implementing this conversion that accesses the database goes against this design.
This commit is contained in:
Ethan Girouard 2024-07-28 22:37:17 -04:00
parent 74b34b1e54
commit d45f102be7
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146

View File

@ -28,43 +28,6 @@ pub struct SongData {
pub image_path: String, pub image_path: String,
} }
#[cfg(feature = "ssr")]
impl TryInto<SongData> for Song {
type Error = Box<dyn std::error::Error>;
/// Convert a Song object into a SongData object
///
/// This conversion is expensive, as it requires database queries to get the artist and album objects.
/// The SongData/Song conversions are also not truly reversible,
/// due to the way the image_path, album, and artist data is handled.
fn try_into(self) -> Result<SongData, Self::Error> {
use crate::database;
let mut db_con = database::get_db_conn();
let album = self.get_album(&mut db_con)?;
// Use the song's image path if it exists, otherwise use the album's image path, or fallback to the placeholder
let image_path = self.image_path.clone().unwrap_or_else(|| {
album
.as_ref()
.and_then(|album| album.image_path.clone())
.unwrap_or_else(|| "/assets/images/placeholder.jpg".to_string())
});
Ok(SongData {
id: self.id.ok_or("Song id must be present (Some) to convert to SongData")?,
title: self.title.clone(),
artists: self.get_artists(&mut db_con)?,
album: album,
track: self.track,
duration: self.duration,
release_date: self.release_date,
// TODO https://gitlab.mregirouard.com/libretunes/libretunes/-/issues/35
song_path: self.storage_path,
image_path: image_path,
})
}
}
impl TryInto<Song> for SongData { impl TryInto<Song> for SongData {
type Error = Box<dyn std::error::Error>; type Error = Box<dyn std::error::Error>;
@ -72,7 +35,7 @@ impl TryInto<Song> for SongData {
/// Convert a SongData object into a Song object /// Convert a SongData object into a Song object
/// ///
/// The SongData/Song conversions are also not truly reversible, /// The SongData/Song conversions are also not truly reversible,
/// due to the way the image_path, album, and and artist data is handled. /// due to the way the image_path data is handled.
fn try_into(self) -> Result<Song, Self::Error> { fn try_into(self) -> Result<Song, Self::Error> {
Ok(Song { Ok(Song {
id: Some(self.id), id: Some(self.id),