Skip to main content

vyn_core/
models.rs

1use std::fs;
2use std::path::PathBuf;
3
4use anyhow::{Context, Result};
5use serde::{Deserialize, Serialize};
6
7/// Persisted vault configuration, stored in `.vyn/config.toml`.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct VaultConfig {
10    pub vault_id: String,
11    pub project_name: Option<String>,
12    pub storage_provider: String,
13    pub relay_url: Option<String>,
14}
15
16/// Persisted identity, stored in `.vyn/identity.toml`.
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct IdentityConfig {
19    pub github_username: String,
20    pub ssh_private_key: String,
21    pub ssh_public_key: String,
22}
23
24/// A single entry in the push/pull history log, stored under `.vyn/history/`.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct HistoryEntry {
27    pub timestamp_unix: u64,
28    /// `"push"` or `"pull"`.
29    pub source: String,
30    pub manifest_version: u64,
31    pub file_count: usize,
32}
33
34/// Global per-user config, stored at `~/.config/vyn/global.toml`.
35#[derive(Debug, Clone, Default, Serialize, Deserialize)]
36pub struct GlobalConfig {
37    /// How vyn was installed: `"binary"`, `"cargo"`, `"docker"`, or absent when unknown.
38    pub install_method: Option<String>,
39    /// Unix timestamp (seconds) of the last GitHub releases API check.
40    pub last_version_check_unix: Option<u64>,
41    /// Latest version string seen at the last check (e.g. `"0.1.3"`).
42    pub latest_known_version: Option<String>,
43}
44
45/// Returns the path to `~/.config/vyn/global.toml` (XDG-compliant).
46pub fn global_config_path() -> Option<PathBuf> {
47    dirs::config_dir().map(|d| d.join("vyn").join("global.toml"))
48}
49
50/// Loads `GlobalConfig` from disk, returning a default value if missing or malformed.
51pub fn load_global_config() -> GlobalConfig {
52    let path = match global_config_path() {
53        Some(p) => p,
54        None => return GlobalConfig::default(),
55    };
56    let text = match fs::read_to_string(&path) {
57        Ok(t) => t,
58        Err(_) => return GlobalConfig::default(),
59    };
60    toml::from_str(&text).unwrap_or_default()
61}
62
63/// Persists `GlobalConfig` to disk, creating the directory if needed.
64pub fn save_global_config(cfg: &GlobalConfig) -> Result<()> {
65    let path = global_config_path().context("cannot determine config directory")?;
66    if let Some(parent) = path.parent() {
67        fs::create_dir_all(parent).context("cannot create config directory")?;
68    }
69    let text = toml::to_string(cfg).context("cannot serialize global config")?;
70    fs::write(&path, text).context("cannot write global config")?;
71    Ok(())
72}