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