|
4 | 4 | //! which can be extended at any time. |
5 | 5 |
|
6 | 6 | use crate::file::FileType; |
7 | | -use crate::id3::v2::FrameId; |
8 | | -use crate::tag::ItemKey; |
| 7 | +use crate::id3::v2::error::Id3v2Error; |
9 | 8 | pub use crate::util::text::TextEncodingError; |
10 | 9 |
|
11 | 10 | use std::collections::TryReserveError; |
@@ -80,167 +79,6 @@ pub enum ErrorKind { |
80 | 79 | Infallible(std::convert::Infallible), |
81 | 80 | } |
82 | 81 |
|
83 | | -/// The types of errors that can occur while interacting with ID3v2 tags |
84 | | -#[derive(Debug)] |
85 | | -#[non_exhaustive] |
86 | | -pub enum Id3v2ErrorKind { |
87 | | - // Header |
88 | | - /// Arises when an invalid ID3v2 version is found |
89 | | - BadId3v2Version(u8, u8), |
90 | | - /// Arises when a compressed ID3v2.2 tag is encountered |
91 | | - /// |
92 | | - /// At the time the ID3v2.2 specification was written, a compression scheme wasn't decided. |
93 | | - /// As such, it is recommended to ignore the tag entirely. |
94 | | - V2Compression, |
95 | | - /// Arises when an extended header has an invalid size (must be >= 6 bytes and less than the total tag size) |
96 | | - BadExtendedHeaderSize, |
97 | | - |
98 | | - // Frame |
99 | | - /// Arises when a frame ID contains invalid characters (must be within `'A'..'Z'` or `'0'..'9'`) |
100 | | - /// or if the ID is too short/long. |
101 | | - BadFrameId(Vec<u8>), |
102 | | - /// Arises when no frame ID is available in the ID3v2 specification for an item key |
103 | | - /// and the associated value type. |
104 | | - UnsupportedFrameId(ItemKey), |
105 | | - /// Arises when a frame doesn't have enough data |
106 | | - BadFrameLength, |
107 | | - /// Arises when a frame with no content is parsed with [ParsingMode::Strict](crate::config::ParsingMode::Strict) |
108 | | - EmptyFrame(FrameId<'static>), |
109 | | - /// Arises when reading/writing a compressed or encrypted frame with no data length indicator |
110 | | - MissingDataLengthIndicator, |
111 | | - /// Arises when a frame or tag has its unsynchronisation flag set, but the content is not actually synchsafe |
112 | | - /// |
113 | | - /// See [`FrameFlags::unsynchronisation`](crate::id3::v2::FrameFlags::unsynchronisation) for an explanation. |
114 | | - InvalidUnsynchronisation, |
115 | | - /// Arises when a text encoding other than Latin-1 or UTF-16 appear in an ID3v2.2 tag |
116 | | - V2InvalidTextEncoding, |
117 | | - /// Arises when an invalid picture format is parsed. Only applicable to [`ID3v2Version::V2`](crate::id3::v2::Id3v2Version::V2) |
118 | | - BadPictureFormat(String), |
119 | | - /// Arises when invalid data is encountered while reading an ID3v2 synchronized text frame |
120 | | - BadSyncText, |
121 | | - /// Arises when decoding a [`UniqueFileIdentifierFrame`](crate::id3::v2::UniqueFileIdentifierFrame) with no owner |
122 | | - MissingUfidOwner, |
123 | | - /// Arises when decoding a [`RelativeVolumeAdjustmentFrame`](crate::id3::v2::RelativeVolumeAdjustmentFrame) with an invalid channel type |
124 | | - BadRva2ChannelType, |
125 | | - /// Arises when decoding a [`TimestampFormat`](crate::id3::v2::TimestampFormat) with an invalid type |
126 | | - BadTimestampFormat, |
127 | | - |
128 | | - // Compression |
129 | | - #[cfg(feature = "id3v2_compression_support")] |
130 | | - /// Arises when a compressed frame is unable to be decompressed |
131 | | - Decompression(flate2::DecompressError), |
132 | | - #[cfg(not(feature = "id3v2_compression_support"))] |
133 | | - /// Arises when a compressed frame is encountered, but support is disabled |
134 | | - CompressedFrameEncountered, |
135 | | - |
136 | | - // Writing |
137 | | - /// Arises when attempting to write an encrypted frame with an invalid encryption method symbol (must be <= 0x80) |
138 | | - InvalidEncryptionMethodSymbol(u8), |
139 | | - /// Arises when attempting to write an invalid Frame (Bad `FrameId`/`FrameValue` pairing) |
140 | | - BadFrame(String, &'static str), |
141 | | - /// Arises when attempting to write a [`CommentFrame`](crate::id3::v2::CommentFrame) or [`UnsynchronizedTextFrame`](crate::id3::v2::UnsynchronizedTextFrame) with an invalid language |
142 | | - InvalidLanguage([u8; 3]), |
143 | | -} |
144 | | - |
145 | | -impl Display for Id3v2ErrorKind { |
146 | | - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
147 | | - match self { |
148 | | - // Header |
149 | | - Self::BadId3v2Version(major, minor) => write!( |
150 | | - f, |
151 | | - "Found an invalid version (v{major}.{minor}), expected any major revision in: (2, \ |
152 | | - 3, 4)" |
153 | | - ), |
154 | | - Self::V2Compression => write!(f, "Encountered a compressed ID3v2.2 tag"), |
155 | | - Self::BadExtendedHeaderSize => { |
156 | | - write!(f, "Found an extended header with an invalid size") |
157 | | - }, |
158 | | - |
159 | | - // Frame |
160 | | - Self::BadFrameId(frame_id) => write!(f, "Failed to parse a frame ID: 0x{frame_id:x?}"), |
161 | | - Self::UnsupportedFrameId(item_key) => { |
162 | | - write!(f, "Unsupported frame ID for item key {item_key:?}") |
163 | | - }, |
164 | | - Self::BadFrameLength => write!( |
165 | | - f, |
166 | | - "Frame isn't long enough to extract the necessary information" |
167 | | - ), |
168 | | - Self::EmptyFrame(id) => write!(f, "Frame `{id}` is empty"), |
169 | | - Self::MissingDataLengthIndicator => write!( |
170 | | - f, |
171 | | - "Encountered an encrypted frame without a data length indicator" |
172 | | - ), |
173 | | - Self::InvalidUnsynchronisation => write!(f, "Encountered an invalid unsynchronisation"), |
174 | | - Self::V2InvalidTextEncoding => { |
175 | | - write!(f, "ID3v2.2 only supports Latin-1 and UTF-16 encodings") |
176 | | - }, |
177 | | - Self::BadPictureFormat(format) => { |
178 | | - write!(f, "Picture: Found unexpected format \"{format}\"") |
179 | | - }, |
180 | | - Self::BadSyncText => write!(f, "Encountered invalid data in SYLT frame"), |
181 | | - Self::MissingUfidOwner => write!(f, "Missing owner in UFID frame"), |
182 | | - Self::BadRva2ChannelType => write!(f, "Encountered invalid channel type in RVA2 frame"), |
183 | | - Self::BadTimestampFormat => write!( |
184 | | - f, |
185 | | - "Encountered an invalid timestamp format in a synchronized frame" |
186 | | - ), |
187 | | - |
188 | | - // Compression |
189 | | - #[cfg(feature = "id3v2_compression_support")] |
190 | | - Self::Decompression(err) => write!(f, "Failed to decompress frame: {err}"), |
191 | | - #[cfg(not(feature = "id3v2_compression_support"))] |
192 | | - Self::CompressedFrameEncountered => write!( |
193 | | - f, |
194 | | - "Encountered a compressed ID3v2 frame, support is disabled" |
195 | | - ), |
196 | | - |
197 | | - // Writing |
198 | | - Self::InvalidEncryptionMethodSymbol(symbol) => write!( |
199 | | - f, |
200 | | - "Attempted to write an encrypted frame with an invalid method symbol ({symbol})" |
201 | | - ), |
202 | | - Self::BadFrame(frame_id, frame_value) => write!( |
203 | | - f, |
204 | | - "Attempted to write an invalid frame. ID: \"{frame_id}\", Value: \"{frame_value}\"", |
205 | | - ), |
206 | | - Self::InvalidLanguage(lang) => write!( |
207 | | - f, |
208 | | - "Invalid frame language found: {lang:?} (expected 3 ascii characters)" |
209 | | - ), |
210 | | - } |
211 | | - } |
212 | | -} |
213 | | - |
214 | | -/// An error that arises while interacting with an ID3v2 tag |
215 | | -pub struct Id3v2Error { |
216 | | - kind: Id3v2ErrorKind, |
217 | | -} |
218 | | - |
219 | | -impl Id3v2Error { |
220 | | - /// Create a new `ID3v2Error` from an [`Id3v2ErrorKind`] |
221 | | - #[must_use] |
222 | | - pub const fn new(kind: Id3v2ErrorKind) -> Self { |
223 | | - Self { kind } |
224 | | - } |
225 | | - |
226 | | - /// Returns the [`Id3v2ErrorKind`] |
227 | | - pub fn kind(&self) -> &Id3v2ErrorKind { |
228 | | - &self.kind |
229 | | - } |
230 | | -} |
231 | | - |
232 | | -impl Debug for Id3v2Error { |
233 | | - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
234 | | - write!(f, "ID3v2: {:?}", self.kind) |
235 | | - } |
236 | | -} |
237 | | - |
238 | | -impl Display for Id3v2Error { |
239 | | - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
240 | | - write!(f, "ID3v2: {}", self.kind) |
241 | | - } |
242 | | -} |
243 | | - |
244 | 82 | /// An error that arises while decoding a file |
245 | 83 | pub struct FileDecodingError { |
246 | 84 | format: Option<FileType>, |
|
0 commit comments