Fix feature issues

This commit is contained in:
Serial 2022-01-28 15:29:34 -05:00
parent 394f4b3c43
commit 3c397705c0
3 changed files with 27 additions and 13 deletions

View file

@ -32,7 +32,6 @@ pub enum ErrorKind {
FileEncoding(FileEncodingError),
// Picture related errors
#[cfg(feature = "id3v2")]
/// Provided an invalid picture
NotAPicture,
/// Attempted to write a picture that the format does not support
@ -62,21 +61,25 @@ pub enum ErrorKind {
Io(std::io::Error),
}
#[cfg(feature = "id3v2")]
#[derive(Debug, Clone)]
#[non_exhaustive]
/// The types of errors that can occur while interacting with ID3v2 tags
pub enum Id3v2ErrorKind {
#[cfg(feature = "id3v2")]
/// Arises when an invalid picture format is parsed. Only applicable to [`Id3v2Version::V2`](crate::id3::v2::Id3v2Version::V2)
BadPictureFormat(String),
/// Arises when an invalid ID3v2 version is found
BadId3v2Version(u8, u8),
#[cfg(feature = "id3v2")]
/// Arises when a frame ID contains invalid characters (must be within `'A'..'Z'` or `'0'..'9'`)
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 = "id3v2")]
/// Arises when attempting to write an invalid Frame (Bad `FrameID`/`FrameValue` pairing)
BadFrame(String, &'static str),
/// A catch-all for all remaining errors
@ -93,17 +96,22 @@ impl Display for Id3v2ErrorKind {
"Found an invalid version (v{}.{}), expected any major revision in: (2, 3, 4)",
major, minor
),
#[cfg(feature = "id3v2")]
Id3v2ErrorKind::BadFrameID => write!(f, "Failed to parse a frame ID"),
#[cfg(feature = "id3v2")]
Id3v2ErrorKind::BadFrameLength => write!(
f,
"Frame isn't long enough to extract the necessary information"
),
#[cfg(feature = "id3v2")]
Id3v2ErrorKind::BadSyncText => write!(f, "Encountered invalid data in SYLT frame"),
#[cfg(feature = "id3v2")]
Id3v2ErrorKind::BadFrame(ref frame_id, frame_value) => write!(
f,
"Attempted to write an invalid frame. ID: \"{}\", Value: \"{}\"",
frame_id, frame_value
),
#[cfg(feature = "id3v2")]
Id3v2ErrorKind::BadPictureFormat(format) => {
write!(f, "Picture: Found unexpected format \"{}\"", format)
},

View file

@ -3,8 +3,7 @@ pub(crate) mod text_utils;
pub(crate) mod upgrade;
#[cfg(feature = "id3v2")]
use crate::error::Result;
use crate::error::{Id3v2Error, Id3v2ErrorKind};
use crate::error::{Id3v2Error, Id3v2ErrorKind, Result};
#[cfg(feature = "id3v2")]
pub(in crate::id3::v2) fn unsynch_content(content: &[u8]) -> Result<Vec<u8>> {

View file

@ -44,15 +44,6 @@ pub struct TaggedFile {
pub(crate) tags: Vec<Tag>,
}
#[cfg(any(
feature = "id3v1",
feature = "riff_info_list",
feature = "aiff_text_chunks",
feature = "vorbis_comments",
feature = "id3v2",
feature = "mp4_ilst",
feature = "ape"
))]
impl TaggedFile {
/// Returns the primary tag
///
@ -107,6 +98,9 @@ impl TaggedFile {
/// Inserts a [`Tag`]
///
/// NOTE: This will do nothing if the [`FileType`] does not support
/// the [`TagType`]. See [`FileType::supports_tag_type`]
///
/// If a tag is replaced, it will be returned
pub fn insert_tag(&mut self, tag: Tag) -> Option<Tag> {
let tag_type = *tag.tag_type();
@ -131,6 +125,19 @@ impl TaggedFile {
.map(|pos| self.tags.remove(pos))
}
/// Changes the [`FileType`]
///
/// NOTES:
///
/// * This will remove any tag the format does not support. See [`FileType::supports_tag_type`]
/// * This will reset the [`FileProperties`]
pub fn change_file_type(&mut self, file_type: FileType) {
self.ty = file_type;
self.properties = FileProperties::default();
self.tags
.retain(|t| self.ty.supports_tag_type(t.tag_type()));
}
/// Removes all tags from the file
pub fn clear(&mut self) {
self.tags.clear()