mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-14 16:37:12 +00:00
Accessor: Improve accessor_trait!
macro for future expansion
This commit is contained in:
parent
eddd906ac2
commit
ced003dc0a
1 changed files with 68 additions and 45 deletions
113
src/traits.rs
113
src/traits.rs
|
@ -4,10 +4,13 @@
|
|||
// Usage:
|
||||
//
|
||||
// accessor_trait! {
|
||||
// field_name<type>
|
||||
// [field_name]<type>
|
||||
// }
|
||||
//
|
||||
// Where `type` is the return type for `Accessor::field_name`. By default, this type will also be used
|
||||
// * `field_name` is the name of the method to access the field. If a name consists of multiple segments,
|
||||
// such as `track_number`, they should be separated by spaces like so: [track number]<type>.
|
||||
//
|
||||
// * `type` is the return type for `Accessor::field_name`. By default, this type will also be used
|
||||
// in the setter.
|
||||
//
|
||||
// An owned type can also be specified for the setter:
|
||||
|
@ -16,82 +19,102 @@
|
|||
// field_name<type, owned_type>
|
||||
// }
|
||||
macro_rules! accessor_trait {
|
||||
($($name:ident < $($ty:ty),+ >),+ $(,)?) => {
|
||||
($([$($name:tt)+] < $($ty:ty),+ >),+ $(,)?) => {
|
||||
/// Provides accessors for common items
|
||||
///
|
||||
/// This attempts to only provide methods for items that all tags have in common,
|
||||
/// but there may be exceptions.
|
||||
pub trait Accessor {
|
||||
paste::paste! {
|
||||
$(
|
||||
accessor_trait! { @GETTER $name $($ty),+ }
|
||||
$(
|
||||
accessor_trait! { @GETTER [$($name)+] $($ty),+ }
|
||||
|
||||
accessor_trait! { @SETTER $name $($ty),+ }
|
||||
accessor_trait! { @SETTER [$($name)+] $($ty),+ }
|
||||
|
||||
accessor_trait! { @REMOVE $name $($ty),+ }
|
||||
)+
|
||||
}
|
||||
accessor_trait! { @REMOVE [$($name)+] $($ty),+ }
|
||||
)+
|
||||
}
|
||||
};
|
||||
(@GETTER $name:ident $ty:ty $(, $_ty:tt)?) => {
|
||||
(@GETTER [$($name:tt)+] $ty:ty $(, $_ty:tt)?) => {
|
||||
accessor_trait! { @GET_METHOD [$($name)+] Option<$ty> }
|
||||
};
|
||||
(@SETTER [$($name:tt)+] $_ty:ty, $owned_ty:tt) => {
|
||||
accessor_trait! { @SETTER [$($name)+] $owned_ty }
|
||||
};
|
||||
(@SETTER [$($name:tt)+] $ty:ty) => {
|
||||
accessor_trait! { @SET_METHOD [$($name)+] $ty }
|
||||
};
|
||||
(@REMOVE [$($name:tt)+] $_ty:ty, $owned_ty:tt) => {
|
||||
accessor_trait! { @REMOVE [$($name)+] $owned_ty }
|
||||
};
|
||||
(@REMOVE [$($name:tt)+] $ty:ty) => {
|
||||
accessor_trait! { @REMOVE_METHOD [$($name)+], $ty }
|
||||
};
|
||||
(@GET_METHOD [$name:tt $($other:tt)*] Option<$ret_ty:ty>) => {
|
||||
paste::paste! {
|
||||
#[doc = "Returns the " $name]
|
||||
#[doc = "Returns the " $name $(" " $other)*]
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use lofty::{Tag, Accessor};
|
||||
///
|
||||
/// # let tag_type = lofty::TagType::ID3v2;
|
||||
/// let mut tag = Tag::new(tag_type);
|
||||
///
|
||||
#[doc = "assert_eq!(tag." $name $(_ $other)* "(), None);"]
|
||||
/// ```
|
||||
fn [<
|
||||
$name $(_ $other)*
|
||||
>] (&self) -> Option<$ret_ty> { None }
|
||||
}
|
||||
};
|
||||
(@SET_METHOD [$name:tt $($other:tt)*] $owned_ty:ty) => {
|
||||
paste::paste! {
|
||||
#[doc = "Sets the " $name $(" " $other)*]
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use lofty::{Tag, Accessor};
|
||||
///
|
||||
/// let mut tag = Tag::new(tag_type);
|
||||
#[doc = "tag.set_" $name $(_ $other)* "(value);"]
|
||||
///
|
||||
#[doc = "assert_eq!(tag." $name "(), None);"]
|
||||
#[doc = "assert_eq!(tag." $name $(_ $other)* "(), Some(value));"]
|
||||
/// ```
|
||||
fn $name(&self) -> Option<$ty> { None }
|
||||
fn [<
|
||||
set_ $name $(_ $other)*
|
||||
>] (&mut self , _value: $owned_ty) {}
|
||||
}
|
||||
};
|
||||
(@SETTER $name:ident $_ty:ty, $owned_ty:tt) => {
|
||||
accessor_trait! { @SETTER $name $owned_ty }
|
||||
};
|
||||
(@SETTER $name:ident $ty:ty $(, $_ty:tt)?) => {
|
||||
(@REMOVE_METHOD [$name:tt $($other:tt)*], $ty:ty) => {
|
||||
paste::paste! {
|
||||
#[doc = "Sets the " $name]
|
||||
#[doc = "Removes the " $name $(" " $other)*]
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use lofty::{Tag, Accessor};
|
||||
///
|
||||
#[doc = "let mut tag = Tag::new(tag_type);\ntag.set_" $name "(value);"]
|
||||
/// let mut tag = Tag::new(tag_type);
|
||||
#[doc = "tag.set_" $name $(_ $other)* "(value);"]
|
||||
///
|
||||
#[doc = "assert_eq!(tag." $name "(), Some(value));"]
|
||||
#[doc = "assert_eq!(tag." $name $(_ $other)* "(), Some(value));"]
|
||||
///
|
||||
#[doc = "tag.remove_" $name $(_ $other)* "();"]
|
||||
///
|
||||
#[doc = "assert_eq!(tag." $name $(_ $other)* "(), None);"]
|
||||
/// ```
|
||||
fn [<set_ $name>](&mut self, _value: $ty) {}
|
||||
}
|
||||
};
|
||||
(@REMOVE $name:ident $ty:ty $(, $_ty:tt)?) => {
|
||||
paste::paste! {
|
||||
#[doc = "Removes the " $name]
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use lofty::{Tag, Accessor};
|
||||
///
|
||||
#[doc = "let mut tag = Tag::new(tag_type);\ntag.set_" $name "(value);"]
|
||||
///
|
||||
#[doc = "assert_eq!(tag." $name "(), Some(value));"]
|
||||
///
|
||||
#[doc = "tag.remove_" $name "();"]
|
||||
///
|
||||
#[doc = "assert_eq!(tag." $name "(), None);"]
|
||||
/// ```
|
||||
fn [<remove_ $name>](&mut self) {}
|
||||
fn [<
|
||||
remove_ $name $(_ $other)*
|
||||
>] (&mut self) {}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
accessor_trait! {
|
||||
artist<&str, String>, title<&str, String>,
|
||||
album<&str, String>, genre<&str, String>,
|
||||
track<u32>
|
||||
[artist]<&str, String>, [title]<&str, String>,
|
||||
[album]<&str, String>, [genre]<&str, String>,
|
||||
[track]<u32>, [track total]<u32>,
|
||||
[disk]<u32>, [disk total]<u32>,
|
||||
[year]<u32>, [comment]<&str, String>,
|
||||
}
|
||||
|
||||
use crate::tag::Tag;
|
||||
|
|
Loading…
Reference in a new issue