Skip to main content

halley_config/parse/
loader.rs

1use rune_cfg::RuneConfig;
2
3use crate::layout::RuntimeTuning;
4
5use super::keybinds::{
6    apply_explicit_keybind_overrides_entries, parse_inline_keybinds, strip_inline_keybind_block,
7};
8use super::rules::load_rules_section;
9use super::sections::{
10    load_animations_section, load_autostart_section, load_bearings_section, load_clusters_section,
11    load_cursor_section, load_decay_section, load_decorations_section, load_env_section,
12    load_field_section, load_focus_ring_section, load_font_section, load_input_section,
13    load_keybind_sections, load_nodes_section, load_overlays_section, load_physics_section,
14    load_screenshot_section, load_stacking_section, load_tile_section, load_trail_section,
15    load_viewport_section,
16};
17
18impl RuntimeTuning {
19    pub fn from_rune_file(path: &str) -> Option<Self> {
20        let raw = std::fs::read_to_string(path).ok()?;
21        let seed = Self::builtin_defaults();
22        let inline_keybinds = match parse_inline_keybinds(&raw) {
23            Ok(bindings) => bindings,
24            Err(err) => {
25                eprintln!("halley config keybind parse error: {err}");
26                return None;
27            }
28        };
29
30        let cfg = RuneConfig::from_file(path).or_else(|_| {
31            let sanitized = strip_inline_keybind_block(&raw);
32            RuneConfig::from_str(sanitized.as_str())
33        });
34        let cfg = cfg.ok()?;
35
36        Self::from_parsed_rune(raw.as_str(), &cfg, inline_keybinds, seed)
37    }
38
39    pub(crate) fn from_rune_str_with_seed(raw: &str, seed: Self) -> Option<Self> {
40        let inline_keybinds = match parse_inline_keybinds(raw) {
41            Ok(bindings) => bindings,
42            Err(err) => {
43                eprintln!("halley config keybind parse error: {err}");
44                return None;
45            }
46        };
47
48        let cfg = RuneConfig::from_str(raw).or_else(|_| {
49            let sanitized = strip_inline_keybind_block(raw);
50            RuneConfig::from_str(sanitized.as_str())
51        });
52        let cfg = cfg.ok()?;
53
54        Self::from_parsed_rune(raw, &cfg, inline_keybinds, seed)
55    }
56
57    pub fn from_rune_str(raw: &str) -> Option<Self> {
58        Self::from_rune_str_with_seed(raw, Self::builtin_defaults())
59    }
60
61    fn from_parsed_rune(
62        raw: &str,
63        cfg: &RuneConfig,
64        inline_keybinds: Vec<(String, String)>,
65        seed: Self,
66    ) -> Option<Self> {
67        let mut out = seed;
68
69        load_autostart_section(raw, &mut out);
70        if let Err(err) = load_rules_section(raw, &mut out) {
71            eprintln!("halley config rules parse error: {err}");
72            return None;
73        }
74        load_env_section(cfg, &mut out);
75        load_input_section(cfg, &mut out);
76        load_cursor_section(cfg, &mut out);
77        load_font_section(cfg, &mut out);
78        load_viewport_section(cfg, &mut out);
79        load_focus_ring_section(cfg, &mut out);
80        load_bearings_section(cfg, &mut out);
81        load_trail_section(cfg, &mut out);
82        load_nodes_section(cfg, &mut out);
83        load_clusters_section(cfg, &mut out);
84        load_tile_section(cfg, &mut out);
85        load_stacking_section(cfg, &mut out);
86        load_decay_section(cfg, &mut out);
87        load_field_section(cfg, &mut out);
88        load_physics_section(cfg, &mut out);
89        load_decorations_section(cfg, &mut out);
90        load_animations_section(cfg, &mut out);
91        load_overlays_section(cfg, &mut out);
92        load_screenshot_section(cfg, &mut out);
93        if let Err(err) = load_keybind_sections(cfg, &mut out) {
94            eprintln!("halley config keybind parse error: {err}");
95            return None;
96        }
97
98        if !inline_keybinds.is_empty() {
99            if let Err(err) = apply_explicit_keybind_overrides_entries(&inline_keybinds, &mut out) {
100                eprintln!("halley config keybind parse error: {err}");
101                return None;
102            }
103        }
104
105        Some(out)
106    }
107}
108
109pub fn from_rune_file(path: &str) -> Option<RuntimeTuning> {
110    RuntimeTuning::from_rune_file(path)
111}