1use std::path::{Path, PathBuf};
4
5use crate::commands::EntryPoint;
6use abscissa_core::{
7 application::{self, AppCell},
8 config::{self, CfgCell},
9 trace, Application, Configurable, FrameworkError, StandardPaths,
10};
11
12use pace_core::prelude::PaceConfig;
13
14pub static PACE_APP: AppCell<PaceApp> = AppCell::new();
16
17#[derive(Debug)]
19pub struct PaceApp {
20 config: CfgCell<PaceConfig>,
22
23 state: application::State<Self>,
25
26 config_path: PathBuf,
28}
29
30impl Default for PaceApp {
35 fn default() -> Self {
36 Self {
37 config: CfgCell::default(),
38 state: application::State::default(),
39 config_path: PathBuf::default(),
40 }
41 }
42}
43
44impl Application for PaceApp {
45 type Cmd = EntryPoint;
47
48 type Cfg = PaceConfig;
50
51 type Paths = StandardPaths;
53
54 fn config(&self) -> config::Reader<PaceConfig> {
56 self.config.read()
57 }
58
59 fn state(&self) -> &application::State<Self> {
61 &self.state
62 }
63
64 fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
70 let framework_components = self.framework_components(command)?;
71 let mut app_components = self.state.components_mut();
72 app_components.register(framework_components)
73 }
74
75 fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> {
81 let mut components = self.state.components_mut();
83 components.after_config(&config)?;
84 self.config.set_once(config);
85 Ok(())
86 }
87
88 fn tracing_config(&self, command: &EntryPoint) -> trace::Config {
90 if command.verbose {
91 trace::Config::verbose()
92 } else {
93 trace::Config::default()
94 }
95 }
96
97 fn init(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
98 self.register_components(command)?;
102
103 let config = command
105 .config_path()
106 .map(|path| self.load_config(&path))
107 .transpose()?
108 .unwrap_or_default();
109
110 self.set_config_path(command.config_path().unwrap_or_default());
113
114 self.after_config(command.process_config(config)?)?;
117
118 Ok(())
119 }
120}
121
122impl PaceApp {
123 pub fn config_path(&self) -> &Path {
125 &self.config_path
126 }
127
128 pub fn set_config_path(&mut self, config_path: PathBuf) {
130 self.config_path = config_path;
131 }
132}