Remove thiserror

This commit is contained in:
Serial 2021-09-05 19:22:29 -04:00
parent daf24ed2cc
commit 6edee49207
4 changed files with 122 additions and 59 deletions

View file

@ -20,9 +20,6 @@ simdutf8 = { version = "0.1.3", optional = true }
# Quick string accessor methods for Tag
paste = { version = "1.0.5", optional = true }
# Errors
thiserror = "1.0.28"
base64 = "0.13.0"
byteorder = "1.4.3"
cfg-if = "1.0.0"

View file

@ -1,103 +1,169 @@
use ogg_pager::PageError;
use std::fmt::{Display, Formatter};
/// Result of tag operations.
pub type Result<T> = std::result::Result<T, LoftyError>;
/// Errors that could occur within Lofty.
#[derive(thiserror::Error, Debug)]
#[derive(Debug)]
pub enum LoftyError {
// File extension/format related errors
/// Unknown file extension.
#[error("Failed to guess the metadata format based on the file extension.")]
UnknownFileExtension,
/// Unsupported file extension
#[error("Unsupported format: {0}")]
UnsupportedFormat(String),
BadExtension(String),
/// Unable to guess the format
#[error("No format could be determined from the provided file.")]
UnknownFormat,
// File data related errors
/// Provided an empty file
#[error("File contains no data")]
EmptyFile,
/// Attempting to read/write an abnormally large amount of data
#[error("An abnormally large amount of data was provided, and an overflow occurred")]
TooMuchData,
// Picture related errors
#[cfg(feature = "id3v2")]
/// Arises when an invalid picture format is parsed. Only applicable to [`Id3v2Version::V2`](crate::logic::id3::v2::Id3v2Version)
#[error("Picture: Found unexpected format {0}")]
BadPictureFormat(String),
/// Provided an invalid picture
#[error("Picture: Encountered invalid data")]
NotAPicture,
// Tag related errors
/// Arises when writing a tag to a file type that doesn't support it
#[error("Attempted to write a tag to a format that does not support it")]
UnsupportedTag,
/// Errors that arise while parsing OGG pages
#[cfg(feature = "vorbis_comments")]
#[error(transparent)]
OggPage(#[from] ogg_pager::PageError),
/// Errors that arise while decoding ID3v2 text
#[error("Text decoding: {0}")]
TextDecode(&'static str),
/// Errors that arise while reading/writing ID3v2 tags
#[error("ID3v2: {0}")]
Id3v2(&'static str),
/// Arises when an invalid ID3v2 version is found
#[error(
"ID3v2: Found an invalid version (v{0}.{1}), expected any major revision in: (2, 3, 4)"
)]
BadId3v2Version(u8, u8),
/// Arises when [`std::str::from_utf8`] fails to parse a frame ID
#[error("ID3v2: ")]
BadFrameID,
/// Arises when a frame doesn't have enough data
#[error("ID3v2: Frame isn't long enough to extract the necessary information")]
BadFrameLength,
/// Arises when invalid data is encountered while reading an ID3v2 synchronized text frame
#[error("ID3v2: Encountered invalid data in SYLT frame")]
BadSyncText,
/// Arises when a tag is expected (Ex. found an "ID3 " chunk in a WAV file), but isn't found
#[error("Reading: Expected a tag, found invalid data")]
FakeTag,
#[cfg(feature = "id3v2")]
/// Errors that arise while decoding ID3v2 text
TextDecode(&'static str),
#[cfg(feature = "id3v2")]
/// Errors that arise while reading/writing ID3v2 tags
Id3v2(&'static str),
#[cfg(feature = "id3v2")]
/// Arises when an invalid ID3v2 version is found
BadId3v2Version(u8, u8),
#[cfg(feature = "id3v2")]
/// Arises when [`std::str::from_utf8`] fails to parse a frame ID
BadFrameID,
#[cfg(feature = "id3v2")]
/// Arises when a frame doesn't have enough data
BadFrameLength,
#[cfg(feature = "id3v2")]
/// Arises when invalid data is encountered while reading an ID3v2 synchronized text frame
BadSyncText,
#[cfg(feature = "mp4_atoms")]
/// Arises when an atom contains invalid data
#[error("MP4 Atom: {0}")]
BadAtom(&'static str),
// File specific errors
/// Errors that arise while reading/writing to WAV files
#[error("Riff: {0}")]
Wav(&'static str),
/// Errors that arise while reading/writing to AIFF files
#[error("Aiff: {0}")]
Aiff(&'static str),
/// Errors that arise while reading/writing to FLAC files
#[error("Flac: {0}")]
Flac(&'static str),
/// Errors that arise while reading/writing to OPUS files
#[error("Opus: {0}")]
Opus(&'static str),
/// Errors that arise while reading/writing to OGG Vorbis files
#[error("Vorbis: {0}")]
Vorbis(&'static str),
/// Errors that arise while reading/writing to OGG files
#[error("OGG: {0}")]
Ogg(&'static str),
/// Errors that arise while reading/writing to MP3 files
#[error("MP3: {0}")]
Mp3(&'static str),
/// Errors that arise while reading/writing to MP4 files
#[error("MP4: {0}")]
Mp4(&'static str),
/// Errors that arise while reading/writing to APE files
#[error("APE: {0}")]
Ape(&'static str),
// Conversions for std Errors
// Conversions for external errors
/// Errors that arise while parsing OGG pages
#[cfg(feature = "vorbis_comments")]
OggPage(ogg_pager::PageError),
/// Unable to convert bytes to a String
#[error(transparent)]
FromUtf8(#[from] std::string::FromUtf8Error),
FromUtf8(std::string::FromUtf8Error),
/// Represents all cases of `std::io::Error`.
#[error(transparent)]
Io(#[from] std::io::Error),
Io(std::io::Error),
}
/// Result of tag operations.
pub type Result<T> = std::result::Result<T, LoftyError>;
impl Display for LoftyError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
// Conversions
#[cfg(feature = "vorbis_comments")]
LoftyError::OggPage(ref err) => write!(f, "{}", err),
LoftyError::FromUtf8(ref err) => write!(f, "{}", err),
LoftyError::Io(ref err) => write!(f, "{}", err),
LoftyError::BadExtension(ext) => write!(f, "Found unknown file extension \"{}\"", ext),
LoftyError::UnknownFormat => {
write!(f, "No format could be determined from the provided file")
},
LoftyError::EmptyFile => write!(f, "File contains no data"),
LoftyError::TooMuchData => write!(
f,
"An abnormally large amount of data was provided, and an overflow occurred"
),
LoftyError::NotAPicture => write!(f, "Picture: Encountered invalid data"),
LoftyError::UnsupportedTag => write!(
f,
"Attempted to write a tag to a format that does not support it"
),
LoftyError::FakeTag => write!(f, "Reading: Expected a tag, found invalid data"),
#[cfg(feature = "id3v2")]
LoftyError::BadPictureFormat(format) => {
write!(f, "Picture: Found unexpected format \"{}\"", format)
},
#[cfg(feature = "id3v2")]
LoftyError::TextDecode(message) => write!(f, "Text decoding: {}", message),
#[cfg(feature = "id3v2")]
LoftyError::Id3v2(message) => write!(f, "ID3v2: {}", message),
#[cfg(feature = "id3v2")]
LoftyError::BadId3v2Version(major, minor) => write!(
f,
"ID3v2: Found an invalid version (v{}.{}), expected any major revision in: (2, 3, \
4)",
major, minor
),
#[cfg(feature = "id3v2")]
LoftyError::BadFrameID => write!(f, "ID3v2: Failed to parse a frame ID"),
#[cfg(feature = "id3v2")]
LoftyError::BadFrameLength => write!(
f,
"ID3v2: Frame isn't long enough to extract the necessary information"
),
#[cfg(feature = "id3v2")]
LoftyError::BadSyncText => write!(f, "ID3v2: Encountered invalid data in SYLT frame"),
#[cfg(feature = "mp4_atoms")]
LoftyError::BadAtom(message) => write!(f, "MP4 Atom: {}", message),
// Files
LoftyError::Wav(message) => write!(f, "WAV: {}", message),
LoftyError::Aiff(message) => write!(f, "AIFF: {}", message),
LoftyError::Flac(message) => write!(f, "FLAC: {}", message),
LoftyError::Opus(message) => write!(f, "Opus: {}", message),
LoftyError::Vorbis(message) => write!(f, "OGG Vorbis: {}", message),
LoftyError::Ogg(message) => write!(f, "OGG: {}", message),
LoftyError::Mp3(message) => write!(f, "MP3: {}", message),
LoftyError::Mp4(message) => write!(f, "MP4: {}", message),
LoftyError::Ape(message) => write!(f, "APE: {}", message),
}
}
}
impl std::error::Error for LoftyError {}
impl From<ogg_pager::PageError> for LoftyError {
fn from(input: PageError) -> Self {
LoftyError::OggPage(input)
}
}
impl From<std::io::Error> for LoftyError {
fn from(input: std::io::Error) -> Self {
LoftyError::Io(input)
}
}
impl From<std::string::FromUtf8Error> for LoftyError {
fn from(input: std::string::FromUtf8Error) -> Self {
LoftyError::FromUtf8(input)
}
}

View file

@ -1,6 +1,7 @@
use crate::logic::ape::ApeFile;
use crate::logic::iff::aiff::AiffFile;
use crate::logic::iff::wav::WavFile;
use crate::logic::mp4::Mp4File;
use crate::logic::mpeg::MpegFile;
use crate::logic::ogg::flac::FlacFile;
use crate::logic::ogg::opus::OpusFile;
@ -8,7 +9,6 @@ use crate::logic::ogg::vorbis::VorbisFile;
use crate::types::file::AudioFile;
use crate::{FileType, LoftyError, Result, TaggedFile};
use crate::logic::mp4::Mp4File;
use std::io::{Cursor, Read, Seek};
use std::path::Path;

View file

@ -312,7 +312,7 @@ impl FileType {
"oga" => Err(LoftyError::Ogg(
"Files with extension \"oga\" must have their type determined by content",
)),
_ => Err(LoftyError::UnsupportedFormat(ext.to_string())),
_ => Err(LoftyError::BadExtension(ext.to_string())),
}
}