fix: remove panics and unblock flows
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

This commit is contained in:
2026-11-25 03:20:40 +07:00
parent 0fd8ea12fb
commit 3f64b36ff4
52 changed files with 753 additions and 567 deletions
+1
View File
@@ -6,6 +6,7 @@ publish.workspace = true
[dependencies]
dirs = "6"
log.workspace = true
[lints]
workspace = true
+68 -78
View File
@@ -1,105 +1,95 @@
use std::path::PathBuf;
use std::sync::OnceLock;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
/// The application name.
///
/// It derives the platform-specific data, config and cache directory paths.
/// The application name, which derives the platform-specific directory paths.
pub const APP_NAME: &str = "Signed";
/// Lowercased form of [`APP_NAME`].
///
/// Used in XDG-style paths on Linux and FreeBSD, and the macOS `~/.config` fallback.
/// Lowercased [`APP_NAME`], used in XDG paths and the macOS `~/.config` fallback.
pub const APP_NAME_LOWERCASE: &str = "signed";
/// The resolved data directory.
/// On macOS, this is `~/Library/Application Support/Signed`.
/// On Linux/FreeBSD, this is `$XDG_DATA_HOME/signed`.
/// On Windows, this is `%LOCALAPPDATA%\Signed`.
static CURRENT_DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
/// The resolved config directory, under the platform's standard config location.
static CONFIG_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
if cfg!(target_os = "windows") {
resolve_dir(dirs::config_dir(), "RoamingAppData").join(APP_NAME)
} else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
flatpak_xdg_config.into()
} else {
resolve_dir(dirs::config_dir(), "XDG_CONFIG_HOME")
}
.join(APP_NAME_LOWERCASE)
} else {
home_dir().join(".config").join(APP_NAME_LOWERCASE)
}
});
/// The resolved config directory.
/// On macOS, this is `~/.config/signed`.
/// On Linux/FreeBSD, this is `$XDG_CONFIG_HOME/signed`.
/// On Windows, this is `%APPDATA%\Signed`.
static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
/// The resolved data directory, under the platform's standard data location.
static CURRENT_DATA_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
if cfg!(target_os = "macos") {
home_dir()
.join("Library/Application Support")
.join(APP_NAME)
} else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
flatpak_xdg_data.into()
} else {
resolve_dir(dirs::data_local_dir(), "XDG_DATA_HOME")
}
.join(APP_NAME_LOWERCASE)
} else if cfg!(target_os = "windows") {
resolve_dir(dirs::data_local_dir(), "LocalAppData").join(APP_NAME)
} else {
config_dir().to_path_buf()
}
});
fn home_dir() -> PathBuf {
dirs::home_dir().expect("failed to determine home directory")
static NOSTR_DIR: LazyLock<PathBuf> = LazyLock::new(|| data_dir().join("nostr"));
static REPOS_DIR: LazyLock<PathBuf> =
LazyLock::new(|| std::env::temp_dir().join(APP_NAME_LOWERCASE).join("repos"));
static SETTINGS_FILE: LazyLock<PathBuf> = LazyLock::new(|| config_dir().join("settings.json"));
/// Returns `dir`, or the temp directory when the platform could not resolve it.
fn resolve_dir(dir: Option<PathBuf>, name: &str) -> PathBuf {
dir.unwrap_or_else(|| {
log::warn!("failed to determine the {name} directory; falling back to the temp directory");
std::env::temp_dir()
})
}
/// Returns the current user's Desktop folder.
///
/// Falls back to the home directory or an empty path when it cannot be determined.
fn home_dir() -> PathBuf {
resolve_dir(dirs::home_dir(), "home")
}
/// Returns the user's Desktop folder, falling back to home and then to an empty path.
pub fn desktop_dir() -> PathBuf {
dirs::desktop_dir().unwrap_or_else(|| dirs::home_dir().unwrap_or_default())
}
/// Returns the current user's Documents folder.
///
/// Falls back to the home directory or an empty path when it cannot be determined.
/// Returns the user's Documents folder, falling back to home and then to an empty path.
pub fn documents_dir() -> PathBuf {
dirs::document_dir().unwrap_or_else(|| dirs::home_dir().unwrap_or_default())
}
fn config_dir() -> &'static PathBuf {
CONFIG_DIR.get_or_init(|| {
if cfg!(target_os = "windows") {
dirs::config_dir()
.expect("failed to determine RoamingAppData directory")
.join(APP_NAME)
} else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
flatpak_xdg_config.into()
} else {
dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
}
.join(APP_NAME_LOWERCASE)
} else {
home_dir().join(".config").join(APP_NAME_LOWERCASE)
}
})
fn config_dir() -> &'static Path {
CONFIG_DIR.as_path()
}
fn data_dir() -> &'static PathBuf {
CURRENT_DATA_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
home_dir()
.join("Library/Application Support")
.join(APP_NAME)
} else if cfg!(any(target_os = "linux", target_os = "freebsd")) {
if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
flatpak_xdg_data.into()
} else {
dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
}
.join(APP_NAME_LOWERCASE)
} else if cfg!(target_os = "windows") {
dirs::data_local_dir()
.expect("failed to determine LocalAppData directory")
.join(APP_NAME)
} else {
config_dir().clone()
}
})
fn data_dir() -> &'static Path {
CURRENT_DATA_DIR.as_path()
}
/// Returns the path to the nostr database directory, LMDB.
pub fn nostr_dir() -> &'static PathBuf {
static NOSTR_DIR: OnceLock<PathBuf> = OnceLock::new();
NOSTR_DIR.get_or_init(|| data_dir().join("nostr"))
pub fn nostr_dir() -> &'static Path {
NOSTR_DIR.as_path()
}
/// Returns the path to the local git clone cache, the grasp mirrors.
///
/// The mirrors are disposable and re-cloned from their grasp server on
/// demand, so the cache lives in the OS temp directory for the system to
/// reclaim.
pub fn repos_dir() -> &'static PathBuf {
static REPOS_DIR: OnceLock<PathBuf> = OnceLock::new();
REPOS_DIR.get_or_init(|| std::env::temp_dir().join(APP_NAME_LOWERCASE).join("repos"))
/// The grasp mirror cache, disposable and re-cloned on demand, so it lives in the temp directory.
pub fn repos_dir() -> &'static Path {
REPOS_DIR.as_path()
}
pub fn settings_file() -> &'static PathBuf {
static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
pub fn settings_file() -> &'static Path {
SETTINGS_FILE.as_path()
}