Skip to main content

rgx/config/
settings.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4use crate::engine::EngineKind;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Settings {
8    #[serde(default = "default_engine")]
9    pub default_engine: String,
10    #[serde(default)]
11    pub case_insensitive: bool,
12    #[serde(default)]
13    pub multiline: bool,
14    #[serde(default)]
15    pub dotall: bool,
16    #[serde(default = "default_true")]
17    pub unicode: bool,
18    #[serde(default)]
19    pub extended: bool,
20    #[serde(default)]
21    pub show_whitespace: bool,
22    #[serde(default)]
23    pub theme: ThemeSettings,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, Default)]
27pub struct ThemeSettings {
28    #[serde(default)]
29    pub catppuccin: bool,
30}
31
32fn default_engine() -> String {
33    "rust".to_string()
34}
35
36fn default_true() -> bool {
37    true
38}
39
40impl Default for Settings {
41    fn default() -> Self {
42        Self {
43            default_engine: default_engine(),
44            case_insensitive: false,
45            multiline: false,
46            dotall: false,
47            unicode: default_true(),
48            extended: false,
49            show_whitespace: false,
50            theme: ThemeSettings::default(),
51        }
52    }
53}
54
55impl Settings {
56    pub fn load() -> Self {
57        let path = config_path();
58        if let Some(path) = path {
59            if path.exists() {
60                if let Ok(content) = std::fs::read_to_string(&path) {
61                    if let Ok(settings) = toml::from_str(&content) {
62                        return settings;
63                    }
64                }
65            }
66        }
67        Self::default()
68    }
69
70    pub fn parse_engine(&self) -> EngineKind {
71        match self.default_engine.as_str() {
72            "fancy" => EngineKind::FancyRegex,
73            #[cfg(feature = "pcre2-engine")]
74            "pcre2" => EngineKind::Pcre2,
75            _ => EngineKind::RustRegex,
76        }
77    }
78}
79
80fn config_path() -> Option<PathBuf> {
81    dirs::config_dir().map(|d| d.join("rgx").join("config.toml"))
82}