Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tag::date_raw to get date string before Timestamp conversion #45

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ readme-rustdocifier = "0.1.1"
[features]
default = ['from']
from = []
raw-date = []
15 changes: 12 additions & 3 deletions src/components/flac_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<'a> From<AnyTag<'a>> for FlacTag {
t.set_artist(&v)
}
if let Some(v) = inp.date {
t.set_date(v)
t.set_date(TimestampTag::Id3(v))
}
if let Some(v) = inp.year {
t.set_year(v)
Expand Down Expand Up @@ -117,8 +117,17 @@ impl AudioTagEdit for FlacTag {
None
}
}
fn set_date(&mut self, date: Timestamp) {
self.set_first("DATE", &date.to_string());
#[cfg(feature = "raw-date")]
fn date_raw(&self) -> Option<TimestampTag> {
self.get_first("DATE")
.map(|e| TimestampTag::Unknown(e.to_string()))
}
fn set_date(&mut self, date: TimestampTag) {
match date {
TimestampTag::Id3(date_fmt) => self.set_first("DATE", &date_fmt.to_string()),
#[cfg(feature = "raw-date")]
TimestampTag::Unknown(date_str) => self.set_first("DATE", &date_str.to_string()),
}
}
fn remove_date(&mut self) {
self.remove("DATE");
Expand Down
19 changes: 17 additions & 2 deletions src/components/id3_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,23 @@ impl AudioTagEdit for Id3v2Tag {
fn date(&self) -> Option<Timestamp> {
self.inner.date_recorded()
}
fn set_date(&mut self, timestamp: Timestamp) {
self.inner.set_date_recorded(timestamp)
#[cfg(feature = "raw-date")]
fn date_raw(&self) -> Option<TimestampTag> {
self.inner.get("TDRC").and_then(|frame| {
frame
.content()
.text()
.map(|e| TimestampTag::Unknown(e.to_string()))
})
}
fn set_date(&mut self, date: TimestampTag) {
match date {
TimestampTag::Id3(date_fmt) => self.inner.set_date_recorded(date_fmt),
#[cfg(feature = "raw-date")]
TimestampTag::Unknown(date_str) => {
self.inner.add_frame(Frame::text("TDRC", date_str));
}
}
}
fn remove_date(&mut self) {
self.inner.remove_date_recorded()
Expand Down
14 changes: 12 additions & 2 deletions src/components/mp4_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,18 @@ impl AudioTagEdit for Mp4Tag {
None
}
}
fn set_date(&mut self, date: Timestamp) {
self.inner.set_year(date.to_string())
#[cfg(feature = "raw-date")]
fn date_raw(&self) -> Option<TimestampTag> {
self.inner
.year()
.map(|e| TimestampTag::Unknown(e.to_string()))
}
fn set_date(&mut self, date: TimestampTag) {
match date {
TimestampTag::Id3(date_fmt) => self.inner.set_year(date_fmt.to_string()),
#[cfg(feature = "raw-date")]
TimestampTag::Unknown(date_str) => self.inner.set_year(date_str.to_string()),
}
}
fn remove_date(&mut self) {
self.inner.remove_year()
Expand Down
4 changes: 3 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub trait AudioTagEdit: AudioTagConfig {
}

fn date(&self) -> Option<Timestamp>;
fn set_date(&mut self, date: Timestamp);
#[cfg(feature = "raw-date")]
fn date_raw(&self) -> Option<TimestampTag>;
fn set_date(&mut self, date: TimestampTag);
fn remove_date(&mut self);

fn year(&self) -> Option<i32>;
Expand Down
15 changes: 15 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ pub enum MimeType {
Gif,
}

/// The types of date for the timestamp.
/// Most of the time you will want to use `TimestampTag::Id3` as it is using the
/// standard specifications. However when you are working with audio with
/// unknown date format or non-ISO then you can use `TimestampTag::Unknown` as
/// an escape hatch to get the original text and parse to your own format
/// if needed.
///
/// - To use TimestampTag you need to enable the `raw-date` feature
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TimestampTag {
Id3(id3::Timestamp),
#[cfg(feature = "raw-date")]
Unknown(String),
}

impl TryFrom<&str> for MimeType {
type Error = crate::Error;
fn try_from(inp: &str) -> crate::Result<Self> {
Expand Down
4 changes: 2 additions & 2 deletions tests/io.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use audiotags::{MimeType, Picture, Tag};
use audiotags::{MimeType, Picture, Tag, TimestampTag};
use id3::Timestamp;
use std::ffi::OsString;
use std::fs;
Expand Down Expand Up @@ -30,7 +30,7 @@ macro_rules! test_file {
assert!(tags.artist().is_none());
tags.remove_artist();

tags.set_date(Timestamp::from_str("2020-05-22").unwrap());
tags.set_date(TimestampTag::Id3(Timestamp::from_str("2020-05-22").unwrap()));
assert_eq!(
tags.date(),
Some(Timestamp::from_str("2020-05-22").unwrap())
Expand Down