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 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(
33        flake_text: String,
34        flake_path: PathBuf,
35        config_path: Option<PathBuf>,
36    ) -> Result<Self, ConfigError> {
37        Ok(Self {
38            flake_text,
39            flake_path,
40            lock_file: None,
41            diff: false,
42            no_lock: false,
43            interactive: true,
44            no_cache: false,
45            cache_path: None,
46            config: Config::load_from(config_path.as_deref())?,
47        })
48    }
49
50    pub fn with_diff(mut self, diff: bool) -> Self {
51        self.diff = diff;
52        self
53    }
54
55    pub fn with_no_lock(mut self, no_lock: bool) -> Self {
56        self.no_lock = no_lock;
57        self
58    }
59
60    pub fn with_interactive(mut self, interactive: bool) -> Self {
61        self.interactive = interactive;
62        self
63    }
64
65    pub fn with_lock_file(mut self, lock_file: Option<PathBuf>) -> Self {
66        self.lock_file = lock_file;
67        self
68    }
69
70    pub fn with_no_cache(mut self, no_cache: bool) -> Self {
71        self.no_cache = no_cache;
72        self
73    }
74
75    pub fn with_cache_path(mut self, cache_path: Option<PathBuf>) -> Self {
76        self.cache_path = cache_path;
77        self
78    }
79
80    /// Get the cache configuration based on CLI flags.
81    pub fn cache_config(&self) -> CacheConfig {
82        if self.no_cache {
83            CacheConfig::None
84        } else if let Some(ref path) = self.cache_path {
85            CacheConfig::Custom(path.clone())
86        } else {
87            CacheConfig::Default
88        }
89    }
90}