mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-10 06:34:18 +00:00
Create AudioTagEdit methods with macros, remove useless methods, rename unclear methods
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
parent
18f6789a5a
commit
10ca5c0295
12 changed files with 54 additions and 175 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -332,9 +332,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lofty_attr"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f55b7f910d0137565a5eed225d3a89efb4512b2986eebe264235bf2e890e13db"
|
||||
checksum = "33676f2eaa85ffe8ee898ca1e5f0e318686db1a16df9738ca83aaefca98d37fa"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn",
|
||||
|
|
|
@ -28,7 +28,7 @@ base64 = "0.13.0"
|
|||
byteorder = "1.4.3"
|
||||
cfg-if = "1.0.0"
|
||||
|
||||
lofty_attr = "0.1.5"
|
||||
lofty_attr = "0.1.6"
|
||||
|
||||
[features]
|
||||
default = ["all_tags"]
|
||||
|
|
|
@ -41,23 +41,19 @@ impl AudioTagEdit for AiffTag {
|
|||
fn title(&self) -> Option<&str> {
|
||||
self.inner.name_id.as_deref()
|
||||
}
|
||||
|
||||
fn set_title(&mut self, title: &str) {
|
||||
self.inner.name_id = Some(title.to_string())
|
||||
}
|
||||
|
||||
fn remove_title(&mut self) {
|
||||
self.inner.name_id = None
|
||||
}
|
||||
|
||||
fn artist_str(&self) -> Option<&str> {
|
||||
fn artist(&self) -> Option<&str> {
|
||||
self.inner.author_id.as_deref()
|
||||
}
|
||||
|
||||
fn set_artist(&mut self, artist: &str) {
|
||||
self.inner.author_id = Some(artist.to_string())
|
||||
}
|
||||
|
||||
fn remove_artist(&mut self) {
|
||||
self.inner.author_id = None
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ impl AudioTagEdit for ApeTag {
|
|||
self.remove_key("Title")
|
||||
}
|
||||
|
||||
fn artist_str(&self) -> Option<&str> {
|
||||
fn artist(&self) -> Option<&str> {
|
||||
self.get_value("Artist")
|
||||
}
|
||||
|
||||
|
@ -167,15 +167,15 @@ impl AudioTagEdit for ApeTag {
|
|||
fn album_title(&self) -> Option<&str> {
|
||||
self.get_value("Album")
|
||||
}
|
||||
fn set_album_title(&mut self, title: &str) {
|
||||
self.set_value("Album", title)
|
||||
fn set_album_title(&mut self, album_title: &str) {
|
||||
self.set_value("Album", album_title)
|
||||
}
|
||||
fn remove_album_title(&mut self) {
|
||||
self.remove_key("Album")
|
||||
}
|
||||
|
||||
// Album artists aren't standard?
|
||||
fn album_artist_str(&self) -> Option<&str> {
|
||||
fn album_artist(&self) -> Option<&str> {
|
||||
self.get_value("Album artist")
|
||||
}
|
||||
|
||||
|
@ -183,7 +183,7 @@ impl AudioTagEdit for ApeTag {
|
|||
self.set_value("Album artist", artists)
|
||||
}
|
||||
|
||||
fn remove_album_artists(&mut self) {
|
||||
fn remove_album_artist(&mut self) {
|
||||
self.remove_key("Album artist")
|
||||
}
|
||||
|
||||
|
|
|
@ -91,7 +91,7 @@ impl AudioTagEdit for Id3v2Tag {
|
|||
self.inner.remove_title();
|
||||
}
|
||||
|
||||
fn artist_str(&self) -> Option<&str> {
|
||||
fn artist(&self) -> Option<&str> {
|
||||
self.inner.artist()
|
||||
}
|
||||
|
||||
|
@ -192,15 +192,13 @@ impl AudioTagEdit for Id3v2Tag {
|
|||
self.inner.remove_album();
|
||||
}
|
||||
|
||||
fn album_artist_str(&self) -> Option<&str> {
|
||||
fn album_artist(&self) -> Option<&str> {
|
||||
self.inner.album_artist()
|
||||
}
|
||||
|
||||
fn set_album_artist(&mut self, artists: &str) {
|
||||
self.inner.set_album_artist(artists)
|
||||
fn set_album_artist(&mut self, album_artist: &str) {
|
||||
self.inner.set_album_artist(album_artist)
|
||||
}
|
||||
|
||||
fn remove_album_artists(&mut self) {
|
||||
fn remove_album_artist(&mut self) {
|
||||
self.inner.remove_album_artist()
|
||||
}
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ impl AudioTagEdit for Mp4Tag {
|
|||
self.inner.remove_title();
|
||||
}
|
||||
|
||||
fn artist_str(&self) -> Option<&str> {
|
||||
fn artist(&self) -> Option<&str> {
|
||||
self.inner.artist()
|
||||
}
|
||||
fn set_artist(&mut self, artist: &str) {
|
||||
|
@ -135,13 +135,13 @@ impl AudioTagEdit for Mp4Tag {
|
|||
self.inner.remove_album();
|
||||
}
|
||||
|
||||
fn album_artist_str(&self) -> Option<&str> {
|
||||
fn album_artist(&self) -> Option<&str> {
|
||||
self.inner.album_artist()
|
||||
}
|
||||
fn set_album_artist(&mut self, artists: &str) {
|
||||
self.inner.set_album_artist(artists)
|
||||
fn set_album_artist(&mut self, album_artist: &str) {
|
||||
self.inner.set_album_artist(album_artist)
|
||||
}
|
||||
fn remove_album_artists(&mut self) {
|
||||
fn remove_album_artist(&mut self) {
|
||||
self.inner.remove_album_artists();
|
||||
}
|
||||
|
||||
|
@ -226,9 +226,6 @@ impl AudioTagEdit for Mp4Tag {
|
|||
}
|
||||
}
|
||||
|
||||
fn remove_track(&mut self) {
|
||||
self.inner.remove_track(); // faster than removing separately
|
||||
}
|
||||
fn track_number(&self) -> Option<u32> {
|
||||
self.inner.track_number().map(u32::from)
|
||||
}
|
||||
|
|
|
@ -186,18 +186,16 @@ impl AudioTagEdit for OggTag {
|
|||
fn set_title(&mut self, title: &str) {
|
||||
self.inner.set_value("TITLE", title);
|
||||
}
|
||||
|
||||
fn remove_title(&mut self) {
|
||||
self.inner.remove_key("TITLE");
|
||||
}
|
||||
fn artist_str(&self) -> Option<&str> {
|
||||
|
||||
fn artist(&self) -> Option<&str> {
|
||||
self.inner.get_value("ARTIST")
|
||||
}
|
||||
|
||||
fn set_artist(&mut self, artist: &str) {
|
||||
self.inner.set_value("ARTIST", artist)
|
||||
}
|
||||
|
||||
fn remove_artist(&mut self) {
|
||||
self.inner.remove_key("ARTIST");
|
||||
}
|
||||
|
@ -290,15 +288,13 @@ impl AudioTagEdit for OggTag {
|
|||
self.inner.remove_key("ALBUM");
|
||||
}
|
||||
|
||||
fn album_artist_str(&self) -> Option<&str> {
|
||||
fn album_artist(&self) -> Option<&str> {
|
||||
self.inner.get_value("ALBUMARTIST")
|
||||
}
|
||||
|
||||
fn set_album_artist(&mut self, artist: &str) {
|
||||
self.inner.set_value("ALBUMARTIST", artist)
|
||||
fn set_album_artist(&mut self, album_artist: &str) {
|
||||
self.inner.set_value("ALBUMARTIST", album_artist)
|
||||
}
|
||||
|
||||
fn remove_album_artists(&mut self) {
|
||||
fn remove_album_artist(&mut self) {
|
||||
self.inner.remove_key("ALBUMARTIST");
|
||||
}
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ impl AudioTagEdit for RiffTag {
|
|||
self.remove_key("INAM")
|
||||
}
|
||||
|
||||
fn artist_str(&self) -> Option<&str> {
|
||||
fn artist(&self) -> Option<&str> {
|
||||
self.get_value("IART")
|
||||
}
|
||||
fn set_artist(&mut self, artist: &str) {
|
||||
|
@ -129,11 +129,9 @@ impl AudioTagEdit for RiffTag {
|
|||
|
||||
None
|
||||
}
|
||||
|
||||
fn set_track_number(&mut self, track_number: u32) {
|
||||
self.set_value("ITRK", track_number.to_string())
|
||||
}
|
||||
|
||||
fn remove_track_number(&mut self) {
|
||||
self.remove_key("ITRK")
|
||||
}
|
||||
|
@ -145,11 +143,9 @@ impl AudioTagEdit for RiffTag {
|
|||
|
||||
None
|
||||
}
|
||||
|
||||
fn set_total_tracks(&mut self, total_track: u32) {
|
||||
self.set_value("IFRM", total_track.to_string())
|
||||
}
|
||||
|
||||
fn remove_total_tracks(&mut self) {
|
||||
self.remove_key("IFRM")
|
||||
}
|
||||
|
@ -161,11 +157,9 @@ impl AudioTagEdit for RiffTag {
|
|||
|
||||
None
|
||||
}
|
||||
|
||||
fn set_disc_number(&mut self, disc_number: u32) {
|
||||
self.set_value("DISC", disc_number.to_string())
|
||||
}
|
||||
|
||||
fn remove_disc_number(&mut self) {
|
||||
self.remove_key("DISC")
|
||||
}
|
||||
|
@ -173,11 +167,9 @@ impl AudioTagEdit for RiffTag {
|
|||
fn total_discs(&self) -> Option<u32> {
|
||||
self.disc_number()
|
||||
}
|
||||
|
||||
fn set_total_discs(&mut self, total_discs: u32) {
|
||||
self.set_disc_number(total_discs)
|
||||
}
|
||||
|
||||
fn remove_total_discs(&mut self) {
|
||||
self.remove_disc_number()
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
//! let mut tag_sig = Tag::new().read_from_path_signature("tests/assets/a.wav").unwrap();
|
||||
//! tag_sig.set_artist("Foo artist");
|
||||
//!
|
||||
//! assert_eq!(tag_sig.artist_str(), Some("Foo artist"));
|
||||
//! assert_eq!(tag_sig.artist(), Some("Foo artist"));
|
||||
//! ```
|
||||
//!
|
||||
//! ## Specifying a TagType
|
||||
|
@ -75,7 +75,7 @@
|
|||
//! let oggtag: OggTag = anytag.into();
|
||||
//!
|
||||
//! assert_eq!(oggtag.title(), Some("Foo title"));
|
||||
//! assert_eq!(oggtag.artist_str(), Some("Foo artist"));
|
||||
//! assert_eq!(oggtag.artist(), Some("Foo artist"));
|
||||
//! ```
|
||||
//!
|
||||
//! # Concrete types
|
||||
|
|
136
src/traits.rs
136
src/traits.rs
|
@ -5,6 +5,8 @@ use crate::{Album, AnyTag, Picture, Result, TagType};
|
|||
use std::borrow::Cow;
|
||||
use std::fs::{File, OpenOptions};
|
||||
|
||||
use lofty_attr::{str_accessor, u32_accessor, i32_accessor, u16_accessor};
|
||||
|
||||
/// Combination of [`AudioTagEdit`], [`AudioTagWrite`], and [`ToAnyTag`]
|
||||
pub trait AudioTag: AudioTagEdit + AudioTagWrite + ToAnyTag {}
|
||||
|
||||
|
@ -12,24 +14,13 @@ pub trait AudioTag: AudioTagEdit + AudioTagWrite + ToAnyTag {}
|
|||
///
|
||||
/// Constructor methods e.g. `from_file` should be implemented separately.
|
||||
pub trait AudioTagEdit {
|
||||
/// Returns the track title
|
||||
fn title(&self) -> Option<&str>;
|
||||
/// Sets the track title
|
||||
fn set_title(&mut self, title: &str);
|
||||
/// Removes the track title
|
||||
fn remove_title(&mut self);
|
||||
|
||||
/// Returns the artist(s) as a string
|
||||
fn artist_str(&self) -> Option<&str>;
|
||||
/// Sets the artist string
|
||||
fn set_artist(&mut self, artist: &str);
|
||||
str_accessor!(title);
|
||||
str_accessor!(artist);
|
||||
|
||||
/// Splits the artist string into a `Vec`
|
||||
fn artists(&self, delimiter: &str) -> Option<Vec<&str>> {
|
||||
self.artist_str().map(|a| a.split(delimiter).collect())
|
||||
self.artist().map(|a| a.split(delimiter).collect())
|
||||
}
|
||||
/// Removes the artist string
|
||||
fn remove_artist(&mut self);
|
||||
|
||||
/// Returns the track date
|
||||
fn date(&self) -> Option<String> {
|
||||
|
@ -46,82 +37,31 @@ pub trait AudioTagEdit {
|
|||
self.remove_year()
|
||||
}
|
||||
|
||||
/// Returns the track year
|
||||
fn year(&self) -> Option<i32> {
|
||||
None
|
||||
}
|
||||
/// Sets the track year
|
||||
fn set_year(&mut self, _year: i32) {}
|
||||
/// Removes the track year
|
||||
fn remove_year(&mut self) {}
|
||||
i32_accessor!(year);
|
||||
|
||||
/// Returns the copyright
|
||||
fn copyright(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
/// Sets the copyright
|
||||
fn set_copyright(&mut self, _copyright: &str) {}
|
||||
/// Removes the copyright
|
||||
fn remove_copyright(&mut self) {}
|
||||
str_accessor!(copyright);
|
||||
str_accessor!(genre);
|
||||
str_accessor!(lyrics);
|
||||
|
||||
/// Returns the genre
|
||||
fn genre(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
/// Sets the genre
|
||||
fn set_genre(&mut self, _genre: &str) {}
|
||||
/// Removes the genre
|
||||
fn remove_genre(&mut self) {}
|
||||
|
||||
/// Returns the lyrics
|
||||
fn lyrics(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
/// Sets the lyrics
|
||||
fn set_lyrics(&mut self, _lyrics: &str) {}
|
||||
/// Removes the lyrics
|
||||
fn remove_lyrics(&mut self) {}
|
||||
|
||||
/// Returns the lyrics
|
||||
fn bpm(&self) -> Option<u16> {
|
||||
None
|
||||
}
|
||||
/// Sets the lyrics
|
||||
fn set_bpm(&mut self, _bpm: u16) {}
|
||||
/// Removes the lyrics
|
||||
fn remove_bpm(&mut self) {}
|
||||
u16_accessor!(bpm);
|
||||
|
||||
/// Returns the track's [`Album`]
|
||||
fn album(&self) -> Album<'_> {
|
||||
Album {
|
||||
title: self.album_title(),
|
||||
artist: self.album_artist_str(),
|
||||
artist: self.album_artist(),
|
||||
covers: self.album_covers(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the album title
|
||||
fn album_title(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
/// Sets the album title
|
||||
fn set_album_title(&mut self, _title: &str) {}
|
||||
/// Removes the album title
|
||||
fn remove_album_title(&mut self) {}
|
||||
str_accessor!(album_title);
|
||||
str_accessor!(album_artist);
|
||||
|
||||
/// Returns the album artist string
|
||||
fn album_artist_str(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
/// Splits the artist string into a `Vec`
|
||||
fn album_artists(&self, delimiter: &str) -> Option<Vec<&str>> {
|
||||
self.album_artist_str()
|
||||
self.album_artist()
|
||||
.map(|a| a.split(delimiter).collect())
|
||||
}
|
||||
/// Sets the album artist string
|
||||
fn set_album_artist(&mut self, _artist: &str) {}
|
||||
/// Removes the album artist string
|
||||
fn remove_album_artists(&mut self) {}
|
||||
|
||||
/// Returns the front and back album covers
|
||||
fn album_covers(&self) -> (Option<Picture>, Option<Picture>) {
|
||||
|
@ -160,34 +100,9 @@ pub trait AudioTagEdit {
|
|||
fn track(&self) -> (Option<u32>, Option<u32>) {
|
||||
(self.track_number(), self.total_tracks())
|
||||
}
|
||||
/// Sets the track number and total tracks
|
||||
fn set_track(&mut self, track_number: u32, total_tracks: u32) {
|
||||
self.set_track_number(track_number);
|
||||
self.set_total_tracks(total_tracks);
|
||||
}
|
||||
/// Removes the track number and total tracks
|
||||
fn remove_track(&mut self) {
|
||||
self.remove_track_number();
|
||||
self.remove_total_tracks();
|
||||
}
|
||||
|
||||
/// Returns the track number
|
||||
fn track_number(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
/// Sets the track number
|
||||
fn set_track_number(&mut self, _track_number: u32) {}
|
||||
/// Removes the track number
|
||||
fn remove_track_number(&mut self) {}
|
||||
|
||||
/// Returns the total tracks
|
||||
fn total_tracks(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
/// Sets the total tracks
|
||||
fn set_total_tracks(&mut self, _total_track: u32) {}
|
||||
/// Removes the total tracks
|
||||
fn remove_total_tracks(&mut self) {}
|
||||
u32_accessor!(track_number);
|
||||
u32_accessor!(total_tracks);
|
||||
|
||||
/// Returns the disc number and total discs
|
||||
fn disc(&self) -> (Option<u32>, Option<u32>) {
|
||||
|
@ -199,23 +114,8 @@ pub trait AudioTagEdit {
|
|||
self.remove_total_discs();
|
||||
}
|
||||
|
||||
/// Returns the disc number
|
||||
fn disc_number(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
/// Sets the disc number
|
||||
fn set_disc_number(&mut self, _disc_number: u32) {}
|
||||
/// Removes the disc number
|
||||
fn remove_disc_number(&mut self) {}
|
||||
|
||||
/// Returns the total discs
|
||||
fn total_discs(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
/// Sets the total discs
|
||||
fn set_total_discs(&mut self, _total_discs: u32) {}
|
||||
/// Removes the total discs
|
||||
fn remove_total_discs(&mut self) {}
|
||||
u32_accessor!(disc_number);
|
||||
u32_accessor!(total_discs);
|
||||
}
|
||||
|
||||
/// Functions for writing to a file
|
||||
|
|
|
@ -5,7 +5,7 @@ use lofty::{Id3Format, OggFormat, Tag, TagType};
|
|||
macro_rules! convert_tag {
|
||||
($tag: ident) => {
|
||||
assert_eq!($tag.title(), Some("Title Updated"));
|
||||
assert_eq!($tag.artist_str(), Some("Artist Updated"));
|
||||
assert_eq!($tag.artist(), Some("Artist Updated"));
|
||||
assert_eq!($tag.track_number(), Some(5));
|
||||
};
|
||||
}
|
||||
|
|
14
tests/io.rs
14
tests/io.rs
|
@ -112,7 +112,7 @@ macro_rules! verify_write {
|
|||
assert_eq!(tag.title(), Some("foo title"));
|
||||
|
||||
println!("Verifying artist");
|
||||
assert_eq!(tag.artist_str(), Some("foo artist"));
|
||||
assert_eq!(tag.artist(), Some("foo artist"));
|
||||
|
||||
// Skip this since RIFF INFO doesn't support year
|
||||
if file_name != stringify!("tests/assets/a.wav") {
|
||||
|
@ -175,7 +175,7 @@ macro_rules! verify_write {
|
|||
assert_eq!(tag.bpm(), Some(50));
|
||||
|
||||
println!("Verifying album artist");
|
||||
assert_eq!(tag.album_artist_str(), Some("foo album artist"));
|
||||
assert_eq!(tag.album_artist(), Some("foo album artist"));
|
||||
|
||||
println!("Verifying album covers");
|
||||
|
||||
|
@ -200,7 +200,7 @@ macro_rules! remove_tags {
|
|||
|
||||
println!("Removing artist");
|
||||
tag.remove_artist();
|
||||
assert!(tag.artist_str().is_none());
|
||||
assert!(tag.artist().is_none());
|
||||
tag.remove_artist();
|
||||
|
||||
println!("Removing year");
|
||||
|
@ -234,9 +234,9 @@ macro_rules! remove_tags {
|
|||
tag.remove_album_title();
|
||||
|
||||
println!("Removing album artists");
|
||||
tag.remove_album_artists();
|
||||
assert!(tag.album_artist_str().is_none());
|
||||
tag.remove_album_artists();
|
||||
tag.remove_album_artist();
|
||||
assert!(tag.album_artist().is_none());
|
||||
tag.remove_album_artist();
|
||||
|
||||
println!("Removing album covers");
|
||||
tag.remove_album_covers();
|
||||
|
@ -292,7 +292,7 @@ fn test_aiff_text() {
|
|||
println!("Verifying title");
|
||||
assert_eq!(tag.title(), Some("foo title"));
|
||||
println!("Verifying artist");
|
||||
assert_eq!(tag.artist_str(), Some("foo artist"));
|
||||
assert_eq!(tag.artist(), Some("foo artist"));
|
||||
println!("Verifying copyright");
|
||||
assert_eq!(tag.copyright(), Some("1988"));
|
||||
|
||||
|
|
Loading…
Reference in a new issue