Skip to main content

selene_core/
config.rs

1use std::{
2    collections::HashMap,
3    sync::{LazyLock, RwLock, RwLockReadGuard, RwLockWriteGuard},
4};
5
6use lunar_lib::{config::Config, error};
7use serde::{Deserialize, Serialize};
8
9static COMMON_CONFIG: LazyLock<RwLock<CommonConfig>> = LazyLock::new(|| {
10    let config = match CommonConfig::load() {
11        Ok(config) => config,
12        Err(err) => {
13            error!(
14                "Could not load common config from file, assuming defaults. Reason: {:?}",
15                err
16            );
17            CommonConfig::default()
18        }
19    };
20    RwLock::new(config)
21});
22
23/// Returns a read-only guard to the common config.
24///
25/// # Panics
26///
27/// This function will panic if the internal read/write lock is poisoned
28pub fn common_config() -> RwLockReadGuard<'static, CommonConfig> {
29    COMMON_CONFIG.read().unwrap()
30}
31
32/// Returns a mutable guard to the common config.
33///
34/// # Panics
35///
36/// This function will panic if the internal read/write lock is poisoned
37pub fn common_config_mut() -> RwLockWriteGuard<'static, CommonConfig> {
38    COMMON_CONFIG.write().unwrap()
39}
40
41#[derive(Debug, Serialize, Deserialize, Default)]
42#[serde(default)]
43pub struct CommonConfig {
44    pub main: main_settings::MainSettings,
45    pub loudnorm: loudnorm_settings::LoudnormSettings,
46
47    #[serde(skip_serializing_if = "HashMap::is_empty")]
48    pub export_preset: HashMap<String, export_config::ExportConfig>,
49}
50
51mod core_impls;
52
53mod main_settings;
54pub use main_settings::*;
55
56mod loudnorm_settings;
57pub use loudnorm_settings::*;
58
59mod export_config;
60pub use export_config::*;