mirror of
https://codeberg.org/unspeaker/perch.git
synced 2025-12-06 09:36:42 +01:00
use macro to define field setters
This commit is contained in:
parent
50dea84174
commit
5a5b312955
1 changed files with 35 additions and 64 deletions
|
|
@ -159,72 +159,43 @@ impl Metadata {
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn set_artist (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
}
|
||||||
match self {
|
|
||||||
Metadata::Music { artist, .. } => {
|
macro_rules! metadata_set_string {
|
||||||
if artist.as_deref() != Some(value.as_ref()) {
|
($name:ident, $field:ident, $key:expr) => {
|
||||||
*artist = Some(value.as_ref().into());
|
impl Metadata {
|
||||||
Some(TagItem::new(
|
pub fn $name (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
||||||
ItemKey::TrackArtist,
|
if let Metadata::Music { $field, .. } = self
|
||||||
ItemValue::Text(value.as_ref().into())
|
&& Some(value.as_ref()) != $field.as_deref()
|
||||||
));
|
{
|
||||||
|
*$field = Some(value.as_ref().into());
|
||||||
|
return Some(TagItem::new($key, ItemValue::Text(value.as_ref().into())))
|
||||||
}
|
}
|
||||||
//todo!("emit task");
|
|
||||||
None
|
None
|
||||||
},
|
}
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_year (&mut self, value: &impl AsRef<str> ) -> Option<TagItem> {
|
|
||||||
match self {
|
|
||||||
Metadata::Music { year, .. } => {
|
|
||||||
if let Ok(value) = value.as_ref().trim().parse::<u32>() {
|
|
||||||
*year = Some(value);
|
|
||||||
}
|
|
||||||
Some(TagItem::new(
|
|
||||||
ItemKey::Year,
|
|
||||||
ItemValue::Text(value.as_ref().into())
|
|
||||||
))
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_album (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
|
||||||
match self {
|
|
||||||
Metadata::Music { album, .. } => {
|
|
||||||
*album = Some(value.as_ref().into());
|
|
||||||
Some(TagItem::new(
|
|
||||||
ItemKey::AlbumTitle,
|
|
||||||
ItemValue::Text(value.as_ref().into())
|
|
||||||
))
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_title (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
|
||||||
match self {
|
|
||||||
Metadata::Music { title, .. } => {
|
|
||||||
*title = Some(value.as_ref().into());
|
|
||||||
Some(TagItem::new(
|
|
||||||
ItemKey::TrackTitle,
|
|
||||||
ItemValue::Text(value.as_ref().into())
|
|
||||||
))
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_track (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
|
||||||
match self {
|
|
||||||
Metadata::Music { track, .. } => {
|
|
||||||
if let Ok(value) = value.as_ref().trim().parse::<u32>() {
|
|
||||||
*track = Some(value);
|
|
||||||
}
|
|
||||||
Some(TagItem::new(
|
|
||||||
ItemKey::TrackNumber,
|
|
||||||
ItemValue::Text(value.as_ref().into())
|
|
||||||
))
|
|
||||||
},
|
|
||||||
_ => None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! metadata_set_number {
|
||||||
|
($name:ident, $field:ident, $key:expr) => {
|
||||||
|
impl Metadata {
|
||||||
|
pub fn $name (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
||||||
|
if let Metadata::Music { $field, .. } = self
|
||||||
|
&& let Ok(value) = value.as_ref().trim().parse::<u32>()
|
||||||
|
&& Some(value) != *$field
|
||||||
|
{
|
||||||
|
*$field = Some(value);
|
||||||
|
return Some(TagItem::new($key, ItemValue::Text(format!("{value}"))))
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata_set_string!(set_artist, artist, ItemKey::TrackArtist);
|
||||||
|
metadata_set_number!(set_year, track, ItemKey::Year);
|
||||||
|
metadata_set_string!(set_album, album, ItemKey::AlbumTitle);
|
||||||
|
metadata_set_number!(set_track, track, ItemKey::TrackNumber);
|
||||||
|
metadata_set_string!(set_title, title, ItemKey::TrackTitle);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue