Skip to content

Add into_inner for BoundTaggedFile #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
in an immutable container, and silently rejoined with the tag when converting back to the original format
or when writing.
- **TagItem**: `set_lang` and `set_description` to allow for generic conversions of additional ID3v2 frames (such as comments) ([issue](https://github.com/Serial-ATA/lofty-rs/issues/383)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/392))
- **BoundTaggedFile**: `BoundTaggedFile::into_inner` to get the original file handle ([PR](https://github.com/Serial-ATA/lofty-rs/pull/404))

### Changed
- **VorbisComments**/**ApeTag**: Verify contents of `ItemKey::FlagCompilation` during `Tag` merge ([PR](https://github.com/Serial-ATA/lofty-rs/pull/387))
Expand Down
9 changes: 9 additions & 0 deletions lofty/src/file/tagged_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,15 @@ impl BoundTaggedFile {

Ok(())
}

/// Consume this tagged file and return the internal file "buffer".
/// This allows you to reuse the internal file.
///
/// Any changes that haven't been commited will be discarded once you
/// call this function.
pub fn into_inner(self) -> File {
self.file_handle
}
}

impl TaggedFileExt for BoundTaggedFile {
Expand Down
25 changes: 24 additions & 1 deletion lofty/tests/files/mpeg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{set_artist, temp_file, verify_artist};
use lofty::config::{ParseOptions, WriteOptions};
use lofty::file::FileType;
use lofty::file::{BoundTaggedFile, FileType};
use lofty::id3::v2::{Frame, FrameId, Id3v2Tag, KeyValueFrame};
use lofty::mpeg::MpegFile;
use lofty::prelude::*;
Expand Down Expand Up @@ -210,6 +210,29 @@ fn save_number_of_track_and_disk_to_id3v2() {
assert!(tag.disk_total().is_none());
}

#[test]
fn test_bound_tagged_into_inner() {
let file = temp_file!("tests/files/assets/minimal/full_test.mp3");

let mut bounded = BoundTaggedFile::read_from(file, ParseOptions::default()).unwrap();

let tag = bounded
.tag_mut(TagType::Id3v2)
.expect("Couldn't get ref to tag");
tag.set_disk(123);
bounded
.save(WriteOptions::default())
.expect("Couldn't save tags");

// Reread the file
let mut original_file = bounded.into_inner();
original_file.rewind().unwrap();
let mut bounded = BoundTaggedFile::read_from(original_file, ParseOptions::default()).unwrap();
let tag = bounded.tag_mut(TagType::Id3v2).unwrap();

assert_eq!(tag.disk(), Some(123));
}

#[test]
fn save_total_of_track_and_disk_to_id3v2() {
let mut file = temp_file!("tests/files/assets/minimal/full_test.mp3");
Expand Down