This commit is contained in:
Tianyi Shi 2020-10-27 14:19:10 +00:00
parent 0dca646968
commit b955bb60c8
2 changed files with 46 additions and 20 deletions

View file

@ -1,3 +1,17 @@
#[macro_export]
macro_rules! impl_audiotag_config {
($tag:ident) => {
impl AudioTagConfig for $tag {
fn config(&self) -> &Config {
&self.config
}
fn set_config(&mut self, config: Config) {
self.config = config.clone();
}
}
};
}
#[macro_export] #[macro_export]
macro_rules! impl_tag { macro_rules! impl_tag {
($tag:ident , $inner:ident) => { ($tag:ident , $inner:ident) => {
@ -17,13 +31,9 @@ macro_rules! impl_tag {
}) })
} }
} }
impl AudioTagCommon for $tag { impl_audiotag_config!($tag);
fn config(&self) -> &Config {
&self.config impl IntoAnyTag for $tag {
}
fn set_config(&mut self, config: Config) {
self.config = config.clone();
}
fn into_anytag(&self) -> AnyTag<'_> { fn into_anytag(&self) -> AnyTag<'_> {
self.into() self.into()
} }

View file

@ -101,7 +101,7 @@ impl Tag {
} }
} }
pub fn read_from_path(&self, path: impl AsRef<Path>) -> crate::Result<Box<dyn AudioTagEdit>> { pub fn read_from_path(&self, path: impl AsRef<Path>) -> crate::Result<Box<dyn AudioTag>> {
match self.tag_type.unwrap_or(TagType::try_from_ext( match self.tag_type.unwrap_or(TagType::try_from_ext(
path.as_ref() path.as_ref()
.extension() .extension()
@ -231,32 +231,45 @@ pub struct AnyTag<'a> {
pub total_discs: Option<u16>, pub total_discs: Option<u16>,
} }
// impl<'a> From<&'a AnyTag> for AnyTag<'a> {
// fn from(inp: &'a AnyTag)
// }
impl AudioTagConfig for AnyTag<'_> {
fn config(&self) -> &Config {
&self.config
}
fn set_config(&mut self, config: Config) {
self.config = config.clone();
}
}
impl AnyTag<'_> { impl AnyTag<'_> {
pub fn title(&self) -> Option<&str> { fn title(&self) -> Option<&str> {
self.title.as_deref() self.title.as_deref()
} }
pub fn artists(&self) -> Option<&[&str]> { fn artists(&self) -> Option<&[&str]> {
self.artists.as_deref() self.artists.as_deref()
} }
pub fn year(&self) -> Option<i32> { fn year(&self) -> Option<i32> {
self.year self.year
} }
pub fn album_title(&self) -> Option<&str> { fn album_title(&self) -> Option<&str> {
self.album_title.as_deref() self.album_title.as_deref()
} }
pub fn album_artists(&self) -> Option<&[&str]> { fn album_artists(&self) -> Option<&[&str]> {
self.album_artists.as_deref() self.album_artists.as_deref()
} }
pub fn track_number(&self) -> Option<u16> { fn track_number(&self) -> Option<u16> {
self.track_number self.track_number
} }
pub fn total_tracks(&self) -> Option<u16> { fn total_tracks(&self) -> Option<u16> {
self.total_tracks self.total_tracks
} }
pub fn disc_number(&self) -> Option<u16> { fn disc_number(&self) -> Option<u16> {
self.track_number self.track_number
} }
pub fn total_discs(&self) -> Option<u16> { fn total_discs(&self) -> Option<u16> {
self.total_tracks self.total_tracks
} }
} }
@ -272,7 +285,7 @@ impl AnyTag<'_> {
} }
} }
pub trait AudioTag: AudioTagEdit + AudioTagWrite {} pub trait AudioTag: AudioTagEdit + AudioTagWrite + IntoAnyTag {}
// pub trait TagIo { // pub trait TagIo {
// fn read_from_path(path: &str) -> crate::Result<AnyTag>; // fn read_from_path(path: &str) -> crate::Result<AnyTag>;
@ -282,7 +295,7 @@ pub trait AudioTag: AudioTagEdit + AudioTagWrite {}
/// Implementors of this trait are able to read and write audio metadata. /// Implementors of this trait are able to read and write audio metadata.
/// ///
/// Constructor methods e.g. `from_file` should be implemented separately. /// Constructor methods e.g. `from_file` should be implemented separately.
pub trait AudioTagEdit: AudioTagCommon { pub trait AudioTagEdit: AudioTagConfig {
fn title(&self) -> Option<&str>; fn title(&self) -> Option<&str>;
fn set_title(&mut self, title: &str); fn set_title(&mut self, title: &str);
fn remove_title(&mut self); fn remove_title(&mut self);
@ -404,9 +417,12 @@ pub trait AudioTagWrite {
fn write_to_path(&mut self, path: &str) -> crate::Result<()>; fn write_to_path(&mut self, path: &str) -> crate::Result<()>;
} }
pub trait AudioTagCommon { pub trait AudioTagConfig {
fn config(&self) -> &Config; fn config(&self) -> &Config;
fn set_config(&mut self, config: Config); fn set_config(&mut self, config: Config);
}
pub trait IntoAnyTag {
fn into_anytag(&self) -> AnyTag<'_>; fn into_anytag(&self) -> AnyTag<'_>;
/// Convert the tag type, which can be lossy. /// Convert the tag type, which can be lossy.