pub struct Tag { /* private fields */ }Expand description
Represents a parsed tag
This is a tag that is loosely bound to a specific TagType.
It is used for conversions and as the return type for read_from.
Compared to other formats, this gives a much higher-level view of the
tag items. Rather than storing items according to their format-specific
keys, ItemKeys are used.
You can easily remap this to another TagType with Tag::re_map.
Any conversion will, of course, be lossy to a varying degree.
Usage
Accessing common items
use lofty::{Accessor, Tag, TagType};
let tag = Tag::new(TagType::Id3v2);
// There are multiple quick getter methods for common items
let title = tag.title();
let artist = tag.artist();
let album = tag.album();
let genre = tag.genre();Getting an item of a known type
use lofty::{ItemKey, Tag, TagType};
let tag = Tag::new(TagType::Id3v2);
// If the type of an item is known, there are getter methods
// to prevent having to match against the value
tag.get_string(&ItemKey::TrackTitle);
tag.get_binary(&ItemKey::TrackTitle, false);Converting between formats
use lofty::id3::v2::Id3v2Tag;
use lofty::{Tag, TagType};
// Converting between formats is as simple as an `into` call.
// However, such conversions can potentially be *very* lossy.
let tag = Tag::new(TagType::Id3v2);
let id3v2_tag: Id3v2Tag = tag.into();Implementations§
source§impl Tag
impl Tag
sourcepub fn item_count(&self) -> u32
pub fn item_count(&self) -> u32
Returns the number of TagItems
sourcepub fn picture_count(&self) -> u32
pub fn picture_count(&self) -> u32
Returns the number of Pictures
sourcepub fn items(&self) -> impl Iterator<Item = &TagItem> + Clone
pub fn items(&self) -> impl Iterator<Item = &TagItem> + Clone
Returns the stored TagItems as a slice
sourcepub fn get_string(&self, item_key: &ItemKey) -> Option<&str>
pub fn get_string(&self, item_key: &ItemKey) -> Option<&str>
Get a string value from an ItemKey
sourcepub fn get_binary(&self, item_key: &ItemKey, convert: bool) -> Option<&[u8]>
pub fn get_binary(&self, item_key: &ItemKey, convert: bool) -> Option<&[u8]>
Gets a byte slice from an ItemKey
Use convert to convert ItemValue::Text and ItemValue::Locator to byte slices
sourcepub fn insert_unchecked(&mut self, item: TagItem)
pub fn insert_unchecked(&mut self, item: TagItem)
Insert a TagItem, replacing any existing one of the same ItemKey
Notes:
- This will not verify an
ItemKeymapping exists - This will not allow writing item keys that are out of spec (keys are verified before writing)
This is only necessary if dealing with ItemKey::Unknown.
sourcepub fn push(&mut self, item: TagItem) -> bool
pub fn push(&mut self, item: TagItem) -> bool
Append a TagItem to the tag
This will not remove any items of the same ItemKey, unlike Tag::insert
NOTE: This will verify an ItemKey mapping exists for the target TagType
Multiple items of the same ItemKey are not valid in all formats, in which case
the first available item will be used.
This will return true if the item was pushed.
sourcepub fn push_unchecked(&mut self, item: TagItem)
pub fn push_unchecked(&mut self, item: TagItem)
Append a TagItem to the tag
Notes: See Tag::insert_unchecked
sourcepub fn insert_text(&mut self, item_key: ItemKey, text: String) -> bool
pub fn insert_text(&mut self, item_key: ItemKey, text: String) -> bool
An alias for Tag::insert that doesn’t require the user to create a TagItem
NOTE: This will replace any existing item with item_key. See Tag::insert
sourcepub fn take(&mut self, key: &ItemKey) -> impl Iterator<Item = TagItem> + '_
pub fn take(&mut self, key: &ItemKey) -> impl Iterator<Item = TagItem> + '_
Removes all items with the specified ItemKey, and returns them
sourcepub fn take_strings(
&mut self,
key: &ItemKey
) -> impl Iterator<Item = String> + '_
pub fn take_strings( &mut self, key: &ItemKey ) -> impl Iterator<Item = String> + '_
Removes all items with the specified ItemKey, and filters them through ItemValue::into_string
sourcepub fn get_items<'a>(
&'a self,
key: &'a ItemKey
) -> impl Iterator<Item = &'a TagItem> + Clone
pub fn get_items<'a>( &'a self, key: &'a ItemKey ) -> impl Iterator<Item = &'a TagItem> + Clone
Returns references to all TagItems with the specified key
sourcepub fn get_strings<'a>(
&'a self,
key: &'a ItemKey
) -> impl Iterator<Item = &'a str> + Clone
pub fn get_strings<'a>( &'a self, key: &'a ItemKey ) -> impl Iterator<Item = &'a str> + Clone
Returns references to all texts of TagItems with the specified key, and ItemValue::Text
sourcepub fn get_locators<'a>(
&'a self,
key: &'a ItemKey
) -> impl Iterator<Item = &'a str> + Clone
pub fn get_locators<'a>( &'a self, key: &'a ItemKey ) -> impl Iterator<Item = &'a str> + Clone
Returns references to all locators of TagItems with the specified key, and ItemValue::Locator
sourcepub fn get_bytes<'a>(
&'a self,
key: &'a ItemKey
) -> impl Iterator<Item = &'a [u8]> + Clone
pub fn get_bytes<'a>( &'a self, key: &'a ItemKey ) -> impl Iterator<Item = &'a [u8]> + Clone
Returns references to all bytes of TagItems with the specified key, and ItemValue::Binary
sourcepub fn remove_key(&mut self, key: &ItemKey)
pub fn remove_key(&mut self, key: &ItemKey)
Remove an item by its key
This will remove all items with this key.
sourcepub fn retain<F>(&mut self, f: F)where
F: FnMut(&TagItem) -> bool,
pub fn retain<F>(&mut self, f: F)where F: FnMut(&TagItem) -> bool,
Retain tag items based on the predicate
See Vec::retain
sourcepub fn remove_empty(&mut self)
pub fn remove_empty(&mut self)
Remove all items with empty values
sourcepub fn get_picture_type(&self, picture_type: PictureType) -> Option<&Picture>
pub fn get_picture_type(&self, picture_type: PictureType) -> Option<&Picture>
Returns the first occurrence of the PictureType
sourcepub fn push_picture(&mut self, picture: Picture)
pub fn push_picture(&mut self, picture: Picture)
Pushes a Picture to the tag
sourcepub fn remove_picture_type(&mut self, picture_type: PictureType)
pub fn remove_picture_type(&mut self, picture_type: PictureType)
Removes all Pictures of a PictureType
sourcepub fn set_picture(&mut self, index: usize, picture: Picture)
pub fn set_picture(&mut self, index: usize, picture: Picture)
Replaces the picture at the given index
NOTE: If index is out of bounds, the picture will be appended
to the list.
Examples
use lofty::{Picture, Tag, TagType};
let mut tag = Tag::new(TagType::Id3v2);
// Add a front cover
tag.push_picture(front_cover);
assert_eq!(tag.pictures().len(), 1);
assert_eq!(tag.pictures()[0].pic_type(), PictureType::CoverFront);
// Replace the front cover with a back cover
tag.set_picture(0, back_cover);
assert_eq!(tag.pictures().len(), 1);
assert_eq!(tag.pictures()[0].pic_type(), PictureType::CoverBack);
// Use an out of bounds index
tag.set_picture(100, another_picture);
assert_eq!(tag.pictures().len(), 2);sourcepub fn remove_picture(&mut self, index: usize) -> Picture
pub fn remove_picture(&mut self, index: usize) -> Picture
Removes and returns the picture at the given index
Panics
Panics if index is out of bounds.
Examples
use lofty::{Picture, Tag, TagType};
let mut tag = Tag::new(TagType::Id3v2);
tag.push_picture(picture);
assert_eq!(tag.pictures().len(), 1);
tag.remove_picture(0);
assert_eq!(tag.pictures().len(), 0);Trait Implementations§
source§impl Accessor for Tag
impl Accessor for Tag
source§fn set_artist(&mut self, value: String)
fn set_artist(&mut self, value: String)
source§fn remove_artist(&mut self)
fn remove_artist(&mut self)
source§fn remove_title(&mut self)
fn remove_title(&mut self)
source§fn remove_album(&mut self)
fn remove_album(&mut self)
source§fn remove_genre(&mut self)
fn remove_genre(&mut self)
source§fn set_comment(&mut self, value: String)
fn set_comment(&mut self, value: String)
source§fn remove_comment(&mut self)
fn remove_comment(&mut self)
source§fn remove_track(&mut self)
fn remove_track(&mut self)
source§fn set_track_total(&mut self, value: u32)
fn set_track_total(&mut self, value: u32)
source§fn remove_track_total(&mut self)
fn remove_track_total(&mut self)
source§fn remove_disk(&mut self)
fn remove_disk(&mut self)
source§fn set_disk_total(&mut self, value: u32)
fn set_disk_total(&mut self, value: u32)
source§fn remove_disk_total(&mut self)
fn remove_disk_total(&mut self)
source§fn remove_year(&mut self)
fn remove_year(&mut self)
source§impl From<AIFFTextChunks> for Tag
impl From<AIFFTextChunks> for Tag
source§fn from(input: AIFFTextChunks) -> Self
fn from(input: AIFFTextChunks) -> Self
source§impl From<RIFFInfoList> for Tag
impl From<RIFFInfoList> for Tag
source§fn from(input: RIFFInfoList) -> Self
fn from(input: RIFFInfoList) -> Self
source§impl From<Tag> for AIFFTextChunks
impl From<Tag> for AIFFTextChunks
source§impl From<Tag> for RIFFInfoList
impl From<Tag> for RIFFInfoList
source§impl From<Tag> for VorbisComments
impl From<Tag> for VorbisComments
source§impl From<VorbisComments> for Tag
impl From<VorbisComments> for Tag
source§fn from(input: VorbisComments) -> Self
fn from(input: VorbisComments) -> Self
source§impl TagExt for Tag
impl TagExt for Tag
source§fn save_to(&self, file: &mut File) -> Result<(), Self::Err>
fn save_to(&self, file: &mut File) -> Result<(), Self::Err>
Save the Tag to a File
Errors
- A
FileTypecouldn’t be determined from the File - Attempting to write a tag to a format that does not support it. See
FileType::supports_tag_type