use std::path::{Path, PathBuf};
use crate::commands::EntryPoint;
use abscissa_core::{
application::{self, AppCell},
config::{self, CfgCell},
trace, Application, Configurable, FrameworkError, StandardPaths,
};
use pace_core::prelude::PaceConfig;
pub static PACE_APP: AppCell<PaceApp> = AppCell::new();
#[derive(Debug)]
pub struct PaceApp {
config: CfgCell<PaceConfig>,
state: application::State<Self>,
config_path: PathBuf,
}
impl Default for PaceApp {
fn default() -> Self {
Self {
config: CfgCell::default(),
state: application::State::default(),
config_path: PathBuf::default(),
}
}
}
impl Application for PaceApp {
type Cmd = EntryPoint;
type Cfg = PaceConfig;
type Paths = StandardPaths;
fn config(&self) -> config::Reader<PaceConfig> {
self.config.read()
}
fn state(&self) -> &application::State<Self> {
&self.state
}
fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
let framework_components = self.framework_components(command)?;
let mut app_components = self.state.components_mut();
app_components.register(framework_components)
}
fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> {
let mut components = self.state.components_mut();
components.after_config(&config)?;
self.config.set_once(config);
Ok(())
}
fn tracing_config(&self, command: &EntryPoint) -> trace::Config {
if command.verbose {
trace::Config::verbose()
} else {
trace::Config::default()
}
}
fn init(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
self.register_components(command)?;
let config = command
.config_path()
.map(|path| self.load_config(&path))
.transpose()?
.unwrap_or_default();
self.set_config_path(command.config_path().unwrap_or_default());
self.after_config(command.process_config(config)?)?;
Ok(())
}
}
impl PaceApp {
pub fn config_path(&self) -> &Path {
&self.config_path
}
pub fn set_config_path(&mut self, config_path: PathBuf) {
self.config_path = config_path;
}
}