refactor; EntryInfo -> Metadata

This commit is contained in:
🪞👃🪞 2025-03-23 19:00:45 +02:00
parent 95aa9f8b02
commit cf18e32e13
5 changed files with 384 additions and 351 deletions

100
src/model/entry.rs Normal file
View file

@ -0,0 +1,100 @@
use crate::*;
use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
#[derive(Debug)]
pub struct Entry {
pub path: PathBuf,
pub depth: usize,
pub info: Arc<RwLock<Metadata>>,
}
impl Entry {
pub fn new (root: &impl AsRef<Path>, path: &impl AsRef<Path>, depth: usize) -> Perhaps<Self> {
let path = path.as_ref();
if path.is_dir() {
Self::new_dir(root, &path, depth)
} else if path.is_file() {
Self::new_file(root, &path, depth)
} else {
Ok(None)
}
}
fn new_dir (_: &impl AsRef<Path>, path: &Path, depth: usize) -> Perhaps<Self> {
Ok(Some(Self {
depth,
path: path.into(),
info: Arc::new(RwLock::new(Metadata::Directory {
hash_file: None,
catalog_file: None,
artist_file: None,
release_file: None,
})),
}))
}
fn new_file (_: &impl AsRef<Path>, path: &Path, depth: usize) -> Perhaps<Self> {
Ok(Some(Self {
depth,
info: Arc::new(RwLock::new(Metadata::new(path)?)),
path: path.into(),
}))
}
pub fn is_dir (&self) -> bool {
matches!(&*self.info.read().unwrap(), Metadata::Directory { .. })
}
pub fn is_mus (&self) -> bool {
matches!(&*self.info.read().unwrap(), Metadata::Music { .. })
}
pub fn is_img (&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 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> ) {
self.info.write().unwrap().set_artist(value)
}
pub fn set_album (&self, value: &impl AsRef<str> ) {
self.info.write().unwrap().set_album(value)
}
pub fn set_title (&self, value: &impl AsRef<str> ) {
self.info.write().unwrap().set_title(value)
}
pub fn set_track (&self, value: &impl AsRef<str> ) {
self.info.write().unwrap().set_track(value)
}
}
impl Eq for Entry {}
impl PartialEq for Entry {
fn eq (&self, other: &Self) -> bool {
self.path.eq(&other.path)
}
}
impl Ord for Entry {
fn cmp (&self, other: &Self) -> Ordering {
self.path.cmp(&other.path)
}
}
impl PartialOrd for Entry {
fn partial_cmp (&self, other: &Self) -> Option<Ordering> {
self.path.partial_cmp(&other.path)
}
}