mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-12-12 13:42:34 +00:00
Separate date and year methods
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
parent
10ca5c0295
commit
edd315467f
5 changed files with 27 additions and 34 deletions
|
@ -102,16 +102,11 @@ impl AudioTagEdit for ApeTag {
|
|||
}
|
||||
|
||||
fn year(&self) -> Option<i32> {
|
||||
if let Some(Ok(y)) = self
|
||||
.get_value("Date")
|
||||
.map(|s| s.chars().take(4).collect::<String>().parse::<i32>())
|
||||
{
|
||||
Some(y)
|
||||
} else if let Some(Ok(y)) = self.get_value("Year").map(str::parse::<i32>) {
|
||||
Some(y)
|
||||
} else {
|
||||
None
|
||||
if let Some(Ok(y)) = self.get_value("Year").map(str::parse::<i32>) {
|
||||
return Some(y);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
fn set_year(&mut self, year: i32) {
|
||||
self.set_value("Year", year.to_string())
|
||||
|
|
|
@ -82,7 +82,7 @@ impl AudioTagEdit for Mp4Tag {
|
|||
self.inner.set_year(year.to_string())
|
||||
}
|
||||
fn remove_year(&mut self) {
|
||||
self.inner.remove_year();
|
||||
self.inner.remove_year()
|
||||
}
|
||||
|
||||
fn copyright(&self) -> Option<&str> {
|
||||
|
|
|
@ -205,11 +205,9 @@ impl AudioTagEdit for OggTag {
|
|||
.get_value("DATE")
|
||||
.map(std::string::ToString::to_string)
|
||||
}
|
||||
|
||||
fn set_date(&mut self, date: &str) {
|
||||
self.inner.set_value("DATE", date)
|
||||
}
|
||||
|
||||
fn remove_date(&mut self) {
|
||||
self.inner.remove_key("DATE")
|
||||
}
|
||||
|
@ -217,12 +215,6 @@ impl AudioTagEdit for OggTag {
|
|||
fn year(&self) -> Option<i32> {
|
||||
if let Some(Ok(y)) = self.inner.get_value("YEAR").map(str::parse::<i32>) {
|
||||
return Some(y);
|
||||
} else if let Some(Ok(y)) = self
|
||||
.inner
|
||||
.get_value("DATE")
|
||||
.map(|d| (&d[..4]).parse::<i32>())
|
||||
{
|
||||
return Some(y);
|
||||
}
|
||||
|
||||
None
|
||||
|
|
|
@ -5,7 +5,7 @@ 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};
|
||||
use lofty_attr::{i32_accessor, str_accessor, u16_accessor, u32_accessor};
|
||||
|
||||
/// Combination of [`AudioTagEdit`], [`AudioTagWrite`], and [`ToAnyTag`]
|
||||
pub trait AudioTag: AudioTagEdit + AudioTagWrite + ToAnyTag {}
|
||||
|
@ -22,22 +22,16 @@ pub trait AudioTagEdit {
|
|||
self.artist().map(|a| a.split(delimiter).collect())
|
||||
}
|
||||
|
||||
/// Returns the track date
|
||||
i32_accessor!(year);
|
||||
|
||||
/// Returns the date
|
||||
fn date(&self) -> Option<String> {
|
||||
self.year().map(|y| y.to_string())
|
||||
}
|
||||
/// Sets the track date
|
||||
fn set_date(&mut self, date: &str) {
|
||||
if let Ok(d) = date.parse::<i32>() {
|
||||
self.set_year(d)
|
||||
}
|
||||
}
|
||||
/// Removes the track date
|
||||
fn remove_date(&mut self) {
|
||||
self.remove_year()
|
||||
}
|
||||
|
||||
i32_accessor!(year);
|
||||
/// Sets the date
|
||||
fn set_date(&mut self, _date: &str) {}
|
||||
/// Removes the date
|
||||
fn remove_date(&mut self) {}
|
||||
|
||||
str_accessor!(copyright);
|
||||
str_accessor!(genre);
|
||||
|
@ -59,8 +53,7 @@ pub trait AudioTagEdit {
|
|||
|
||||
/// Splits the artist string into a `Vec`
|
||||
fn album_artists(&self, delimiter: &str) -> Option<Vec<&str>> {
|
||||
self.album_artist()
|
||||
.map(|a| a.split(delimiter).collect())
|
||||
self.album_artist().map(|a| a.split(delimiter).collect())
|
||||
}
|
||||
|
||||
/// Returns the front and back album covers
|
||||
|
|
13
tests/io.rs
13
tests/io.rs
|
@ -33,6 +33,9 @@ macro_rules! add_tags {
|
|||
println!("Setting year");
|
||||
tag.set_year(2020);
|
||||
|
||||
println!("Setting date");
|
||||
tag.set_date("20200415");
|
||||
|
||||
println!("Setting copyright");
|
||||
tag.set_copyright("1988");
|
||||
|
||||
|
@ -120,6 +123,11 @@ macro_rules! verify_write {
|
|||
assert_eq!(tag.year(), Some(2020));
|
||||
}
|
||||
|
||||
if file_name != stringify!("tests/assets/a.m4a") {
|
||||
println!("Verifying date");
|
||||
assert_eq!(tag.date(), Some("20200415".to_string()));
|
||||
}
|
||||
|
||||
println!("Verifying copyright");
|
||||
assert_eq!(tag.copyright(), Some("1988"));
|
||||
|
||||
|
@ -208,6 +216,11 @@ macro_rules! remove_tags {
|
|||
assert!(tag.year().is_none());
|
||||
tag.remove_year();
|
||||
|
||||
println!("Removing date");
|
||||
tag.remove_date();
|
||||
assert!(tag.date().is_none());
|
||||
tag.remove_date();
|
||||
|
||||
println!("Removing copyright");
|
||||
tag.remove_copyright();
|
||||
assert!(tag.copyright().is_none());
|
||||
|
|
Loading…
Reference in a new issue