Skip to main content

smplx_sdk_test/
global.rs

1use std::sync::OnceLock;
2
3use serde::{Deserialize, Serialize};
4
5use crate::program::TrackerLogLevel;
6
7static GLOBAL_CONFIG: OnceLock<GlobalConfig> = OnceLock::new();
8
9/// A structure to represent the global configuration settings for the application.
10#[derive(Clone, Copy, Debug, Default)]
11pub struct GlobalConfig {
12    verbosity: Verbosity,
13}
14
15impl GlobalConfig {
16    /// Sets the global configuration for the SDK.
17    ///
18    /// This function allows setting a global configuration which includes
19    /// the logging level for `simplicity` contracts execution.
20    /// It must be called exactly once during the application's lifetime.
21    ///
22    /// # Errors
23    /// Returns an error containing the newly created `GlobalConfig` if the global configuration has already been initialized.
24    pub fn set_global_config(verbosity: Verbosity) -> Result<(), GlobalConfig> {
25        GLOBAL_CONFIG.set(GlobalConfig { verbosity })
26    }
27
28    /// Returns the default log level if `GLOBAL_CONFIG` is not initialized
29    pub fn get_log_level() -> TrackerLogLevel {
30        GLOBAL_CONFIG
31            .get()
32            .map_or(GlobalConfig::default().verbosity.tracker_log_level(), |config| {
33                config.verbosity.tracker_log_level()
34            })
35    }
36
37    /// Returns whether the global configuration includes debug symbols,
38    /// defaulting to `false` if `GLOBAL_CONFIG` is not initialized.
39    pub fn get_include_debug_symbols() -> bool {
40        GLOBAL_CONFIG
41            .get()
42            .map_or(GlobalConfig::default().verbosity.include_debug_symbols(), |config| {
43                config.verbosity.include_debug_symbols()
44            })
45    }
46
47    /// Returns `true` if the log level corresponds to [`Verbosity::MAX_VERBOSITY_LEVEL`]
48    ///
49    /// Equivalent to the `-vv` flag passed to `simplex test`.
50    #[must_use]
51    pub fn is_max_verbose() -> bool {
52        Self::get_log_level() == Verbosity::new(Verbosity::MAX_VERBOSITY_LEVEL).tracker_log_level()
53    }
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
57/// An enumeration to represent the verbosity levels of the Simplicity execution logging.
58pub enum Verbosity {
59    /// No logs will be printed.
60    #[default]
61    None = 0,
62    /// The debug mode
63    Debug = 1,
64    /// The trace mode
65    Trace = 2,
66}
67
68impl Verbosity {
69    /// The maximum allowed verbosity level.
70    pub const MAX_VERBOSITY_LEVEL: u8 = 2;
71
72    /// Creates a `Verbosity` instance from the number of verbosity flags provided (e.g., -v, -vv).
73    #[must_use]
74    pub fn new(flags: u8) -> Self {
75        match flags {
76            0 => Verbosity::None,
77            1 => Verbosity::Debug,
78            _ => Verbosity::Trace,
79        }
80    }
81
82    /// Converts the `Verbosity` level to a corresponding `TrackerLogLevel`.
83    #[must_use]
84    pub fn tracker_log_level(&self) -> TrackerLogLevel {
85        match self {
86            Verbosity::None => TrackerLogLevel::None,
87            Verbosity::Debug => TrackerLogLevel::Debug,
88            Verbosity::Trace => TrackerLogLevel::Trace,
89        }
90    }
91
92    /// Determines if the current verbosity level includes debug symbols.
93    #[must_use]
94    pub fn include_debug_symbols(&self) -> bool {
95        matches!(self, Verbosity::Debug | Verbosity::Trace)
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn verbosity_maps_flags_to_logging_behavior() {
105        assert_eq!(Verbosity::new(0), Verbosity::None);
106        assert_eq!(Verbosity::new(1), Verbosity::Debug);
107        assert_eq!(Verbosity::new(2), Verbosity::Trace);
108        assert_eq!(Verbosity::new(9), Verbosity::Trace);
109        assert!(!Verbosity::None.include_debug_symbols());
110        assert!(Verbosity::Debug.include_debug_symbols());
111        assert_eq!(Verbosity::Trace.tracker_log_level(), TrackerLogLevel::Trace);
112    }
113}