Struct lofty::TaggedFile
source · [−]pub struct TaggedFile { /* private fields */ }Expand description
A generic representation of a file
This is used when the FileType has to be guessed
Implementations
sourceimpl TaggedFile
impl TaggedFile
Returns all tags
Examples
use lofty::FileType;
// An MP3 file with 3 tags
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
let tags = tagged_file.tags();
assert_eq!(tags.len(), 3);sourcepub fn primary_tag_type(&self) -> TagType
pub fn primary_tag_type(&self) -> TagType
Returns the file type’s primary TagType
See FileType::primary_tag_type
Examples
use lofty::TagType;
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
assert_eq!(tagged_file.primary_tag_type(), TagType::ID3v2);sourcepub fn supports_tag_type(&self, tag_type: TagType) -> bool
pub fn supports_tag_type(&self, tag_type: TagType) -> bool
sourcepub fn tag_mut(&mut self, tag_type: TagType) -> Option<&mut Tag>
pub fn tag_mut(&mut self, tag_type: TagType) -> Option<&mut Tag>
Get a mutable reference to a specific TagType
Examples
use lofty::TagType;
// Read an MP3 file with an ID3v2 tag
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
// An ID3v2 tag
let tag = tagged_file.tag(TagType::ID3v2);
assert!(tag.is_some());
assert_eq!(tag.unwrap().tag_type(), TagType::ID3v2);
// Alter the tag...sourcepub fn primary_tag(&self) -> Option<&Tag>
pub fn primary_tag(&self) -> Option<&Tag>
Returns the primary tag
See FileType::primary_tag_type
Examples
use lofty::TagType;
// Read an MP3 file with an ID3v2 tag
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
// An ID3v2 tag
let tag = tagged_file.primary_tag();
assert!(tag.is_some());
assert_eq!(tag.unwrap().tag_type(), TagType::ID3v2);sourcepub fn primary_tag_mut(&mut self) -> Option<&mut Tag>
pub fn primary_tag_mut(&mut self) -> Option<&mut Tag>
Gets a mutable reference to the file’s “Primary tag”
See FileType::primary_tag_type
Examples
use lofty::TagType;
// Read an MP3 file with an ID3v2 tag
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
// An ID3v2 tag
let tag = tagged_file.primary_tag_mut();
assert!(tag.is_some());
assert_eq!(tag.unwrap().tag_type(), TagType::ID3v2);
// Alter the tag...sourcepub fn first_tag(&self) -> Option<&Tag>
pub fn first_tag(&self) -> Option<&Tag>
Gets the first tag, if there are any
NOTE: This will grab the first available tag, you cannot rely on the result being a specific type
Examples
// A file we know has tags
let mut tagged_file = lofty::read_from_path(path)?;
// A tag of a (currently) unknown type
let tag = tagged_file.first_tag();
assert!(tag.is_some());sourcepub fn first_tag_mut(&mut self) -> Option<&mut Tag>
pub fn first_tag_mut(&mut self) -> Option<&mut Tag>
Gets a mutable reference to the first tag, if there are any
NOTE: This will grab the first available tag, you cannot rely on the result being a specific type
Examples
// A file we know has tags
let mut tagged_file = lofty::read_from_path(path)?;
// A tag of a (currently) unknown type
let tag = tagged_file.first_tag_mut();
assert!(tag.is_some());
// Alter the tag...sourcepub fn insert_tag(&mut self, tag: Tag) -> Option<Tag>
pub fn insert_tag(&mut self, tag: Tag) -> Option<Tag>
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
Examples
use lofty::{AudioFile, Tag, TagType};
// Read an MP3 file without an ID3v2 tag
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
assert!(!tagged_file.contains_tag_type(TagType::ID3v2));
// Insert the ID3v2 tag
let new_id3v2_tag = Tag::new(TagType::ID3v2);
tagged_file.insert_tag(new_id3v2_tag);
assert!(tagged_file.contains_tag_type(TagType::ID3v2));sourcepub fn take(&mut self, tag_type: TagType) -> Option<Tag>
pub fn take(&mut self, tag_type: TagType) -> Option<Tag>
Removes a specific TagType and returns it
Examples
use lofty::{AudioFile, TagType};
// Read an MP3 file containing an ID3v2 tag
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
assert!(tagged_file.contains_tag_type(TagType::ID3v2));
// Take the ID3v2 tag
let id3v2 = tagged_file.take(TagType::ID3v2);
assert!(!tagged_file.contains_tag_type(TagType::ID3v2));sourcepub fn change_file_type(&mut self, file_type: FileType)
pub fn change_file_type(&mut self, file_type: FileType)
Changes the FileType
NOTES:
- This will remove any tag the format does not support. See
FileType::supports_tag_type - This will reset the
FileProperties
Examples
use lofty::{AudioFile, FileType, TagType};
// Read an MP3 file containing an ID3v2 tag
let mut tagged_file = lofty::read_from_path(path_to_mp3)?;
assert!(tagged_file.contains_tag_type(TagType::ID3v2));
// Remap our MP3 file to WavPack, which doesn't support ID3v2
tagged_file.change_file_type(FileType::WavPack);
assert!(!tagged_file.contains_tag_type(TagType::ID3v2));sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all tags from the file
Examples
let mut tagged_file = lofty::read_from_path(path)?;
tagged_file.clear();
assert!(tagged_file.tags().is_empty());sourcepub fn save_to_path(&self, path: impl AsRef<Path>) -> Result<()>
pub fn save_to_path(&self, path: impl AsRef<Path>) -> Result<()>
sourcepub fn save_to(&self, file: &mut File) -> Result<()>
pub fn save_to(&self, file: &mut File) -> Result<()>
Attempts to write all tags to a file
Errors
See Tag::save_to, however this is applicable to every tag in the TaggedFile.
Examples
use std::fs::OpenOptions;
let mut tagged_file = lofty::read_from_path(path)?;
// Edit the tags
let mut file = OpenOptions::new().read(true).write(true).open(path)?;
tagged_file.save_to(&mut file)?;Trait Implementations
sourceimpl AudioFile for TaggedFile
impl AudioFile for TaggedFile
type Properties = FileProperties
type Properties = FileProperties
sourcefn read_from<R>(reader: &mut R, parse_options: ParseOptions) -> Result<Self>where
R: Read + Seek,
Self: Sized,
fn read_from<R>(reader: &mut R, parse_options: ParseOptions) -> Result<Self>where
R: Read + Seek,
Self: Sized,
sourcefn properties(&self) -> &Self::Properties
fn properties(&self) -> &Self::Properties
sourcefn contains_tag(&self) -> bool
fn contains_tag(&self) -> bool
sourcefn contains_tag_type(&self, tag_type: TagType) -> bool
fn contains_tag_type(&self, tag_type: TagType) -> bool
TagType