gled-core 2.30.2

The engine of gled without a user interface: inputs, timing, rendering, Art-Net/DMX output and storage
Documentation
//! Where gled keeps its files. The desktop app uses the user's directories;
//! the server is given one data directory and keeps everything below it.

use std::{path::PathBuf, sync::OnceLock};

use directories::BaseDirs;

static DATA_DIR: OnceLock<PathBuf> = OnceLock::new();

/// Keeps every file below `dir` from now on. Call before the services start;
/// later calls are ignored. Returns `false` if a directory was already set.
pub fn set_data_dir(dir: PathBuf) -> bool {
    DATA_DIR.set(dir).is_ok()
}

/// The asset repository.
pub(crate) fn storage_dir() -> PathBuf {
    match DATA_DIR.get() {
        Some(dir) => dir.join("assets"),
        None => BaseDirs::new()
            .map(|dirs| dirs.data_dir().join("gled2"))
            .unwrap_or_else(|| PathBuf::from("gled2")),
    }
}

/// The TLS certificate and key of the web server.
pub fn tls_dir() -> PathBuf {
    match DATA_DIR.get() {
        Some(dir) => dir.join("tls"),
        None => BaseDirs::new()
            .map(|dirs| dirs.config_dir().join("gled").join("tls"))
            .unwrap_or_else(|| PathBuf::from("gled-tls")),
    }
}

/// Which GPU gled chose for which set of GPUs.
pub(crate) fn gpu_choice_file() -> Option<PathBuf> {
    match DATA_DIR.get() {
        Some(dir) => Some(dir.join("gpu_choice.json")),
        None => BaseDirs::new().map(|dirs| dirs.config_dir().join("gled").join("gpu_choice.json")),
    }
}

/// The certificate fingerprints of the servers this machine connected to.
pub fn known_servers_file() -> Option<PathBuf> {
    BaseDirs::new().map(|dirs| dirs.config_dir().join("gled").join("known_servers.json"))
}

/// The machine's own settings, which are not part of any project.
pub(crate) fn persistent_state_file() -> Option<PathBuf> {
    match DATA_DIR.get() {
        Some(dir) => Some(dir.join("settings.json")),
        None => home::home_dir().map(|path| path.join(".gled")),
    }
}