Skip to content

Commit

Permalink
[feat] add into_inner for BoundTaggedFile
Browse files Browse the repository at this point in the history
  • Loading branch information
rakbladsvalsen committed Jun 5, 2024
1 parent d2d4b81 commit bc3418c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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
40 changes: 39 additions & 1 deletion lofty/tests/files/mpeg.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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::*;
use lofty::probe::Probe;
use lofty::tag::{Tag, TagType};

use std::borrow::Cow;
use std::fs::OpenOptions;
use std::io::{Seek, Write};

#[test]
Expand Down Expand Up @@ -210,6 +211,43 @@ fn save_number_of_track_and_disk_to_id3v2() {
assert!(tag.disk_total().is_none());
}

#[test]
fn test_bound_tagged_into_inner() {
let file = OpenOptions::new()
.read(true)
.write(true)
.open("tests/files/assets/minimal/full_test.mp3")
.expect("Cannot open file");
let mut bounded =
BoundTaggedFile::read_from(file, Default::default()).expect("Couldn't parse file");
let tag = bounded
.tag_mut(TagType::Id3v2)
.expect("Couldn't get ref to tag");
let original_disk = tag.disk();
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().expect("Couldn't rewind");
let mut bounded = BoundTaggedFile::read_from(original_file, Default::default())
.expect("Couldn't reparse file");
let tag = bounded
.tag_mut(TagType::Id3v2)
.expect("Cannot get ref to tag");

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

// Revert the file to whatever it was after this test.
if let Some(disk) = original_disk {
tag.set_disk(disk);
}
bounded
.save(WriteOptions::default())
.expect("Couldn't save tags");
}

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

0 comments on commit bc3418c

Please sign in to comment.