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 crate::*;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufReader, Read};
|
use std::io::{BufReader, Read};
|
||||||
|
use std::borrow::Borrow;
|
||||||
use byte_unit::{Byte, Unit::MB};
|
use byte_unit::{Byte, Unit::MB};
|
||||||
use lofty::{file::TaggedFileExt, probe::Probe, tag::{Accessor, Tag, TagItem, ItemKey, ItemValue}};
|
use lofty::{file::TaggedFileExt, probe::Probe, tag::{Accessor, Tag, TagItem, ItemKey, ItemValue}};
|
||||||
|
|
||||||
|
|
@ -16,16 +17,7 @@ pub enum Metadata {
|
||||||
hash: Arc<str>,
|
hash: Arc<str>,
|
||||||
size: Arc<str>,
|
size: Arc<str>,
|
||||||
tag: Option<Arc<Tag>>,
|
tag: Option<Arc<Tag>>,
|
||||||
artist: Option<Arc<str>>,
|
new_tag: Option<Arc<Tag>>,
|
||||||
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>>,
|
|
||||||
},
|
},
|
||||||
Image {
|
Image {
|
||||||
invalid: bool,
|
invalid: bool,
|
||||||
|
|
@ -54,19 +46,10 @@ impl Metadata {
|
||||||
let size = Byte::from_u64(data.len() as u64).get_adjusted_unit(MB);
|
let size = Byte::from_u64(data.len() as u64).get_adjusted_unit(MB);
|
||||||
Ok(Self::Music {
|
Ok(Self::Music {
|
||||||
hash,
|
hash,
|
||||||
size: format!("{:#>8.2}", size).into(),
|
size: format!("{:#>8.2}", size).into(),
|
||||||
artist: tag.map(|t|t.artist().map(|t|t.into())).flatten(),
|
tag: tag.map(|t|t.clone().into()),
|
||||||
album: tag.map(|t|t.album().map(|t|t.into())).flatten(),
|
new_tag: tag.map(|t|t.clone().into()),
|
||||||
track: tag.map(|t|t.track().map(|t|t.into())).flatten(),
|
invalid: false,
|
||||||
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()),
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Self::new_fallback(path)
|
Self::new_fallback(path)
|
||||||
|
|
@ -132,74 +115,77 @@ impl Metadata {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! metadata_get_string {
|
macro_rules! metadata_field {
|
||||||
($name:ident) => {
|
(string: $get:ident $set:ident $key:expr) => {
|
||||||
impl Metadata {
|
impl Metadata {
|
||||||
pub fn $name (&self) -> Option<Arc<str>> {
|
pub fn $get (&self) -> Option<Arc<str>> {
|
||||||
match self {
|
if let Metadata::Music { tag: Some(tag), .. } = self {
|
||||||
Metadata::Music { tag: Some(tag), .. } =>
|
return tag.$get().map(|t|t.into())
|
||||||
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())))
|
|
||||||
}
|
}
|
||||||
None
|
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() {
|
||||||
|
}
|
||||||
macro_rules! metadata_set_number {
|
if let Some(old_value) = old_tag.$get() {
|
||||||
($name:ident, $field:ident, $key:expr) => {
|
if old_value != value.as_ref() {
|
||||||
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
|
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_field!(string: artist set_artist ItemKey::TrackArtist);
|
||||||
|
metadata_field!(number: year set_year ItemKey::Year);
|
||||||
metadata_get_number!(year); metadata_set_number!(set_year, year, ItemKey::Year);
|
metadata_field!(string: album set_album ItemKey::AlbumTitle);
|
||||||
|
metadata_field!(number: track set_track ItemKey::TrackNumber);
|
||||||
metadata_get_string!(album); metadata_set_string!(set_album, album, ItemKey::AlbumTitle);
|
metadata_field!(string: title set_title ItemKey::TrackTitle);
|
||||||
|
|
||||||
metadata_get_number!(track); metadata_set_number!(set_track, track, ItemKey::TrackNumber);
|
|
||||||
|
|
||||||
metadata_get_string!(title); metadata_set_string!(set_title, title, ItemKey::TrackTitle);
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue