Actually usable????????//

This commit is contained in:
2024-11-21 05:02:30 +02:00
parent b1c8417b0f
commit 4ee4ca1add
26 changed files with 2339 additions and 356 deletions

View File

@@ -18,8 +18,11 @@ crate-type = ["rlib"]
bench = false
[dependencies]
xmpd-cliargs.path = "../xmpd-cliargs"
xmpd-settings.path = "../xmpd-settings"
anyhow.workspace = true
uuid.workspace = true
serde.workspace = true
serde_json.workspace = true
url.workspace = true
toml.workspace = true

View File

@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};
#[cfg(test)]
@@ -43,6 +43,29 @@ impl<ST: store::BaseStore + Clone> Manifest<ST> {
self.store_mut().load_from(&p)?;
Ok(())
}
pub fn convert_to<ST2: store::BaseStore>(&self) -> ST2 {
let songs = self.store().get_songs().clone();
let playlists = self.store().get_playlists().clone();
let mut st2 = ST2::empty();
*st2.get_songs_mut() = songs;
*st2.get_playlists_mut() = playlists;
st2
}
pub fn convert_and_save_to<ST2: store::BaseStore>(&self, path: &Path) -> Result<()> {
let st2 = self.convert_to::<ST2>();
std::fs::write(path, st2.to_bytes()?)?;
Ok(())
}
pub fn get_song_as_path(&self, sid: uuid::Uuid) -> Result<PathBuf> {
let ext = &xmpd_settings::Settings::get()?.tooling.song_format;
let mut p = xmpd_cliargs::CLIARGS.cache_path().into_std_path_buf();
p.push("songs");
p.push(sid.to_string());
p.set_extension(ext);
Ok(p)
}
}

View File

@@ -1,4 +1,4 @@
use std::{path::PathBuf, str::FromStr};
use std::{fmt::Display, path::PathBuf, str::FromStr};
@@ -72,3 +72,14 @@ impl SourceType {
}
}
impl Display for SourceType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Youtube => write!(f, "YouTube"),
Self::Soundcloud => write!(f, "SoundCloud"),
Self::Spotify => write!(f, "Spotify"),
s => write!(f, "{s:?}"),
}
}
}

View File

@@ -5,7 +5,9 @@ use uuid::Uuid;
use crate::{playlist::Playlist, song::Song};
mod json;
mod toml;
pub use json::JsonStore;
pub use toml::TomlStore;
pub trait BaseStore {
fn get_songs(&self) -> &HashMap<Uuid, Song>;

View File

@@ -0,0 +1,71 @@
use std::{collections::HashMap, path::PathBuf};
use uuid::Uuid;
use crate::{playlist::Playlist, song::Song};
const DEFAULT_TEXT: &str = r#"{
"songs": {},
"playlists": {}
}"#;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct TomlStore {
#[serde(skip)]
original_path: PathBuf,
songs: HashMap<Uuid, Song>,
playlists: HashMap<Uuid, Playlist>
}
impl super::BaseStore for TomlStore {
fn get_default_file_contents() -> &'static str {
&DEFAULT_TEXT
}
fn get_file_extension() -> &'static str {
"toml"
}
fn empty() -> Self where Self: Sized {
Self {
original_path: PathBuf::new(),
songs: HashMap::default(),
playlists: HashMap::default(),
}
}
fn to_bytes(&self) -> crate::Result<Vec<u8>> {
let s: Vec<u8> = toml::to_string_pretty(self)?.chars().map(|c| c as u8).collect();
Ok(s)
}
fn from_bytes(s: &[u8]) -> crate::Result<Self> where Self: Sized {
let s: Self = toml::from_str(&String::from_utf8(s.to_vec())?)?;
Ok(s)
}
fn get_songs(&self) -> &HashMap<Uuid, Song> {
&self.songs
}
fn get_songs_mut(&mut self) -> &mut HashMap<Uuid, Song> {
&mut self.songs
}
fn get_song(&self, id: &Uuid) -> Option<&Song> {
self.songs.get(id)
}
fn get_song_mut(&mut self, id: &Uuid) -> Option<&mut Song> {
self.songs.get_mut(id)
}
fn get_playlists(&self) -> &HashMap<Uuid, Playlist> {
&self.playlists
}
fn get_playlists_mut(&mut self) -> &mut HashMap<Uuid, Playlist> {
&mut self.playlists
}
fn get_playlist(&self, id: &Uuid) -> Option<&Playlist> {
self.playlists.get(id)
}
fn get_playlist_mut(&mut self, id: &Uuid) -> Option<&mut Playlist> {
self.playlists.get_mut(id)
}
fn save_original_path(&mut self, p: &std::path::Path) {
self.original_path = p.to_path_buf();
}
fn get_original_path(&self) -> &std::path::Path {
&self.original_path
}
}