74 lines
2.1 KiB
Rust
74 lines
2.1 KiB
Rust
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::StoreExtras for TomlStore {}
|
|
|
|
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
|
|
}
|
|
}
|
|
|