use crate::error::{TagEncodingError, TagParseError};
use crate::tag::{ItemKey, TagType};
use lofty_attr::LoftyError;
pub use super::frame::error::{FrameEncodingError, FrameParseError};
pub use super::frame::header::FrameIdParseError;
pub use super::items::sync_text::{BadSyncTextContentTypeError, BadTimestampFormatError};
#[derive(Debug)]
#[non_exhaustive]
pub enum Id3v2ErrorKind {
UnsupportedFrameId(ItemKey),
InvalidUnsynchronisation,
V2InvalidTextEncoding,
BadSyncText,
MissingUfidOwner,
BadTimestampFormat,
}
impl core::fmt::Display for Id3v2ErrorKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnsupportedFrameId(item_key) => {
write!(f, "Unsupported frame ID for item key {item_key:?}")
},
Self::InvalidUnsynchronisation => write!(f, "Encountered an invalid unsynchronisation"),
Self::V2InvalidTextEncoding => {
write!(f, "ID3v2.2 only supports Latin-1 and UTF-16 encodings")
},
Self::BadSyncText => write!(f, "Encountered invalid data in SYLT frame"),
Self::MissingUfidOwner => write!(f, "Missing owner in UFID frame"),
Self::BadTimestampFormat => write!(
f,
"Encountered an invalid timestamp format in a synchronized frame"
),
}
}
}
#[derive(Debug)]
pub enum Id3v2HeaderError {
BadId3v2Version(u8, u8),
V2Compression,
BadExtendedHeaderSize,
}
impl core::fmt::Display for Id3v2HeaderError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Id3v2HeaderError::BadId3v2Version(major, minor) => write!(
f,
"found an invalid version (v{major}.{minor}), expected any major revision in: (2, \
3, 4)"
),
Id3v2HeaderError::V2Compression => write!(f, "encountered a compressed ID3v2.2 tag"),
Id3v2HeaderError::BadExtendedHeaderSize => {
write!(f, "encountered an extended header with an invalid size")
},
}
}
}
impl core::error::Error for Id3v2HeaderError {}
#[derive(LoftyError)]
#[error(message = "failed to parse ID3v2 tag")]
pub(crate) struct Id3v2ParseError {
#[error(from(
std::io::Error,
crate::error::FakeTagError,
Id3v2HeaderError,
super::frame::error::FrameParseError,
crate::util::alloc::AllocationError,
crate::iff::error::ChunkParseError,
))]
source: Box<dyn core::error::Error + Send + Sync + 'static>,
}
impl From<Id3v2ParseError> for TagParseError {
fn from(input: Id3v2ParseError) -> Self {
TagParseError::new(TagType::Id3v2, input.source)
}
}
#[derive(LoftyError)]
#[error(message = "failed to write ID3v2 tag")]
pub(crate) struct Id3v2EncodingError {
#[error(from(
std::io::Error,
super::frame::error::FrameEncodingError,
crate::id3::v2::util::synchsafe::SynchOverflowError,
crate::util::alloc::AllocationError,
))]
source: Box<dyn core::error::Error + Send + Sync + 'static>,
}
impl From<Id3v2EncodingError> for TagEncodingError {
fn from(input: Id3v2EncodingError) -> Self {
TagEncodingError::new(TagType::Id3v2, input.source)
}
}