Struct lofty::Tag

source ·
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

source

pub const fn new(tag_type: TagType) -> Self

Initialize a new tag with a certain TagType

source

pub fn re_map(&mut self, tag_type: TagType)

Change the TagType, remapping all items

source

pub fn tag_type(&self) -> TagType

Returns the TagType

source

pub fn item_count(&self) -> u32

Returns the number of TagItems

source

pub fn picture_count(&self) -> u32

Returns the number of Pictures

source

pub fn items(&self) -> impl Iterator<Item = &TagItem> + Clone

Returns the stored TagItems as a slice

source

pub fn get(&self, item_key: &ItemKey) -> Option<&TagItem>

Returns a reference to a TagItem matching an ItemKey

source

pub fn get_string(&self, item_key: &ItemKey) -> Option<&str>

Get a string value from an ItemKey

source

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

source

pub fn insert(&mut self, item: TagItem) -> bool

Insert a TagItem, replacing any existing one of the same ItemKey

NOTE: This will verify an ItemKey mapping exists for the target TagType

This will return true if the item was inserted.

source

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 ItemKey mapping 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.

source

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.

source

pub fn push_unchecked(&mut self, item: TagItem)

Append a TagItem to the tag

Notes: See Tag::insert_unchecked

source

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

source

pub fn take(&mut self, key: &ItemKey) -> impl Iterator<Item = TagItem> + '_

Removes all items with the specified ItemKey, and returns them

source

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

source

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

source

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

source

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

source

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

source

pub fn remove_key(&mut self, key: &ItemKey)

Remove an item by its key

This will remove all items with this key.

source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&TagItem) -> bool,

Retain tag items based on the predicate

See Vec::retain

source

pub fn remove_empty(&mut self)

Remove all items with empty values

source

pub fn pictures(&self) -> &[Picture]

Returns the stored Pictures as a slice

source

pub fn get_picture_type(&self, picture_type: PictureType) -> Option<&Picture>

Returns the first occurrence of the PictureType

source

pub fn push_picture(&mut self, picture: Picture)

Pushes a Picture to the tag

source

pub fn remove_picture_type(&mut self, picture_type: PictureType)

Removes all Pictures of a PictureType

source

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);
source

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

source§

fn artist(&self) -> Option<Cow<'_, str>>

Returns the artist Read more
source§

fn set_artist(&mut self, value: String)

Sets the artist Read more
source§

fn remove_artist(&mut self)

Removes the artist Read more
source§

fn title(&self) -> Option<Cow<'_, str>>

Returns the title Read more
source§

fn set_title(&mut self, value: String)

Sets the title Read more
source§

fn remove_title(&mut self)

Removes the title Read more
source§

fn album(&self) -> Option<Cow<'_, str>>

Returns the album Read more
source§

fn set_album(&mut self, value: String)

Sets the album Read more
source§

fn remove_album(&mut self)

Removes the album Read more
source§

fn genre(&self) -> Option<Cow<'_, str>>

Returns the genre Read more
source§

fn set_genre(&mut self, value: String)

Sets the genre Read more
source§

fn remove_genre(&mut self)

Removes the genre Read more
source§

fn comment(&self) -> Option<Cow<'_, str>>

Returns the comment Read more
source§

fn set_comment(&mut self, value: String)

Sets the comment Read more
source§

fn remove_comment(&mut self)

Removes the comment Read more
source§

fn track(&self) -> Option<u32>

Returns the track Read more
source§

fn set_track(&mut self, value: u32)

Sets the track Read more
source§

fn remove_track(&mut self)

Removes the track Read more
source§

fn track_total(&self) -> Option<u32>

Returns the track total Read more
source§

fn set_track_total(&mut self, value: u32)

Sets the track total Read more
source§

fn remove_track_total(&mut self)

Removes the track total Read more
source§

fn disk(&self) -> Option<u32>

Returns the disk Read more
source§

fn set_disk(&mut self, value: u32)

Sets the disk Read more
source§

fn remove_disk(&mut self)

Removes the disk Read more
source§

fn disk_total(&self) -> Option<u32>

Returns the disk total Read more
source§

fn set_disk_total(&mut self, value: u32)

Sets the disk total Read more
source§

fn remove_disk_total(&mut self)

Removes the disk total Read more
source§

fn year(&self) -> Option<u32>

Returns the year Read more
source§

fn set_year(&mut self, value: u32)

Sets the year Read more
source§

fn remove_year(&mut self)

Removes the year Read more
source§

impl Clone for Tag

source§

fn clone(&self) -> Tag

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl From<AIFFTextChunks> for Tag

source§

fn from(input: AIFFTextChunks) -> Self

Converts to this type from the input type.
source§

impl From<ApeTag> for Tag

source§

fn from(input: ApeTag) -> Self

Converts to this type from the input type.
source§

impl From<Id3v1Tag> for Tag

source§

fn from(input: Id3v1Tag) -> Self

Converts to this type from the input type.
source§

impl From<Id3v2Tag> for Tag

source§

fn from(input: Id3v2Tag) -> Self

Converts to this type from the input type.
source§

impl From<Ilst> for Tag

source§

fn from(input: Ilst) -> Self

Converts to this type from the input type.
source§

impl From<RIFFInfoList> for Tag

source§

fn from(input: RIFFInfoList) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for AIFFTextChunks

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for ApeTag

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for Id3v1Tag

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for Id3v2Tag

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for Ilst

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for RIFFInfoList

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<Tag> for VorbisComments

source§

fn from(input: Tag) -> Self

Converts to this type from the input type.
source§

impl From<VorbisComments> for Tag

source§

fn from(input: VorbisComments) -> Self

Converts to this type from the input type.
source§

impl SplitTag for Tag

§

type Remainder = SplitTagRemainder

The remainder of the split operation that is not represented in the resulting Tag.
source§

fn split_tag(self) -> (Self::Remainder, Self)

Extract and split generic contents into a Tag. Read more
source§

impl TagExt for Tag

source§

fn save_to(&self, file: &mut File) -> Result<(), Self::Err>

Save the Tag to a File

§Errors
source§

fn remove_from_path<P: AsRef<Path>>(&self, path: P) -> Result<(), Self::Err>

Remove a tag from a Path

§Errors

See TagType::remove_from

source§

fn remove_from(&self, file: &mut File) -> Result<(), Self::Err>

Remove a tag from a File

§Errors

See TagType::remove_from

§

type Err = LoftyError

The associated error which can be returned from IO operations
§

type RefKey<'a> = &'a ItemKey

The type of key used in the tag for non-mutating functions
source§

fn len(&self) -> usize

Returns the number of items in the tag Read more
source§

fn contains<'a>(&'a self, key: Self::RefKey<'a>) -> bool

Whether the tag contains an item with the key Read more
source§

fn is_empty(&self) -> bool

Whether the tag has any items Read more
source§

fn dump_to<W: Write>(&self, writer: &mut W) -> Result<()>

Dump the tag to a writer Read more
source§

fn clear(&mut self)

Clear the tag, removing all items Read more
source§

fn save_to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), Self::Err>

Save the tag to a path Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Tag

§

impl Send for Tag

§

impl Sync for Tag

§

impl Unpin for Tag

§

impl UnwindSafe for Tag

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.