solana_test/
application.rs

1//! SolanaTest Abscissa Application
2
3use crate::{commands::EntryPoint, config::SolanaTestConfig};
4use abscissa_core::{
5    application::{self, AppCell},
6    config::{self, CfgCell},
7    trace, Application, FrameworkError, StandardPaths,
8};
9use abscissa_tokio::TokioComponent;
10
11/// Application state
12pub static APP: AppCell<SolanaTestApp> = AppCell::new();
13
14/// SolanaTest Application
15#[derive(Debug)]
16pub struct SolanaTestApp {
17    /// Application configuration.
18    config: CfgCell<SolanaTestConfig>,
19
20    /// Application state.
21    state: application::State<Self>,
22}
23
24/// Initialize a new application instance.
25///
26/// By default no configuration is loaded, and the framework state is
27/// initialized to a default, empty state (no components, threads, etc).
28impl Default for SolanaTestApp {
29    fn default() -> Self {
30        Self {
31            config: CfgCell::default(),
32            state: application::State::default(),
33        }
34    }
35}
36
37impl Application for SolanaTestApp {
38    /// Entrypoint command for this application.
39    type Cmd = EntryPoint;
40
41    /// Application configuration.
42    type Cfg = SolanaTestConfig;
43
44    /// Paths to resources within the application.
45    type Paths = StandardPaths;
46
47    /// Accessor for application configuration.
48    fn config(&self) -> config::Reader<SolanaTestConfig> {
49        self.config.read()
50    }
51
52    /// Borrow the application state immutably.
53    fn state(&self) -> &application::State<Self> {
54        &self.state
55    }
56
57    /// Register all components used by this application.
58    ///
59    /// If you would like to add additional components to your application
60    /// beyond the default ones provided by the framework, this is the place
61    /// to do so.
62    fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
63        let mut framework_components = self.framework_components(command)?;
64        framework_components.push(Box::new(TokioComponent::new()?));
65        let mut app_components = self.state.components_mut();
66        app_components.register(framework_components)
67    }
68
69    /// Post-configuration lifecycle callback.
70    ///
71    /// Called regardless of whether config is loaded to indicate this is the
72    /// time in app lifecycle when configuration would be loaded if
73    /// possible.
74    fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> {
75        // Configure components
76        let mut components = self.state.components_mut();
77        components.after_config(&config)?;
78        self.config.set_once(config);
79        Ok(())
80    }
81
82    /// Get tracing configuration from command-line options
83    fn tracing_config(&self, command: &EntryPoint) -> trace::Config {
84        if command.verbose {
85            trace::Config::verbose()
86        } else {
87            trace::Config::default()
88        }
89    }
90}