cargo_audit/
application.rs

1//! Abscissa application for `cargo audit`
2//!
3//! <https://docs.rs/abscissa_core>
4
5use std::sync::Arc;
6
7use crate::{commands::CargoAuditCommand, config::AuditConfig};
8use abscissa_core::{
9    application::{self, AppCell},
10    config::CfgCell,
11    terminal::ColorChoice,
12    trace, Application, FrameworkError, StandardPaths,
13};
14
15/// Application state
16pub static APP: AppCell<CargoAuditApplication> = AppCell::new();
17
18/// `cargo audit` application
19#[derive(Debug)]
20pub struct CargoAuditApplication {
21    /// Application configuration.
22    config: CfgCell<AuditConfig>,
23
24    /// Application state.
25    state: application::State<Self>,
26}
27
28/// Initialize a new application instance.
29///
30/// By default no configuration is loaded, and the framework state is
31/// initialized to a default, empty state (no components, threads, etc).
32impl Default for CargoAuditApplication {
33    fn default() -> Self {
34        Self {
35            config: CfgCell::default(),
36            state: application::State::default(),
37        }
38    }
39}
40
41impl Application for CargoAuditApplication {
42    /// Entrypoint command for this application.
43    type Cmd = CargoAuditCommand;
44
45    /// Application configuration.
46    type Cfg = AuditConfig;
47
48    /// Paths to resources within the application.
49    type Paths = StandardPaths;
50
51    /// Accessor for application configuration.
52    fn config(&self) -> Arc<AuditConfig> {
53        self.config.read()
54    }
55
56    /// Borrow the application state immutably.
57    fn state(&self) -> &application::State<Self> {
58        &self.state
59    }
60
61    /// Register all components used by this application.
62    fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
63        let components = self.framework_components(command)?;
64        self.state.components_mut().register(components)
65    }
66
67    /// Post-configuration lifecycle callback.
68    fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> {
69        // Configure components
70        self.state.components_mut().after_config(&config)?;
71        self.config.set_once(config);
72        Ok(())
73    }
74
75    /// Color configuration for this application.
76    fn term_colors(&self, entrypoint: &CargoAuditCommand) -> ColorChoice {
77        entrypoint.term_colors()
78    }
79
80    /// Get tracing configuration from command-line options
81    fn tracing_config(&self, command: &CargoAuditCommand) -> trace::Config {
82        if command.verbose {
83            trace::Config::verbose()
84        } else {
85            trace::Config::default()
86        }
87    }
88}