Skip to main content

flake_edit/app/
state.rs

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