mirror of
https://codeberg.org/unspeaker/perch.git
synced 2025-12-06 09:36:42 +01:00
wip: write data in new_tag
This commit is contained in:
parent
c3091dab7f
commit
0bdca2da01
1 changed files with 70 additions and 84 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use crate::*;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
use std::borrow::Borrow;
|
||||
use byte_unit::{Byte, Unit::MB};
|
||||
use lofty::{file::TaggedFileExt, probe::Probe, tag::{Accessor, Tag, TagItem, ItemKey, ItemValue}};
|
||||
|
||||
|
|
@ -16,16 +17,7 @@ pub enum Metadata {
|
|||
hash: Arc<str>,
|
||||
size: Arc<str>,
|
||||
tag: Option<Arc<Tag>>,
|
||||
artist: Option<Arc<str>>,
|
||||
album: Option<Arc<str>>,
|
||||
track: Option<u32>,
|
||||
title: Option<Arc<str>>,
|
||||
date: Option<Arc<str>>,
|
||||
year: Option<u32>,
|
||||
people: Option<Vec<Arc<str>>>,
|
||||
publisher: Option<Arc<str>>,
|
||||
key: Option<Arc<str>>,
|
||||
bpm: Option<Arc<str>>,
|
||||
new_tag: Option<Arc<Tag>>,
|
||||
},
|
||||
Image {
|
||||
invalid: bool,
|
||||
|
|
@ -55,18 +47,9 @@ impl Metadata {
|
|||
Ok(Self::Music {
|
||||
hash,
|
||||
size: format!("{:#>8.2}", size).into(),
|
||||
artist: tag.map(|t|t.artist().map(|t|t.into())).flatten(),
|
||||
album: tag.map(|t|t.album().map(|t|t.into())).flatten(),
|
||||
track: tag.map(|t|t.track().map(|t|t.into())).flatten(),
|
||||
title: tag.map(|t|t.title().map(|t|t.into())).flatten(),
|
||||
year: tag.map(|t|t.year().map(|t|t.into())).flatten(),
|
||||
date: None,
|
||||
people: None,
|
||||
publisher: None,
|
||||
key: None,
|
||||
bpm: None,
|
||||
invalid: false,
|
||||
tag: tag.map(|t|t.clone().into()),
|
||||
new_tag: tag.map(|t|t.clone().into()),
|
||||
invalid: false,
|
||||
})
|
||||
} else {
|
||||
Self::new_fallback(path)
|
||||
|
|
@ -132,74 +115,77 @@ impl Metadata {
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! metadata_get_string {
|
||||
($name:ident) => {
|
||||
macro_rules! metadata_field {
|
||||
(string: $get:ident $set:ident $key:expr) => {
|
||||
impl Metadata {
|
||||
pub fn $name (&self) -> Option<Arc<str>> {
|
||||
match self {
|
||||
Metadata::Music { tag: Some(tag), .. } =>
|
||||
tag.$name().map(|t|t.into()),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! metadata_get_number {
|
||||
($name:ident) => {
|
||||
impl Metadata {
|
||||
pub fn $name (&self) -> Option<Arc<str>> {
|
||||
match self {
|
||||
Metadata::Music { tag: Some(tag), .. } =>
|
||||
tag.$name().map(|t|format!("{t}").into()),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
macro_rules! metadata_set_string {
|
||||
($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
|
||||
&& Some(value.as_ref()) != $field.as_deref()
|
||||
{
|
||||
*$field = Some(value.as_ref().into());
|
||||
return Some(TagItem::new($key, ItemValue::Text(value.as_ref().into())))
|
||||
pub fn $get (&self) -> Option<Arc<str>> {
|
||||
if let Metadata::Music { tag: Some(tag), .. } = self {
|
||||
return tag.$get().map(|t|t.into())
|
||||
}
|
||||
None
|
||||
}
|
||||
pub fn $set (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
||||
if let Metadata::Music { tag: Some(old_tag), new_tag, .. } = self {
|
||||
if old_tag.$get().is_none() {
|
||||
}
|
||||
if let Some(old_value) = old_tag.$get() {
|
||||
if old_value != value.as_ref() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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}"))))
|
||||
}
|
||||
//match self {
|
||||
//Metadata::Music { tag: Some(tag), new_tag, .. } => {
|
||||
//}
|
||||
//_ => None
|
||||
//}
|
||||
//let old_tag = &mut self.tag;
|
||||
//let new_tag = &mut self.new_tag;
|
||||
//if let Metadata::Music { tag: Some(tag), .. } = self
|
||||
//&& Some(value.as_ref()) != tag.$get().as_deref()
|
||||
//{
|
||||
//*$field = Some(value.as_ref().into());
|
||||
//return Some(TagItem::new($key, ItemValue::Text(value.as_ref().into())))
|
||||
//}
|
||||
None
|
||||
}
|
||||
}
|
||||
};
|
||||
(number: $get:ident $set:ident $key:expr) => {
|
||||
impl Metadata {
|
||||
pub fn $get (&self) -> Option<Arc<str>> {
|
||||
if let Metadata::Music { tag: Some(tag), .. } = self {
|
||||
return tag.$get().map(|t|format!("{t}").into())
|
||||
}
|
||||
None
|
||||
}
|
||||
pub fn $set (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
|
||||
if let Ok(value) = value.as_ref().trim().parse::<u32>() {
|
||||
if let Metadata::Music { tag: Some(old_tag), new_tag, .. } = self {
|
||||
if old_tag.$get().is_none() {
|
||||
}
|
||||
if let Some(old_value) = old_tag.$get() {
|
||||
if old_value != value {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//let old_tag = &mut self.tag;
|
||||
//let new_tag = &mut self.new_tag;
|
||||
//if let Metadata::Music { tag: Some(tag), .. } = 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_get_string!(artist); metadata_set_string!(set_artist, artist, ItemKey::TrackArtist);
|
||||
|
||||
metadata_get_number!(year); metadata_set_number!(set_year, year, ItemKey::Year);
|
||||
|
||||
metadata_get_string!(album); metadata_set_string!(set_album, album, ItemKey::AlbumTitle);
|
||||
|
||||
metadata_get_number!(track); metadata_set_number!(set_track, track, ItemKey::TrackNumber);
|
||||
|
||||
metadata_get_string!(title); metadata_set_string!(set_title, title, ItemKey::TrackTitle);
|
||||
metadata_field!(string: artist set_artist ItemKey::TrackArtist);
|
||||
metadata_field!(number: year set_year ItemKey::Year);
|
||||
metadata_field!(string: album set_album ItemKey::AlbumTitle);
|
||||
metadata_field!(number: track set_track ItemKey::TrackNumber);
|
||||
metadata_field!(string: title set_title ItemKey::TrackTitle);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue