Skip to content

Commit

Permalink
feat: make file-related APIs compatible with io::Cursor (#121)
Browse files Browse the repository at this point in the history
The codebase defines a StorageFile trait to model anything similar to a file.
By using `impl StorageFile` instead of `fs::File` in the API signatures, users
are able to provide either a file or something similar (for example, `io::Cursor`
to manipulate a block of data in memory).

This commit updates the APIs still using `fs::File` to `impl StorageFile`.
  • Loading branch information
davidepedranz committed Dec 27, 2023
1 parent 0377e82 commit 4586dd0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::storage::{PlainStorage, Storage};
use crate::{Error, ErrorKind, Tag, Version};
use crate::{Error, ErrorKind, StorageFile, Tag, Version};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use std::convert::TryFrom;
use std::fmt;
use std::fs;
use std::io::prelude::*;
use std::io::{BufReader, Seek, SeekFrom};
use std::{convert::TryInto, io};
Expand Down Expand Up @@ -36,7 +35,7 @@ where
/// Writes a tag to the given file. If the file contains no previous tag data, a new ID3
/// chunk is created. Otherwise, the tag is overwritten in place.
pub fn write_id3_chunk_file<F: ChunkFormat>(
mut file: &mut fs::File,
mut file: impl StorageFile,
tag: &Tag,
version: Version,
) -> crate::Result<()> {
Expand Down
14 changes: 9 additions & 5 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a> Tag {
/// Removes an ID3v2 tag from the specified file.
///
/// Returns true if the file initially contained a tag.
pub fn remove_from_file(mut file: &mut fs::File) -> crate::Result<bool> {
pub fn remove_from_file(mut file: impl StorageFile) -> crate::Result<bool> {
let location = match stream::tag::locate_id3v2(&mut file)? {
Some(l) => l,
None => return Ok(false),
Expand Down Expand Up @@ -167,7 +167,7 @@ impl<'a> Tag {
}

/// Reads an AIFF file and returns any present ID3 tag.
pub fn read_from_aiff_file(file: &mut fs::File) -> crate::Result<Tag> {
pub fn read_from_aiff_file(file: impl StorageFile) -> crate::Result<Tag> {
chunk::load_id3_chunk::<chunk::AiffFormat, _>(file)
}

Expand All @@ -183,7 +183,7 @@ impl<'a> Tag {
}

/// Reads an WAV file and returns any present ID3 tag.
pub fn read_from_wav_file(file: &mut fs::File) -> crate::Result<Tag> {
pub fn read_from_wav_file(file: impl StorageFile) -> crate::Result<Tag> {
chunk::load_id3_chunk::<chunk::WavFormat, _>(file)
}

Expand Down Expand Up @@ -237,7 +237,11 @@ impl<'a> Tag {
}

/// Overwrite AIFF file ID3 chunk in a file. The file must be opened read/write.
pub fn write_to_aiff_file(&self, file: &mut fs::File, version: Version) -> crate::Result<()> {
pub fn write_to_aiff_file(
&self,
file: impl StorageFile,
version: Version,
) -> crate::Result<()> {
chunk::write_id3_chunk_file::<chunk::AiffFormat>(file, self, version)
}

Expand All @@ -255,7 +259,7 @@ impl<'a> Tag {
}

/// Overwrite AIFF file ID3 chunk in a file. The file must be opened read/write.
pub fn write_to_wav_file(&self, file: &mut fs::File, version: Version) -> crate::Result<()> {
pub fn write_to_wav_file(&self, file: impl StorageFile, version: Version) -> crate::Result<()> {
chunk::write_id3_chunk_file::<chunk::WavFormat>(file, self, version)
}

Expand Down

0 comments on commit 4586dd0

Please sign in to comment.