TagExt: Add TagExt::len

This commit is contained in:
Serial 2023-01-11 21:07:52 -05:00 committed by Alex
parent 90a7683322
commit af5154b7fb
10 changed files with 61 additions and 0 deletions

View file

@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **OGG**: `OggPictureStorage`
- This was added to cover the overlap in functionality between `VorbisComments` and `FlacFile` in that they both
store `(Picture, PictureInformation)`.
- **TagExt**: `TagExt::len`
### Changed
- **MP4**: `AtomIdent` stores freeform identifiers as `Cow<str>` opposed to `String` ([PR](https://github.com/Serial-ATA/lofty-rs/pull/95))

View file

@ -214,6 +214,10 @@ impl TagExt for ApeTag {
type Err = LoftyError;
type RefKey<'a> = &'a str;
fn len(&self) -> usize {
self.items.len()
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
self.items.iter().any(|i| i.key().eq_ignore_ascii_case(key))
}

View file

@ -175,6 +175,16 @@ impl TagExt for ID3v1Tag {
type Err = LoftyError;
type RefKey<'a> = &'a ItemKey;
fn len(&self) -> usize {
usize::from(self.title.is_some())
+ usize::from(self.artist.is_some())
+ usize::from(self.album.is_some())
+ usize::from(self.year.is_some())
+ usize::from(self.comment.is_some())
+ usize::from(self.track_number.is_some())
+ usize::from(self.genre.is_some())
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
match key {
ItemKey::TrackTitle => self.title.is_some(),

View file

@ -440,6 +440,10 @@ impl TagExt for ID3v2Tag {
type Err = LoftyError;
type RefKey<'a> = &'a FrameID<'a>;
fn len(&self) -> usize {
self.frames.len()
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
self.frames.iter().any(|frame| &frame.id == key)
}

View file

@ -140,6 +140,14 @@ impl TagExt for AIFFTextChunks {
type Err = LoftyError;
type RefKey<'a> = &'a ItemKey;
fn len(&self) -> usize {
usize::from(self.name.is_some())
+ usize::from(self.author.is_some())
+ usize::from(self.copyright.is_some())
+ self.annotations.as_ref().map_or(0, Vec::len)
+ self.comments.as_ref().map_or(0, Vec::len)
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
match key {
ItemKey::TrackTitle => self.name.is_some(),

View file

@ -134,6 +134,10 @@ impl TagExt for RIFFInfoList {
type Err = LoftyError;
type RefKey<'a> = &'a str;
fn len(&self) -> usize {
self.items.len()
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
self.items
.iter()

View file

@ -341,6 +341,10 @@ impl TagExt for Ilst {
type Err = LoftyError;
type RefKey<'a> = &'a AtomIdent<'a>;
fn len(&self) -> usize {
self.atoms.len()
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
self.atoms.iter().any(|atom| &atom.ident == key)
}

View file

@ -245,6 +245,10 @@ impl TagExt for VorbisComments {
type Err = LoftyError;
type RefKey<'a> = &'a str;
fn len(&self) -> usize {
self.items.len() + self.pictures.len()
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
self.items
.iter()

View file

@ -520,6 +520,10 @@ impl TagExt for Tag {
type Err = LoftyError;
type RefKey<'a> = &'a ItemKey;
fn len(&self) -> usize {
self.items.len() + self.pictures.len()
}
fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool {
self.items.iter().any(|item| item.key() == key)
}

View file

@ -138,6 +138,24 @@ pub trait TagExt: Accessor + Into<Tag> + Sized {
where
Self: 'a;
/// Returns the number of items in the tag
///
/// This will also include any extras, such as pictures.
///
/// # Example
///
/// ```rust
/// use lofty::{Accessor, ItemKey, Tag, TagExt};
/// # let tag_type = lofty::TagType::ID3v2;
///
/// let mut tag = Tag::new(tag_type);
/// assert_eq!(tag.len(), 0);
///
/// tag.set_artist(String::from("Foo artist"));
/// assert_eq!(tag.len(), 1);
/// ```
fn len(&self) -> usize;
/// Whether the tag contains an item with the key
///
/// # Example