(cleanup): cleanup on src/

This commit is contained in:
socatlolmeow 2026-10-10 18:04:20 +02:00
commit 576801f108
10 changed files with 503 additions and 193 deletions

View file

@ -1,6 +1,6 @@
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
@ -12,15 +12,29 @@ pub struct Store {
pub playlists: BTreeMap<String, Vec<Track>>,
}
pub fn write_atomic(path: &Path, text: &str) -> Result<()> {
if let Some(dir) = path.parent() {
fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
}
let tmp = path.with_extension("tmp");
fs::write(&tmp, text).with_context(|| format!("writing {}", tmp.display()))?;
fs::rename(&tmp, path).with_context(|| format!("replacing {}", path.display()))?;
Ok(())
}
impl Store {
pub fn path() -> Result<PathBuf> {
pub fn dir() -> Result<PathBuf> {
let dir = dirs::config_dir().ok_or_else(|| anyhow!("cannot determine config directory"))?;
let new_dir = dir.join("drift");
let old_dir = dir.join("ytm-tui");
if !new_dir.exists() && old_dir.exists() {
let _ = fs::rename(&old_dir, &new_dir);
}
Ok(new_dir.join("playlists.json"))
Ok(new_dir)
}
fn path() -> Result<PathBuf> {
Ok(Self::dir()?.join("playlists.json"))
}
pub fn load() -> (Store, Option<String>) {
@ -55,15 +69,7 @@ impl Store {
}
pub fn save(&self) -> Result<()> {
let path = Self::path()?;
if let Some(dir) = path.parent() {
fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
}
let tmp = path.with_extension("json.tmp");
let json = serde_json::to_string_pretty(self)?;
fs::write(&tmp, json).with_context(|| format!("writing {}", tmp.display()))?;
fs::rename(&tmp, &path).with_context(|| format!("replacing {}", path.display()))?;
Ok(())
write_atomic(&Self::path()?, &serde_json::to_string_pretty(self)?)
}
pub fn resolve(&self, name: &str) -> Option<String> {