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§

Initialize a new tag with a certain TagType

Change the TagType, remapping all items

Returns the TagType

Returns the number of TagItems

Returns the number of Pictures

Returns the stored TagItems as a slice

Returns a reference to a TagItem matching an ItemKey

Get a string value from an ItemKey

Gets a byte slice from an ItemKey

Use convert to convert ItemValue::Text and ItemValue::Locator to byte slices

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.

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.

Append a TagItem to the tag

This will not remove any items of the same ItemKey, unlike Tag::insert_item

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.

Append a TagItem to the tag

Notes: See Tag::insert_item_unchecked

An alias for Tag::insert_item that doesn’t require the user to create a TagItem

NOTE: This will replace any existing item with item_key. See Tag::insert_item

Removes all items with the specified ItemKey, and returns them

Removes all items with the specified ItemKey, and filters them through ItemValue::into_string

Returns references to all TagItems with the specified key

Returns references to all texts of TagItems with the specified key, and ItemValue::Text

Returns references to all locators of TagItems with the specified key, and ItemValue::Locator

Returns references to all bytes of TagItems with the specified key, and ItemValue::Binary

Remove an item by its key

This will remove all items with this key.

Retain tag items based on the predicate

See Vec::retain

Returns the stored Pictures as a slice

Returns the first occurrence of the PictureType

Pushes a Picture to the tag

Removes all Pictures of a PictureType

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

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§

Returns the artist Read more
Sets the artist Read more
Removes the artist Read more
Returns the title Read more
Sets the title Read more
Removes the title Read more
Returns the album Read more
Sets the album Read more
Removes the album Read more
Returns the genre Read more
Sets the genre Read more
Removes the genre Read more
Returns the comment Read more
Sets the comment Read more
Removes the comment Read more
Returns the track Read more
Sets the track Read more
Removes the track Read more
Returns the track total Read more
Sets the track total Read more
Removes the track total Read more
Returns the disk Read more
Sets the disk Read more
Removes the disk Read more
Returns the disk total Read more
Sets the disk total Read more
Removes the disk total Read more
Returns the year Read more
Sets the year Read more
Removes the year Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Extract and split generic contents into a Tag. Read more
Rejoin a Tag. Read more

Save the Tag to a path

Errors
  • Path doesn’t exist
  • Path is not writable
  • See Tag::save_to

Save the Tag to a File

Errors

Remove a tag from a Path

Errors

See TagType::remove_from

Remove a tag from a File

Errors

See TagType::remove_from

The associated error which can be returned from IO operations
The type of key used in the tag for non-mutating functions
Returns the number of items in the tag Read more
Whether the tag contains an item with the key Read more
Whether the tag has any items Read more
Dump the tag to a writer Read more
Clear the tag, removing all items Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.