Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add desktop.json settings store
Daniel Norman committed 7 days ago
commit 9c5b661190b1a5b78dd9086c98b01903b73e6f6b
parent f434db11b6728f99ca305788e4478063da1a2e4e
2 files changed +55 -0
modified crates/radicle-types/src/lib.rs
@@ -18,6 +18,7 @@ pub mod oid;
pub mod outbound;
pub mod repo;
pub mod seeder;
+
pub mod settings;
pub mod source;
pub mod syntax;
pub mod test;
added crates/radicle-types/src/settings.rs
@@ -0,0 +1,54 @@
+
use std::path::{Path, PathBuf};
+

+
use serde::{Deserialize, Serialize};
+

+
use crate::error::Error;
+

+
const SETTINGS_FILE: &str = "desktop.json";
+

+
#[derive(Debug, Clone, Serialize, Deserialize)]
+
#[serde(rename_all = "camelCase")]
+
pub struct Settings {
+
    /// Whether newly-added artifacts are automatically imported into the
+
    /// FsStore and registered with an `iroh://` location. Defaults to true
+
    /// — turning it off keeps the create-artifact path COB-only.
+
    #[serde(default = "default_auto_seed")]
+
    pub auto_seed_artifacts: bool,
+
}
+

+
fn default_auto_seed() -> bool {
+
    true
+
}
+

+
impl Default for Settings {
+
    fn default() -> Self {
+
        Self {
+
            auto_seed_artifacts: default_auto_seed(),
+
        }
+
    }
+
}
+

+
fn path(home: &Path) -> PathBuf {
+
    home.join(SETTINGS_FILE)
+
}
+

+
/// Read settings from `<home>/desktop.json`. Missing file or parse failures
+
/// fall back to defaults — settings are non-critical.
+
pub fn load(home: &Path) -> Settings {
+
    let p = path(home);
+
    let Ok(bytes) = std::fs::read(&p) else {
+
        return Settings::default();
+
    };
+
    serde_json::from_slice(&bytes).unwrap_or_default()
+
}
+

+
/// Write settings atomically (write to temp, rename) so a crash mid-write
+
/// can't leave a half-written file.
+
pub fn save(home: &Path, settings: &Settings) -> Result<(), Error> {
+
    let p = path(home);
+
    let tmp = p.with_extension("json.tmp");
+
    let bytes = serde_json::to_vec_pretty(settings)?;
+
    std::fs::write(&tmp, bytes)?;
+
    std::fs::rename(&tmp, &p)?;
+
    Ok(())
+
}