flake_edit/app/
state.rs

1use std::path::PathBuf;
2
3use crate::cache::CacheConfig;
4use crate::config::Config;
5
6/// Application state for a flake-edit session.
7///
8/// Holds the flake content, file paths, and configuration options.
9#[derive(Debug, Clone)]
10pub struct AppState {
11    /// Content of the flake.nix file
12    pub flake_text: String,
13    /// Path to the flake.nix file
14    pub flake_path: PathBuf,
15    /// Path to the flake.lock file (if specified)
16    pub lock_file: Option<PathBuf>,
17    /// Only show diff, don't write changes
18    pub diff: bool,
19    /// Skip running nix flake lock after changes
20    pub no_lock: bool,
21    /// Allow interactive TUI prompts
22    pub interactive: bool,
23    /// Disable reading from and writing to the completion cache
24    pub no_cache: bool,
25    /// Custom cache file path (for testing or portable configs)
26    pub cache_path: Option<PathBuf>,
27    /// Loaded configuration
28    pub config: Config,
29}
30
31impl AppState {
32    pub fn new(flake_text: String, flake_path: PathBuf) -> Self {
33        Self {
34            flake_text,
35            flake_path,
36            lock_file: None,
37            diff: false,
38            no_lock: false,
39            interactive: true,
40            no_cache: false,
41            cache_path: None,
42            config: Config::load(),
43        }
44    }
45
46    pub fn with_diff(mut self, diff: bool) -> Self {
47        self.diff = diff;
48        self
49    }
50
51    pub fn with_no_lock(mut self, no_lock: bool) -> Self {
52        self.no_lock = no_lock;
53        self
54    }
55
56    pub fn with_interactive(mut self, interactive: bool) -> Self {
57        self.interactive = interactive;
58        self
59    }
60
61    pub fn with_lock_file(mut self, lock_file: Option<PathBuf>) -> Self {
62        self.lock_file = lock_file;
63        self
64    }
65
66    pub fn with_no_cache(mut self, no_cache: bool) -> Self {
67        self.no_cache = no_cache;
68        self
69    }
70
71    pub fn with_cache_path(mut self, cache_path: Option<PathBuf>) -> Self {
72        self.cache_path = cache_path;
73        self
74    }
75
76    /// Get the cache configuration based on CLI flags.
77    pub fn cache_config(&self) -> CacheConfig {
78        if self.no_cache {
79            CacheConfig::None
80        } else if let Some(ref path) = self.cache_path {
81            CacheConfig::Custom(path.clone())
82        } else {
83            CacheConfig::Default
84        }
85    }
86}