use std::path::PathBuf;
use crate::cache::CacheConfig;
use crate::config::{Config, ConfigError};
#[derive(Debug, Clone)]
pub struct AppState {
pub flake_text: String,
pub flake_path: PathBuf,
pub lock_file: Option<PathBuf>,
pub diff: bool,
pub no_lock: bool,
pub interactive: bool,
pub no_cache: bool,
pub cache_path: Option<PathBuf>,
pub config: Config,
}
impl AppState {
pub fn new(
flake_text: String,
flake_path: PathBuf,
config_path: Option<PathBuf>,
) -> Result<Self, ConfigError> {
Ok(Self {
flake_text,
flake_path,
lock_file: None,
diff: false,
no_lock: false,
interactive: true,
no_cache: false,
cache_path: None,
config: Config::load_from(config_path.as_deref())?,
})
}
pub fn with_diff(mut self, diff: bool) -> Self {
self.diff = diff;
self
}
pub fn with_no_lock(mut self, no_lock: bool) -> Self {
self.no_lock = no_lock;
self
}
pub fn with_interactive(mut self, interactive: bool) -> Self {
self.interactive = interactive;
self
}
pub fn with_lock_file(mut self, lock_file: Option<PathBuf>) -> Self {
self.lock_file = lock_file;
self
}
pub fn with_no_cache(mut self, no_cache: bool) -> Self {
self.no_cache = no_cache;
self
}
pub fn with_cache_path(mut self, cache_path: Option<PathBuf>) -> Self {
self.cache_path = cache_path;
self
}
pub fn cache_config(&self) -> CacheConfig {
if self.no_cache {
CacheConfig::None
} else if let Some(ref path) = self.cache_path {
CacheConfig::Custom(path.clone())
} else {
CacheConfig::Default
}
}
}