runtime/
config.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use std::sync::OnceLock;
8
9pub static CONFIG: OnceLock<Config> = OnceLock::new();
10
11#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
12pub enum LogLevel {
13	None = 0,
14	Info = 1,
15	Warn = 2,
16	Error = 3,
17	Debug = 4,
18}
19
20impl LogLevel {
21	pub fn is_stdout(&self) -> bool {
22		match self {
23			LogLevel::None | LogLevel::Info | LogLevel::Debug => true,
24			LogLevel::Warn | LogLevel::Error => false,
25		}
26	}
27
28	pub fn is_stderr(&self) -> bool {
29		!self.is_stdout()
30	}
31}
32
33#[derive(Clone, Copy, Debug)]
34pub struct Config {
35	pub log_level: LogLevel,
36	pub script: bool,
37	pub typescript: bool,
38}
39
40impl Config {
41	pub fn log_level(self, log_level: LogLevel) -> Config {
42		Config { log_level, ..self }
43	}
44
45	pub fn script(self, script: bool) -> Config {
46		Config { script, ..self }
47	}
48
49	pub fn typescript(self, typescript: bool) -> Config {
50		Config { typescript, ..self }
51	}
52
53	pub fn global() -> &'static Config {
54		CONFIG.get().expect("Configuration not initialised")
55	}
56}
57
58impl Default for Config {
59	fn default() -> Config {
60		Config {
61			log_level: LogLevel::Error,
62			script: false,
63			typescript: true,
64		}
65	}
66}