diff --git a/database/src/error.rs b/database/src/error.rs index 5c6734d..a0b7a87 100644 --- a/database/src/error.rs +++ b/database/src/error.rs @@ -10,6 +10,9 @@ pub enum Error { #[error(transparent)] QueryError(#[from] diesel::result::Error), + #[error("Missing item dependency ({0} {1})")] + MissingItem(&'static str, String), + #[error(transparent)] SendError(#[from] std::sync::mpsc::SendError), diff --git a/database/src/medium.rs b/database/src/medium.rs index 534f6b7..bcf1ac1 100644 --- a/database/src/medium.rs +++ b/database/src/medium.rs @@ -256,22 +256,18 @@ impl Database { let recording = self .get_recording(&recording_id)? - .ok_or(Error::Other(format!( - "Failed to get recording ({}) for track ({}).", - recording_id, - row.id, - )))?; + .ok_or(Error::MissingItem("recording", recording_id))?; let mut part_indices = Vec::new(); - let work_parts = row - .work_parts - .split(','); + let work_parts = row.work_parts.split(','); for part_index in work_parts { if !part_index.is_empty() { - let index = str::parse(part_index) - .or(Err(Error::Other(format!("Failed to parse part index from '{}'.", row.work_parts))))?; + let index = str::parse(part_index).or(Err(Error::Other(format!( + "Failed to parse part index from '{}'.", + row.work_parts + ))))?; part_indices.push(index); } diff --git a/database/src/recordings.rs b/database/src/recordings.rs index 0fefbe6..ec1fdc1 100644 --- a/database/src/recordings.rs +++ b/database/src/recordings.rs @@ -218,47 +218,31 @@ impl Database { person: match row.person { Some(id) => Some( self.get_person(&id)? - .ok_or(Error::Other(format!( - "Failed to get person ({}) for recording ({}).", - id, - row.id, - )))? + .ok_or(Error::MissingItem("person", id))?, ), None => None, }, ensemble: match row.ensemble { Some(id) => Some( self.get_ensemble(&id)? - .ok_or(Error::Other(format!( - "Failed to get ensemble ({}) for recording ({}).", - id, - row.id, - )))? + .ok_or(Error::MissingItem("ensemble", id))?, ), None => None, }, role: match row.role { Some(id) => Some( self.get_instrument(&id)? - .ok_or(Error::Other(format!( - "Failed to get instrument ({}) for recording ({}).", - id, - row.id, - )))? + .ok_or(Error::MissingItem("instrument", id))?, ), None => None, }, }); } - let work_id = &row.work; + let work_id = row.work; let work = self - .get_work(work_id)? - .ok_or(Error::Other(format!( - "Failed to get work ({}) for recording ({}).", - work_id, - row.id, - )))?; + .get_work(&work_id)? + .ok_or(Error::MissingItem("work", work_id))?; let recording_description = Recording { id: row.id, diff --git a/database/src/works.rs b/database/src/works.rs index e6b3e30..f914b0d 100644 --- a/database/src/works.rs +++ b/database/src/works.rs @@ -206,14 +206,10 @@ impl Database { .load::(&self.connection)?; for instrumentation in instrumentations { - let id = &instrumentation.instrument; + let id = instrumentation.instrument; instruments.push( - self.get_instrument(id)? - .ok_or(Error::Other(format!( - "Failed to get instrument ({}) for work ({}).", - id, - row.id, - )))? + self.get_instrument(&id)? + .ok_or(Error::MissingItem("instrument", id))?, ); } @@ -242,14 +238,10 @@ impl Database { }); } - let person_id = &row.composer; + let person_id = row.composer; let person = self - .get_person(person_id)? - .ok_or(Error::Other(format!( - "Failed to get person ({}) for work ({}).", - person_id, - row.id, - )))?; + .get_person(&person_id)? + .ok_or(Error::MissingItem("person", person_id))?; Ok(Work { id: row.id,