Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/components/flac_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl AudioTagEdit for FlacTag {
self.remove("ALBUMARTIST");
}

fn album_cover(&self) -> Option<Picture> {
fn album_cover(&self) -> Option<Picture<'_>> {
self.inner
.pictures()
.find(|&pic| matches!(pic.picture_type, metaflac::block::PictureType::CoverFront))
Expand Down
2 changes: 1 addition & 1 deletion src/components/id3_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl AudioTagEdit for Id3v2Tag {
self.inner.remove_album_artist();
}

fn album_cover(&self) -> Option<Picture> {
fn album_cover(&self) -> Option<Picture<'_>> {
self.inner
.pictures()
.find(|&pic| matches!(pic.picture_type, id3::frame::PictureType::CoverFront))
Expand Down
2 changes: 1 addition & 1 deletion src/components/mp4_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl AudioTagEdit for Mp4Tag {
self.inner.add_album_artist(v);
}

fn album_cover(&self) -> Option<Picture> {
fn album_cover(&self) -> Option<Picture<'_>> {
self.inner.artwork().and_then(|data| match data.fmt {
ImgFmt::Jpeg => Some(Picture {
data: data.data,
Expand Down
45 changes: 36 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,46 @@ impl Tag {
config,
}
}

/// Creates a brand new empty tag without reading from an existing file path.
pub fn empty(tag_type: TagType, config: Config) -> Box<dyn AudioTag + Send + Sync> {
match tag_type {
TagType::Id3v2 => Box::new({
let mut t = Id3v2Tag::new();
t.set_config(config);
t
}),
TagType::Mp4 => Box::new({
let mut t = Mp4Tag::new();
t.set_config(config);
t
}),
TagType::Flac => Box::new({
let mut t = FlacTag::new();
t.set_config(config);
t
}),
}
}

pub fn read_from_path(
&self,
path: impl AsRef<Path>,
) -> crate::Result<Box<dyn AudioTag + Send + Sync>> {
match self.tag_type.unwrap_or(TagType::try_from_ext(
path.as_ref()
.extension()
.ok_or(Error::UnknownFileExtension(String::new()))?
.to_string_lossy()
.to_string()
.to_lowercase()
.as_str(),
)?) {
let tag_type = match self.tag_type {
Some(tag_type) => tag_type,
None => TagType::try_from_ext(
path.as_ref()
.extension()
.ok_or(Error::UnknownFileExtension(String::new()))?
.to_string_lossy()
.to_string()
.to_lowercase()
.as_str(),
)?,
};

match tag_type {
TagType::Id3v2 => Ok(Box::new({
let mut t = Id3v2Tag::read_from_path(path)?;
t.set_config(self.config);
Expand Down
2 changes: 1 addition & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub trait AudioTagEdit: AudioTagConfig {
self.set_album_artist(artist);
}

fn album_cover(&self) -> Option<Picture>;
fn album_cover(&self) -> Option<Picture<'_>>;
fn set_album_cover(&mut self, cover: Picture);
fn remove_album_cover(&mut self);

Expand Down
13 changes: 13 additions & 0 deletions tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,16 @@ macro_rules! test_file {
test_file!(test_mp3, "assets/a.mp3");
test_file!(test_m4a, "assets/a.m4a");
test_file!(test_flac, "assets/a.flac");

#[test]
fn test_read_from_path_honours_tag_type() {
let tmp = Builder::new().suffix(".xyz").tempfile().unwrap();
fs::copy("assets/a.mp3", &tmp).unwrap();

let tag = Tag::default()
.with_tag_type(audiotags::TagType::Id3v2)
.read_from_path(tmp.path())
.expect("should use explicit tag type, not file extension");

assert_eq!(tag.date().unwrap().year, 2013);
}