Skip to main content

spotify_launcher/
paths.rs

1use crate::errors::*;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4use std::time::SystemTime;
5use tokio::fs;
6
7pub fn spotify_launcher_path() -> Result<PathBuf> {
8    let path = dirs::data_dir().context("Failed to detect data directory")?;
9    Ok(path.join("spotify-launcher"))
10}
11
12pub fn install_path() -> Result<PathBuf> {
13    let path = spotify_launcher_path()?;
14    Ok(path.join("install"))
15}
16
17pub fn new_install_path() -> Result<PathBuf> {
18    let path = spotify_launcher_path()?;
19    Ok(path.join("install-new"))
20}
21
22pub fn state_file_path() -> Result<PathBuf> {
23    let path = spotify_launcher_path()?;
24    Ok(path.join("state.json"))
25}
26
27#[derive(Debug, Serialize, Deserialize)]
28pub struct State {
29    pub version: String,
30    pub last_update_check: SystemTime,
31}
32
33pub async fn load_state_file() -> Result<Option<State>> {
34    let state_file_path = state_file_path()?;
35    if fs::metadata(&state_file_path).await.is_ok() {
36        debug!("Reading state file from {:?}...", state_file_path);
37        let buf = fs::read(&state_file_path).await.with_context(|| {
38            anyhow!(
39                "Failed to read spotify-launcher state file at {:?}",
40                state_file_path
41            )
42        })?;
43        let state = serde_json::from_slice::<State>(&buf);
44        debug!("Loaded state: {:?}", state);
45        Ok(state.ok())
46    } else {
47        debug!(
48            "State file at {:?} does not exist, using empty state",
49            state_file_path
50        );
51        Ok(None)
52    }
53}