auto impl getters/setters on Entry

This commit is contained in:
🪞👃🪞 2025-04-06 16:36:56 +03:00
parent 4619d0ea76
commit 44a2108585
3 changed files with 43 additions and 54 deletions

View file

@ -18,10 +18,7 @@ impl Taggart {
{
let value = (column.getter)(&self.entries[self.cursor]);
let value = format!("{}", value.unwrap_or_default());
self.mode = Some(Mode::Edit {
index: value.len(),
value: value.into(),
});
self.mode = Some(Mode::Edit { index: value.len(), value: value.into(), });
}
}
pub fn edit_cancel (&mut self) {

View file

@ -1,6 +1,5 @@
use crate::*;
use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
use lofty::tag::TagItem;
pub struct Entry {
/// How many levels deep is this from the working directory
@ -50,42 +49,6 @@ impl Entry {
pub fn is_image (&self) -> bool {
matches!(&*self.info.read().unwrap(), Metadata::Image { .. })
}
pub fn hash (&self) -> Option<Arc<str>> {
self.info.read().unwrap().hash()
}
pub fn size (&self) -> Option<Arc<str>> {
self.info.read().unwrap().size()
}
pub fn artist (&self) -> Option<Arc<str>> {
self.info.read().unwrap().artist()
}
pub fn year (&self) -> Option<Arc<str>> {
self.info.read().unwrap().year()
}
pub fn album (&self) -> Option<Arc<str>> {
self.info.read().unwrap().album()
}
pub fn title (&self) -> Option<Arc<str>> {
self.info.read().unwrap().title()
}
pub fn track (&self) -> Option<Arc<str>> {
self.info.read().unwrap().track()
}
pub fn set_artist (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_artist(value)
}
pub fn set_year (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_year(value)
}
pub fn set_album (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_album(value)
}
pub fn set_title (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_title(value)
}
pub fn set_track (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_track(value)
}
pub const ICON_DIRECTORY: &'static str = "";
pub const ICON_IMAGE: &'static str = "󰋩";
pub const ICON_MUSIC: &'static str = "";

View file

@ -104,26 +104,47 @@ impl Metadata {
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
})
}
pub fn hash (&self) -> Option<Arc<str>> {
match self {
Metadata::Image { hash, .. } => Some(hash.clone()),
Metadata::Music { hash, .. } => Some(hash.clone()),
Metadata::Unknown { hash, .. } => Some(hash.clone()),
_ => None
}
macro_rules! generated_field {
($get:ident = |$self:ident| $expr:expr) => {
impl Entry {
pub fn $get (&$self) -> Option<Arc<str>> {
$self.info.read().unwrap().$get()
}
}
}
pub fn size (&self) -> Option<Arc<str>> {
match self {
Metadata::Image { size, .. } => Some(size.clone()),
Metadata::Music { size, .. } => Some(size.clone()),
Metadata::Unknown { size, .. } => Some(size.clone()),
_ => None
impl Metadata {
pub fn $get (&$self) -> Option<Arc<str>> {
$expr
}
}
}
}
generated_field!(hash = |self|match self {
Metadata::Image { hash, .. } => Some(hash.clone()),
Metadata::Music { hash, .. } => Some(hash.clone()),
Metadata::Unknown { hash, .. } => Some(hash.clone()),
_ => None
});
generated_field!(size = |self|match self {
Metadata::Image { size, .. } => Some(size.clone()),
Metadata::Music { size, .. } => Some(size.clone()),
Metadata::Unknown { size, .. } => Some(size.clone()),
_ => None
});
macro_rules! music_tag_field {
(string: $get:ident $set:ident $del:ident $key:expr) => {
impl Entry {
pub fn $get (&self) -> Option<Arc<str>> {
self.info.read().unwrap().$get()
}
pub fn $set (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().$set(value)
}
}
impl Metadata {
pub fn $get (&self) -> Option<Arc<str>> {
if let Metadata::Music { original_tag, modified_tag, .. } = self {
@ -169,6 +190,14 @@ macro_rules! music_tag_field {
}
};
(number: $get:ident $set:ident $del:ident $key:expr) => {
impl Entry {
pub fn $get (&self) -> Option<Arc<str>> {
self.info.read().unwrap().$get()
}
pub fn $set (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().$set(value)
}
}
impl Metadata {
pub fn $get (&self) -> Option<Arc<str>> {
if let Metadata::Music { original_tag, modified_tag, .. } = self {