Update error.rs

This commit is contained in:
Serial 2021-08-24 21:44:46 -04:00
parent ebc85cd207
commit 7fd326b327
2 changed files with 22 additions and 12 deletions

View file

@ -44,15 +44,26 @@ pub enum LoftyError {
/// 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 invalid data is encountered while reading an ID3v2 general encapsulated object frame
#[error("ID3v2: Encountered invalid data in GEOB frame")]
BadEncapsulatedObject,
/// Arists when [`std::str::from_utf8`] fails to parse a frame ID
#[error("ID3v2: Encountered an invalid frame ID")]
BadFrameID,
/// 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,
/// Errors that arise while reading/writing to WAV files
#[error("Riff: {0}")]
Wav(&'static str),

View file

@ -4,7 +4,7 @@ use crate::types::picture::TextEncoding;
use std::io::{Cursor, Read};
#[derive(PartialEq)]
#[derive(PartialEq, Clone)]
/// Information about a [`GeneralEncapsulatedObject`]
pub struct GEOBInformation {
/// The text encoding of `file_name` and `description`
@ -32,21 +32,20 @@ impl GeneralEncapsulatedObject {
///
/// # Errors
///
/// This function will return [`BadEncapsulatedObject`][LoftyError::BadEncapsulatedObject] if at any point it's unable to parse the data
/// This function will return an error if at any point it's unable to parse the data
pub fn parse(data: &[u8]) -> Result<Self> {
if data.len() < 4 {
return Err(LoftyError::BadEncapsulatedObject);
return Err(LoftyError::Id3v2("GEOB frame has invalid size (< 4)"));
}
let mut encoding =
TextEncoding::from_u8(data[0]).ok_or_else(|| LoftyError::BadEncapsulatedObject)?;
let encoding = TextEncoding::from_u8(data[0]).ok_or(LoftyError::TextDecode("Found invalid encoding"))?;
let mut cursor = Cursor::new(&data[1..]);
let mime_type = decode_text(&mut cursor, TextEncoding::Latin1, true)?;
let file_name = decode_text(&mut cursor, encoding, true)?;
let description =
decode_text(&mut cursor, encoding, true)?.unwrap_or_else(|| String::new());
decode_text(&mut cursor, encoding, true)?.unwrap_or_else(String::new);
let mut data = Vec::new();
cursor.read_to_end(&mut data)?;