grindvakt-core 1.3.0

The Grindvakt dynamic DNS engine as a library: keep A, AAAA, TXT (including libp2p _dnsaddr) and CNAME records current on DNSimple or DigitalOcean. Separate IPv4/IPv6 discovery that reports when there is no global address, a plan/apply split with dry runs, a provider trait and typed errors. Caller-owned secrets and paths, no CLI, no prompts, no scheduler. Optional encrypted token vault.
Documentation
//! The standalone Grindvakt app's default locations (feature `standalone`).
//!
//! Embedding applications normally choose their own directory and pass explicit paths
//! everywhere; these exist so the Grindvakt app and tools that read its files agree.

#[cfg(not(windows))]
use crate::error::Error;
use crate::error::Result;
use std::path::{Path, PathBuf};

/// `~/.grindvakt` on Unix; `%PROGRAMDATA%\Grindvakt` on Windows.
pub fn default_data_dir() -> Result<PathBuf> {
    #[cfg(windows)]
    {
        Ok(program_data()?.join("Grindvakt"))
    }
    #[cfg(not(windows))]
    {
        Ok(home()?.join(".grindvakt"))
    }
}

/// `$XDG_RUNTIME_DIR/grindvakt`, else `<cache>/grindvakt/run` on Unix;
/// `%PROGRAMDATA%\Grindvakt\run` on Windows.
pub fn default_lock_dir() -> Result<PathBuf> {
    #[cfg(windows)]
    {
        Ok(program_data()?.join("Grindvakt").join("run"))
    }
    #[cfg(not(windows))]
    {
        if let Ok(dir) = std::env::var("XDG_RUNTIME_DIR") {
            return Ok(Path::new(&dir).join("grindvakt"));
        }
        let base = directories::BaseDirs::new()
            .ok_or_else(|| Error::Config("cannot resolve base dirs".into()))?;
        Ok(base.cache_dir().join("grindvakt").join("run"))
    }
}

/// The config file inside `data_dir`.
pub fn config_file(data_dir: &Path) -> PathBuf {
    data_dir.join(crate::config::CONFIG_FILE_NAME)
}

/// The vault file inside `data_dir`.
#[cfg(feature = "vault")]
pub fn vault_file(data_dir: &Path) -> PathBuf {
    data_dir.join(crate::vault::VAULT_FILE_NAME)
}

#[cfg(not(windows))]
fn home() -> Result<PathBuf> {
    directories::UserDirs::new()
        .map(|u| u.home_dir().to_path_buf())
        .ok_or_else(|| Error::Config("could not resolve home directory".into()))
}

#[cfg(windows)]
fn program_data() -> Result<PathBuf> {
    Ok(std::env::var("PROGRAMDATA")
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from(r"C:\ProgramData")))
}