Struct lofty::BoundTaggedFile

source ·
pub struct BoundTaggedFile { /* private fields */ }
Expand description

A variant of TaggedFile that holds a File handle, and reflects changes such as tag removals.

For example:

use lofty::{AudioFile, Tag, TagType, TaggedFileExt};

// We create an empty tag
let tag = Tag::new(TagType::Id3v2);

let mut tagged_file = lofty::read_from_path(path)?;

// Push our empty tag into the TaggedFile
tagged_file.insert_tag(tag);

// After saving, our file still "contains" the ID3v2 tag, but if we were to read
// "foo.mp3", it would not have an ID3v2 tag. Lofty does not write empty tags, but this
// change will not be reflected in `TaggedFile`.
tagged_file.save_to_path("foo.mp3")?;
assert!(tagged_file.contains_tag_type(TagType::Id3v2));

However, when using BoundTaggedFile:

use lofty::{AudioFile, BoundTaggedFile, ParseOptions, Tag, TagType, TaggedFileExt};
use std::fs::OpenOptions;

// We create an empty tag
let tag = Tag::new(TagType::Id3v2);

// We'll need to open our file for reading *and* writing
let file = OpenOptions::new().read(true).write(true).open(path)?;
let parse_options = ParseOptions::new();

let mut bound_tagged_file = BoundTaggedFile::read_from(file, parse_options)?;

// Push our empty tag into the TaggedFile
bound_tagged_file.insert_tag(tag);

// Now when saving, we no longer have to specify a path, and the tags in the `BoundTaggedFile`
// reflect those in the actual file on disk.
bound_tagged_file.save()?;
assert!(!bound_tagged_file.contains_tag_type(TagType::Id3v2));

Implementations§

source§

impl BoundTaggedFile

source

pub fn read_from(file: File, parse_options: ParseOptions) -> Result<Self>

Create a new BoundTaggedFile

Errors

See AudioFile::read_from

Examples
use lofty::{AudioFile, BoundTaggedFile, ParseOptions, Tag, TagType, TaggedFileExt};
use std::fs::OpenOptions;

// We'll need to open our file for reading *and* writing
let file = OpenOptions::new().read(true).write(true).open(path)?;
let parse_options = ParseOptions::new();

let bound_tagged_file = BoundTaggedFile::read_from(file, parse_options)?;
source

pub fn save(&mut self) -> Result<()>

Save the tags to the file stored internally

Errors

See TaggedFile::save_to

Examples
use lofty::{AudioFile, BoundTaggedFile, ParseOptions, Tag, TagType, TaggedFileExt};
use std::fs::OpenOptions;

// We'll need to open our file for reading *and* writing
let file = OpenOptions::new().read(true).write(true).open(path)?;
let parse_options = ParseOptions::new();

let mut bound_tagged_file = BoundTaggedFile::read_from(file, parse_options)?;

// Do some work to the tags...

// This will save the tags to the file we provided to `read_from`
bound_tagged_file.save()?;

Trait Implementations§

source§

impl AudioFile for BoundTaggedFile

§

type Properties = FileProperties

The struct the file uses for audio properties Read more
source§

fn read_from<R>(_: &mut R, _: ParseOptions) -> Result<Self>where R: Read + Seek, Self: Sized,

Read a file from a reader Read more
source§

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

Attempts to write all tags to a file Read more
source§

fn properties(&self) -> &Self::Properties

Returns a reference to the file’s properties
source§

fn contains_tag(&self) -> bool

Checks if the file contains any tags
source§

fn contains_tag_type(&self, tag_type: TagType) -> bool

Checks if the file contains the given TagType
source§

fn save_to_path(&self, path: impl AsRef<Path>) -> Result<()>

Attempts to write all tags to a path Read more
source§

impl From<BoundTaggedFile> for TaggedFile

source§

fn from(input: BoundTaggedFile) -> Self

Converts to this type from the input type.
source§

impl TaggedFileExt for BoundTaggedFile

source§

fn file_type(&self) -> FileType

Returns the file’s FileType Read more
source§

fn tags(&self) -> &[Tag]

Returns all tags Read more
source§

fn tag(&self, tag_type: TagType) -> Option<&Tag>

Get a reference to a specific TagType Read more
source§

fn tag_mut(&mut self, tag_type: TagType) -> Option<&mut Tag>

Get a mutable reference to a specific TagType Read more
source§

fn first_tag_mut(&mut self) -> Option<&mut Tag>

Gets a mutable reference to the first tag, if there are any Read more
source§

fn insert_tag(&mut self, tag: Tag) -> Option<Tag>

Inserts a Tag Read more
source§

fn remove(&mut self, tag_type: TagType) -> Option<Tag>

Removes a specific TagType and returns it Read more
source§

fn clear(&mut self)

Removes all tags from the file Read more
source§

fn primary_tag_type(&self) -> TagType

Returns the file type’s primary TagType Read more
source§

fn supports_tag_type(&self, tag_type: TagType) -> bool

Determines whether the file supports the given TagType Read more
source§

fn primary_tag(&self) -> Option<&Tag>

Returns the primary tag Read more
source§

fn primary_tag_mut(&mut self) -> Option<&mut Tag>

Gets a mutable reference to the file’s “Primary tag” Read more
source§

fn first_tag(&self) -> Option<&Tag>

Gets the first tag, if there are any Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.