flake_edit/app/
state.rs

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