Skip to main content

commit_wizard/engine/models/state/
mod.rs

1pub mod registry;
2
3use serde::{Deserialize, Serialize};
4use std::path::{Path, PathBuf};
5
6use crate::engine::{Result, fs};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(deny_unknown_fields)]
10pub struct AppState {
11    pub version: u32,
12    #[serde(default, skip_serializing_if = "Option::is_none")]
13    pub registry: Option<RegistryState>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(deny_unknown_fields)]
18pub struct RegistryState {
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub name: Option<String>,
21    pub url: String,
22    pub r#ref: String,
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub section: Option<String>,
25    pub resolved_commit: String,
26    pub cache_path: String,
27}
28
29impl AppState {
30    /// Creates a new AppState with the current version
31    pub fn new() -> Self {
32        Self {
33            version: 1,
34            registry: None,
35        }
36    }
37
38    /// Loads AppState from the given path
39    /// Returns a default state if the file doesn't exist
40    pub fn load(path: &Path) -> Result<Self> {
41        if path.exists() {
42            fs::load_json(path)
43        } else {
44            Ok(Self::new())
45        }
46    }
47
48    /// Saves AppState to the given path
49    pub fn save(&self, path: &Path) -> Result<()> {
50        fs::save_json(path, self)
51    }
52}
53
54impl Default for AppState {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl RegistryState {
61    /// Creates a new RegistryState with resolved registry information
62    pub fn new(
63        name: Option<String>,
64        url: String,
65        r#ref: String,
66        section: Option<String>,
67        resolved_commit: String,
68        cache_path: PathBuf,
69    ) -> Self {
70        Self {
71            name,
72            url,
73            r#ref,
74            section,
75            resolved_commit,
76            cache_path: cache_path.to_string_lossy().to_string(),
77        }
78    }
79}