From b955bb60c8bdb32c8c0eba32298cb887d430bcec Mon Sep 17 00:00:00 2001 From: Tianyi Shi Date: Tue, 27 Oct 2020 14:19:10 +0000 Subject: [PATCH] simplify --- audiotags-dev-macro/src/lib.rs | 24 +++++++++++++------ src/lib.rs | 42 +++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/audiotags-dev-macro/src/lib.rs b/audiotags-dev-macro/src/lib.rs index 63e74908..f2db04ac 100644 --- a/audiotags-dev-macro/src/lib.rs +++ b/audiotags-dev-macro/src/lib.rs @@ -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_rules! impl_tag { ($tag:ident , $inner:ident) => { @@ -17,13 +31,9 @@ macro_rules! impl_tag { }) } } - impl AudioTagCommon for $tag { - fn config(&self) -> &Config { - &self.config - } - fn set_config(&mut self, config: Config) { - self.config = config.clone(); - } + impl_audiotag_config!($tag); + + impl IntoAnyTag for $tag { fn into_anytag(&self) -> AnyTag<'_> { self.into() } diff --git a/src/lib.rs b/src/lib.rs index 2104f722..18ec2f84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ impl Tag { } } - pub fn read_from_path(&self, path: impl AsRef) -> crate::Result> { + pub fn read_from_path(&self, path: impl AsRef) -> crate::Result> { match self.tag_type.unwrap_or(TagType::try_from_ext( path.as_ref() .extension() @@ -231,32 +231,45 @@ pub struct AnyTag<'a> { pub total_discs: Option, } +// 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<'_> { - pub fn title(&self) -> Option<&str> { + fn title(&self) -> Option<&str> { self.title.as_deref() } - pub fn artists(&self) -> Option<&[&str]> { + fn artists(&self) -> Option<&[&str]> { self.artists.as_deref() } - pub fn year(&self) -> Option { + fn year(&self) -> Option { self.year } - pub fn album_title(&self) -> Option<&str> { + fn album_title(&self) -> Option<&str> { self.album_title.as_deref() } - pub fn album_artists(&self) -> Option<&[&str]> { + fn album_artists(&self) -> Option<&[&str]> { self.album_artists.as_deref() } - pub fn track_number(&self) -> Option { + fn track_number(&self) -> Option { self.track_number } - pub fn total_tracks(&self) -> Option { + fn total_tracks(&self) -> Option { self.total_tracks } - pub fn disc_number(&self) -> Option { + fn disc_number(&self) -> Option { self.track_number } - pub fn total_discs(&self) -> Option { + fn total_discs(&self) -> Option { self.total_tracks } } @@ -272,7 +285,7 @@ impl AnyTag<'_> { } } -pub trait AudioTag: AudioTagEdit + AudioTagWrite {} +pub trait AudioTag: AudioTagEdit + AudioTagWrite + IntoAnyTag {} // pub trait TagIo { // fn read_from_path(path: &str) -> crate::Result; @@ -282,7 +295,7 @@ pub trait AudioTag: AudioTagEdit + AudioTagWrite {} /// Implementors of this trait are able to read and write audio metadata. /// /// Constructor methods e.g. `from_file` should be implemented separately. -pub trait AudioTagEdit: AudioTagCommon { +pub trait AudioTagEdit: AudioTagConfig { fn title(&self) -> Option<&str>; fn set_title(&mut self, title: &str); fn remove_title(&mut self); @@ -404,9 +417,12 @@ pub trait AudioTagWrite { fn write_to_path(&mut self, path: &str) -> crate::Result<()>; } -pub trait AudioTagCommon { +pub trait AudioTagConfig { fn config(&self) -> &Config; fn set_config(&mut self, config: Config); +} + +pub trait IntoAnyTag { fn into_anytag(&self) -> AnyTag<'_>; /// Convert the tag type, which can be lossy.