use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use tui_pane::WatchedFile;
use crate::config::CargoPortConfig;
use crate::config::EdgeScroll;
use crate::config::NavigationKeys;
use crate::config::NonRustInclusion;
use crate::config::ScrollDirection;
pub(crate) struct Config {
file: WatchedFile<CargoPortConfig>,
}
impl Config {
pub fn new(path: Option<PathBuf>, current: CargoPortConfig) -> Self {
Self {
file: WatchedFile::new(path, current),
}
}
pub const fn current(&self) -> &CargoPortConfig { &self.file.current }
pub const fn current_mut(&mut self) -> &mut CargoPortConfig { &mut self.file.current }
pub fn path(&self) -> Option<&Path> { self.file.path() }
pub fn sync_stamp(&mut self) { self.file.sync_stamp(); }
pub fn take_stamp_change(&mut self) -> Option<&Path> { self.file.take_stamp_change() }
pub const fn lint_enabled(&self) -> bool { self.current().lint.enabled }
pub const fn invert_scroll(&self) -> ScrollDirection { self.current().mouse.invert_scroll }
pub const fn include_non_rust(&self) -> NonRustInclusion { self.current().tui.include_non_rust }
pub const fn ci_run_count(&self) -> u32 { self.current().tui.ci_run_count }
pub const fn navigation_keys(&self) -> NavigationKeys { self.current().tui.navigation_keys }
pub const fn edge_scroll(&self) -> EdgeScroll { self.current().tui.edge_scroll }
pub fn editor(&self) -> &str { &self.current().tui.editor }
pub fn terminal_command(&self) -> &str { &self.current().tui.terminal_command }
pub fn discovery_shimmer_enabled(&self) -> bool {
self.current().tui.discovery_shimmer_secs > 0.0
}
pub fn discovery_shimmer_duration(&self) -> Duration {
Duration::from_secs_f64(self.current().tui.discovery_shimmer_secs)
}
#[cfg(test)]
pub fn force_reload_from(&mut self, path: PathBuf) {
let current = self.file.current.clone();
self.file = WatchedFile::new(Some(path), current);
self.file.clear_stamp_for_test();
}
}
#[cfg(test)]
#[allow(
clippy::expect_used,
clippy::unwrap_used,
reason = "tests should panic on unexpected values"
)]
mod tests {
use super::*;
#[test]
fn config_new_seeds_current() {
let cfg = CargoPortConfig::default();
let config = Config::new(None, cfg);
assert!(config.path().is_none());
}
}