From 8a09b90811180283c42f5d505dcebdca4dca72f7 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Tue, 25 May 2021 17:22:53 -0400 Subject: [PATCH] Change errors Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com> --- src/error.rs | 18 +++++++++--------- src/lib.rs | 2 +- src/tag.rs | 16 ++++++++-------- src/types/picture.rs | 11 ++++++----- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/error.rs b/src/error.rs index 44973769..4c96e007 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ /// Errors that could occur within Lofty. #[derive(thiserror::Error, Debug)] -pub enum Error { +pub enum LoftyError { /// Unknown file extension. #[error("Failed to guess the metadata format based on the file extension.")] UnknownFileExtension, @@ -38,9 +38,6 @@ pub enum Error { /// Any error from [`mp4ameta`] #[error(transparent)] Mp4Tag(#[from] mp4ameta::Error), - /// Any error from [`opus_headers`] - #[error(transparent)] - OpusTag(#[from] opus_headers::ParseError), /// Any error from [`lewton`] #[error(transparent)] Lewton(#[from] lewton::VorbisError), @@ -48,11 +45,14 @@ pub enum Error { #[error(transparent)] Ogg(#[from] ogg::OggReadError), /// Errors that arise while reading/writing to wav files - #[error("{0}")] - Wav(String), + #[error("Invalid Riff file: {0}")] + Riff(String), + /// Errors that arise while reading/writing to opus files + #[error("Invalid Opus file: {0}")] + Opus(String), - /// Failed to convert data to a picture - #[error("")] + /// Arises when provided an invalid picture + #[error("Picture contains invalid data")] NotAPicture, /// If a string isn't Utf8 @@ -68,4 +68,4 @@ pub enum Error { } /// Type for the result of tag operations. -pub type Result = std::result::Result; +pub type Result = std::result::Result; diff --git a/src/lib.rs b/src/lib.rs index bce59545..115f69ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,7 +126,7 @@ mod tag; pub use crate::tag::{Id3Format, Tag, TagType, VorbisFormat}; mod error; -pub use crate::error::{Error, Result}; +pub use crate::error::{LoftyError, Result}; mod components; pub use crate::components::tags::*; diff --git a/src/tag.rs b/src/tag.rs index 2d25cf79..9008fd7d 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -1,6 +1,6 @@ #[allow(clippy::wildcard_imports)] use crate::components::tags::*; -use crate::{AudioTag, Error, Result}; +use crate::{AudioTag, LoftyError, Result}; use std::io::Seek; use std::path::Path; @@ -61,8 +61,8 @@ impl Tag { let extension = path .as_ref() .extension() - .ok_or(Error::UnknownFileExtension)?; - let extension_str = extension.to_str().ok_or(Error::UnknownFileExtension)?; + .ok_or(LoftyError::UnknownFileExtension)?; + let extension_str = extension.to_str().ok_or(LoftyError::UnknownFileExtension)?; TagType::try_from_ext(extension_str)? }); @@ -187,12 +187,12 @@ impl TagType { "ogg" | "oga" => Ok(Self::Vorbis(VorbisFormat::Ogg)), #[cfg(feature = "format-mp4")] "m4a" | "m4b" | "m4p" | "m4v" | "isom" | "mp4" => Ok(Self::Mp4), - _ => Err(Error::UnsupportedFormat(ext.to_owned())), + _ => Err(LoftyError::UnsupportedFormat(ext.to_owned())), } } fn try_from_sig(data: &[u8]) -> Result { if data.is_empty() { - return Err(Error::EmptyFile); + return Err(LoftyError::EmptyFile); } match data[0] { @@ -232,7 +232,7 @@ impl TagType { } // TODO: support AIFF chunks? - Err(Error::UnknownFormat) + Err(LoftyError::UnknownFormat) }, #[cfg(feature = "format-flac")] 102 if data.starts_with(&FLAC) => Ok(Self::Vorbis(VorbisFormat::Flac)), @@ -246,7 +246,7 @@ impl TagType { return Ok(Self::Vorbis(VorbisFormat::Opus)); } - Err(Error::UnknownFormat) + Err(LoftyError::UnknownFormat) }, #[cfg(feature = "format-riff")] 82 if data.starts_with(&RIFF) => { @@ -280,7 +280,7 @@ impl TagType { }, #[cfg(feature = "format-mp4")] _ if data[4..8] == FTYP => Ok(Self::Mp4), - _ => Err(Error::UnknownFormat), + _ => Err(LoftyError::UnknownFormat), } } } diff --git a/src/types/picture.rs b/src/types/picture.rs index 1380b740..5fe940cc 100644 --- a/src/types/picture.rs +++ b/src/types/picture.rs @@ -1,4 +1,4 @@ -use crate::{Error, Result}; +use crate::{LoftyError, Result}; use byteorder::{BigEndian, ReadBytesExt}; use std::borrow::Cow; @@ -59,7 +59,8 @@ impl MimeType { } impl TryFrom<&str> for MimeType { - type Error = Error; + type Error = LoftyError; + fn try_from(inp: &str) -> Result { Ok(match inp { "image/jpeg" => MimeType::Jpeg, @@ -67,7 +68,7 @@ impl TryFrom<&str> for MimeType { "image/tiff" => MimeType::Tiff, "image/bmp" => MimeType::Bmp, "image/gif" => MimeType::Gif, - _ => return Err(Error::UnsupportedMimeType(inp.to_owned())), + _ => return Err(LoftyError::UnsupportedMimeType(inp.to_owned())), }) } } @@ -387,7 +388,7 @@ impl Picture { } } - Err(Error::InvalidData) + Err(LoftyError::NotAPicture) } /// Convert the [`Picture`] back to an APEv2 byte vec: /// @@ -480,6 +481,6 @@ impl Picture { } } - Err(Error::InvalidData) + Err(LoftyError::NotAPicture) } }