jay-toml-config 0.12.0

Internal dependency of the Jay compositor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use {
    crate::{
        config::{
            Action, Config, Egui, Libei, Theme, UiDrag,
            context::Context,
            extractor::{Extractor, ExtractorError, arr, bol, int, opt, recover, str, val},
            parser::{DataType, ParseResult, Parser, UnexpectedDataType},
            parsers::{
                action::ActionParser,
                actions::ActionsParser,
                client_rule::ClientRulesParser,
                color_management::ColorManagementParser,
                connector::ConnectorsParser,
                drm_device::DrmDevicesParser,
                drm_device_match::DrmDeviceMatchParser,
                egui::EguiParser,
                env::EnvParser,
                fallback_output_mode::FallbackOutputModeParser,
                float::FloatParser,
                focus_history::FocusHistoryParser,
                gfx_api::GfxApiParser,
                idle::IdleParser,
                input::InputsParser,
                input_mode::InputModesParser,
                keymap::KeymapParser,
                libei::LibeiParser,
                log_level::LogLevelParser,
                output::OutputsParser,
                repeat_rate::RepeatRateParser,
                shortcuts::{
                    ComplexShortcutsParser, ShortcutsParser, ShortcutsParserError,
                    parse_modified_keysym_str,
                },
                simple_im::SimpleImParser,
                status::StatusParser,
                tearing::TearingParser,
                theme::ThemeParser,
                ui_drag::UiDragParser,
                vrr::VrrParser,
                window_rule::WindowRulesParser,
                workspace_display_order::WorkspaceDisplayOrderParser,
                xwayland::XwaylandParser,
            },
            spanned::SpannedErrorExt,
        },
        toml::{
            toml_span::{DespanExt, Span, Spanned},
            toml_value::Value,
        },
    },
    ahash::AHashMap,
    indexmap::IndexMap,
    jay_config::keyboard::syms::KeySym,
    kbvm::Keysym,
    std::collections::HashSet,
    thiserror::Error,
};

#[derive(Debug, Error)]
pub enum ConfigParserError {
    #[error(transparent)]
    Expected(#[from] UnexpectedDataType),
    #[error(transparent)]
    Extractor(#[from] ExtractorError),
    #[error("Could not parse the shortcuts")]
    ParseShortcuts(#[source] ShortcutsParserError),
}

pub struct ConfigParser<'a>(pub &'a Context<'a>);

impl ConfigParser<'_> {
    fn parse_action(&self, name: &str, action: Option<Spanned<&Value>>) -> Option<Action> {
        match action {
            None => None,
            Some(value) => match value.parse(&mut ActionParser(self.0)) {
                Ok(v) => Some(v),
                Err(e) => {
                    log::warn!("Could not parse the {name} action: {}", self.0.error(e));
                    None
                }
            },
        }
    }
}

impl Parser for ConfigParser<'_> {
    type Value = Config;
    type Error = ConfigParserError;
    const EXPECTED: &'static [DataType] = &[DataType::Table];

    fn parse_table(
        &mut self,
        span: Span,
        table: &IndexMap<Spanned<String>, Spanned<Value>>,
    ) -> ParseResult<Self> {
        let mut ext = Extractor::new(self.0, span, table);
        let (
            (
                keymap_val,
                shortcuts_val,
                on_graphics_init_val,
                status_val,
                outputs_val,
                connectors_val,
                workspace_capture,
                env_val,
                on_startup_val,
                keymaps_val,
            ),
            (
                log_level_val,
                theme_val,
                gfx_api_val,
                drm_devices_val,
                direct_scanout,
                render_device_val,
                inputs_val,
                on_idle_val,
                _,
                idle_val,
            ),
            (
                explicit_sync,
                repeat_rate_val,
                complex_shortcuts_val,
                focus_follows_mouse,
                window_management_key_val,
                vrr_val,
                tearing_val,
                libei_val,
                ui_drag_val,
                xwayland_val,
            ),
            (
                color_management_val,
                float_val,
                actions_val,
                max_action_depth_val,
                client_rules_val,
                window_rules_val,
                pointer_revert_key_str,
                use_hardware_cursor,
                show_bar,
                focus_history_val,
            ),
            (
                middle_click_paste,
                input_modes_val,
                workspace_display_order_val,
                auto_reload,
                simple_im_val,
                show_titles,
                fallback_output_mode_val,
                egui_val,
            ),
        ) = ext.extract((
            (
                opt(val("keymap")),
                opt(val("shortcuts")),
                opt(val("on-graphics-initialized")),
                opt(val("status")),
                opt(val("outputs")),
                opt(val("connectors")),
                recover(opt(bol("workspace-capture"))),
                opt(val("env")),
                opt(val("on-startup")),
                recover(opt(arr("keymaps"))),
            ),
            (
                opt(val("log-level")),
                opt(val("theme")),
                opt(val("gfx-api")),
                opt(val("drm-devices")),
                recover(opt(bol("direct-scanout"))),
                opt(val("render-device")),
                opt(val("inputs")),
                opt(val("on-idle")),
                opt(val("$schema")),
                opt(val("idle")),
            ),
            (
                recover(opt(bol("explicit-sync"))),
                opt(val("repeat-rate")),
                opt(val("complex-shortcuts")),
                recover(opt(bol("focus-follows-mouse"))),
                recover(opt(str("window-management-key"))),
                opt(val("vrr")),
                opt(val("tearing")),
                opt(val("libei")),
                opt(val("ui-drag")),
                opt(val("xwayland")),
            ),
            (
                opt(val("color-management")),
                opt(val("float")),
                opt(val("actions")),
                recover(opt(int("max-action-depth"))),
                opt(val("clients")),
                opt(val("windows")),
                recover(opt(str("pointer-revert-key"))),
                recover(opt(bol("use-hardware-cursor"))),
                recover(opt(bol("show-bar"))),
                opt(val("focus-history")),
            ),
            (
                recover(opt(bol("middle-click-paste"))),
                opt(val("modes")),
                opt(val("workspace-display-order")),
                recover(opt(bol("auto-reload"))),
                opt(val("simple-im")),
                recover(opt(bol("show-titles"))),
                opt(val("fallback-output-mode")),
                opt(val("egui")),
            ),
        ))?;
        let mut keymap = None;
        if let Some(value) = keymap_val {
            match value.parse(&mut KeymapParser {
                cx: self.0,
                definition: false,
            }) {
                Ok(m) => keymap = Some(m),
                Err(e) => {
                    log::warn!("Could not parse the keymap: {}", self.0.error(e));
                }
            }
        }
        let mut used_keys = HashSet::new();
        let mut shortcuts = vec![];
        if let Some(value) = shortcuts_val {
            value
                .parse(&mut ShortcutsParser {
                    cx: self.0,
                    used_keys: &mut used_keys,
                    shortcuts: &mut shortcuts,
                })
                .map_spanned_err(ConfigParserError::ParseShortcuts)?;
        }
        if let Some(value) = complex_shortcuts_val {
            value
                .parse(&mut ComplexShortcutsParser {
                    cx: self.0,
                    used_keys: &mut used_keys,
                    shortcuts: &mut shortcuts,
                })
                .map_spanned_err(ConfigParserError::ParseShortcuts)?;
        }
        if shortcuts.is_empty() {
            log::warn!("Config defines no shortcuts");
        }
        let on_graphics_initialized =
            self.parse_action("on-graphics-initialized", on_graphics_init_val);
        let on_idle = self.parse_action("on-idle", on_idle_val);
        let on_startup = self.parse_action("on-startup", on_startup_val);
        let mut status = None;
        if let Some(value) = status_val {
            match value.parse(&mut StatusParser(self.0)) {
                Ok(v) => status = Some(v),
                Err(e) => log::warn!("Could not parse the status config: {}", self.0.error(e)),
            }
        }
        let mut outputs = vec![];
        if let Some(value) = outputs_val {
            match value.parse(&mut OutputsParser(self.0)) {
                Ok(v) => outputs = v,
                Err(e) => log::warn!("Could not parse the outputs: {}", self.0.error(e)),
            }
        }
        let mut connectors = vec![];
        if let Some(value) = connectors_val {
            match value.parse(&mut ConnectorsParser(self.0)) {
                Ok(v) => connectors = v,
                Err(e) => log::warn!("Could not parse the connectors: {}", self.0.error(e)),
            }
        }
        let mut env = vec![];
        if let Some(value) = env_val {
            match value.parse(&mut EnvParser) {
                Ok(v) => env = v,
                Err(e) => log::warn!(
                    "Could not parse the environment variables: {}",
                    self.0.error(e)
                ),
            }
        }
        let mut keymaps = vec![];
        if let Some(value) = keymaps_val {
            for value in value.value {
                match value.parse(&mut KeymapParser {
                    cx: self.0,
                    definition: true,
                }) {
                    Ok(m) => keymaps.push(m),
                    Err(e) => {
                        log::warn!("Could not parse a keymap: {}", self.0.error(e));
                    }
                }
            }
        }

        let mut log_level = None;
        if let Some(value) = log_level_val {
            match value.parse(&mut LogLevelParser) {
                Ok(v) => log_level = Some(v),
                Err(e) => {
                    log::warn!("Could not parse the log level: {}", self.0.error(e));
                }
            }
        }
        let mut theme = Theme::default();
        if let Some(value) = theme_val {
            match value.parse(&mut ThemeParser(self.0)) {
                Ok(v) => theme = v,
                Err(e) => {
                    log::warn!("Could not parse the theme: {}", self.0.error(e));
                }
            }
        }
        let mut egui = Egui::default();
        if let Some(value) = egui_val {
            match value.parse(&mut EguiParser(self.0)) {
                Ok(v) => egui = v,
                Err(e) => {
                    log::warn!("Could not parse the egui settings: {}", self.0.error(e));
                }
            }
        }
        let mut gfx_api = None;
        if let Some(value) = gfx_api_val {
            match value.parse(&mut GfxApiParser) {
                Ok(v) => gfx_api = Some(v),
                Err(e) => {
                    log::warn!("Could not parse the graphics API: {}", self.0.error(e));
                }
            }
        }
        let mut drm_devices = vec![];
        if let Some(value) = drm_devices_val {
            match value.parse(&mut DrmDevicesParser(self.0)) {
                Ok(v) => drm_devices = v,
                Err(e) => {
                    log::warn!("Could not parse the drm devices: {}", self.0.error(e));
                }
            }
        }
        let mut render_device = None;
        if let Some(value) = render_device_val {
            match value.parse(&mut DrmDeviceMatchParser(self.0)) {
                Ok(v) => render_device = Some(v),
                Err(e) => {
                    log::warn!("Could not parse the render device: {}", self.0.error(e));
                }
            }
        }
        let mut inputs = vec![];
        if let Some(value) = inputs_val {
            match value.parse(&mut InputsParser(self.0)) {
                Ok(v) => inputs = v,
                Err(e) => {
                    log::warn!("Could not parse the inputs: {}", self.0.error(e));
                }
            }
        }
        let mut idle = None;
        let mut grace_period = None;
        if let Some(value) = idle_val {
            match value.parse(&mut IdleParser(self.0)) {
                Ok(v) => {
                    idle = v.timeout;
                    grace_period = v.grace_period;
                }
                Err(e) => {
                    log::warn!("Could not parse the idle timeout: {}", self.0.error(e));
                }
            }
        }
        let mut repeat_rate = None;
        if let Some(value) = repeat_rate_val {
            match value.parse(&mut RepeatRateParser(self.0)) {
                Ok(v) => repeat_rate = Some(v),
                Err(e) => {
                    log::warn!("Could not parse the repeat rate: {}", self.0.error(e));
                }
            }
        }
        let mut window_management_key = None;
        if let Some(value) = window_management_key_val
            && let Some(key) = parse_modified_keysym_str(self.0, value.span, value.value)
        {
            window_management_key = Some(key);
        }
        let mut vrr = None;
        if let Some(value) = vrr_val {
            match value.parse(&mut VrrParser(self.0)) {
                Ok(v) => vrr = Some(v),
                Err(e) => {
                    log::warn!("Could not parse VRR setting: {}", self.0.error(e));
                }
            }
        }
        let mut tearing = None;
        if let Some(value) = tearing_val {
            match value.parse(&mut TearingParser(self.0)) {
                Ok(v) => tearing = Some(v),
                Err(e) => {
                    log::warn!("Could not parse tearing setting: {}", self.0.error(e));
                }
            }
        }
        let mut libei = Libei::default();
        if let Some(value) = libei_val {
            match value.parse(&mut LibeiParser(self.0)) {
                Ok(v) => libei = v,
                Err(e) => {
                    log::warn!("Could not parse libei setting: {}", self.0.error(e));
                }
            }
        }
        let mut ui_drag = UiDrag::default();
        if let Some(value) = ui_drag_val {
            match value.parse(&mut UiDragParser(self.0)) {
                Ok(v) => ui_drag = v,
                Err(e) => {
                    log::warn!("Could not parse ui-drag setting: {}", self.0.error(e));
                }
            }
        }
        let mut xwayland = None;
        if let Some(value) = xwayland_val {
            match value.parse(&mut XwaylandParser(self.0)) {
                Ok(v) => xwayland = Some(v),
                Err(e) => {
                    log::warn!("Could not parse Xwayland setting: {}", self.0.error(e));
                }
            }
        }
        let mut color_management = None;
        if let Some(value) = color_management_val {
            match value.parse(&mut ColorManagementParser(self.0)) {
                Ok(v) => color_management = Some(v),
                Err(e) => {
                    log::warn!(
                        "Could not parse the color-management settings: {}",
                        self.0.error(e)
                    );
                }
            }
        }
        let mut float = None;
        if let Some(value) = float_val {
            match value.parse(&mut FloatParser(self.0)) {
                Ok(v) => float = Some(v),
                Err(e) => {
                    log::warn!("Could not parse the float settings: {}", self.0.error(e));
                }
            }
        }
        let mut named_actions = vec![];
        if let Some(value) = actions_val {
            let mut parser = ActionsParser {
                cx: self.0,
                used_names: Default::default(),
                actions: &mut named_actions,
            };
            if let Err(e) = value.parse(&mut parser) {
                log::warn!("Could not parse named actions: {}", self.0.error(e));
            }
        }
        let mut max_action_depth = 16;
        if let Some(mut value) = max_action_depth_val {
            if value.value < 0 {
                log::warn!(
                    "Max action depth should not be negative: {}",
                    self.0.error3(value.span)
                );
                value.value = 0;
            }
            max_action_depth = value.value as _;
        }
        let mut client_rules = vec![];
        if let Some(value) = client_rules_val {
            match value.parse(&mut ClientRulesParser(self.0)) {
                Ok(v) => client_rules = v,
                Err(e) => log::warn!("Could not parse the client rules: {}", self.0.error(e)),
            }
        }
        let mut window_rules = vec![];
        if let Some(value) = window_rules_val {
            match value.parse(&mut WindowRulesParser(self.0)) {
                Ok(v) => window_rules = v,
                Err(e) => log::warn!("Could not parse the window rules: {}", self.0.error(e)),
            }
        }
        let mut pointer_revert_key = None;
        if let Some(value) = pointer_revert_key_str {
            match Keysym::from_str(value.value) {
                Some(s) => pointer_revert_key = Some(KeySym(s.0)),
                None => log::warn!("Unknown keysym: {}", self.0.error3(value.span)),
            }
        }
        let mut focus_history = None;
        if let Some(value) = focus_history_val {
            match value.parse(&mut FocusHistoryParser(self.0)) {
                Ok(v) => focus_history = Some(v),
                Err(e) => {
                    log::warn!(
                        "Could not parse the focus-history settings: {}",
                        self.0.error(e)
                    );
                }
            }
        }
        let mut input_modes = AHashMap::new();
        if let Some(value) = input_modes_val {
            match value.parse(&mut InputModesParser(self.0)) {
                Ok(v) => input_modes = v,
                Err(e) => {
                    log::warn!("Could not parse the input modes: {}", self.0.error(e),);
                }
            }
        }
        let mut workspace_display_order = None;
        if let Some(value) = workspace_display_order_val {
            match value.parse(&mut WorkspaceDisplayOrderParser) {
                Ok(v) => workspace_display_order = Some(v),
                Err(e) => {
                    log::warn!(
                        "Could not parse the workspace display order: {}",
                        self.0.error(e)
                    );
                }
            }
        }
        let mut simple_im = None;
        if let Some(value) = simple_im_val {
            match value.parse(&mut SimpleImParser(self.0)) {
                Ok(v) => simple_im = Some(v),
                Err(e) => {
                    log::warn!("Could not parse simple IM setting: {}", self.0.error(e));
                }
            }
        }
        let mut fallback_output_mode = None;
        if let Some(value) = fallback_output_mode_val {
            match value.parse(&mut FallbackOutputModeParser) {
                Ok(v) => fallback_output_mode = Some(v),
                Err(e) => {
                    log::warn!(
                        "Could not parse the fallback output mode: {}",
                        self.0.error(e)
                    );
                }
            }
        }
        Ok(Config {
            keymap,
            repeat_rate,
            shortcuts,
            on_graphics_initialized,
            on_idle,
            status,
            outputs,
            connectors,
            workspace_capture: workspace_capture.despan().unwrap_or(true),
            env,
            on_startup,
            keymaps,
            auto_reload: auto_reload.despan(),
            log_level,
            theme,
            egui,
            gfx_api,
            drm_devices,
            direct_scanout_enabled: direct_scanout.despan(),
            explicit_sync_enabled: explicit_sync.despan(),
            render_device,
            inputs,
            idle,
            grace_period,
            focus_follows_mouse: focus_follows_mouse.despan().unwrap_or(true),
            window_management_key,
            vrr,
            tearing,
            libei,
            ui_drag,
            xwayland,
            color_management,
            float,
            named_actions,
            max_action_depth,
            client_rules,
            window_rules,
            pointer_revert_key,
            use_hardware_cursor: use_hardware_cursor.despan(),
            show_bar: show_bar.despan(),
            show_titles: show_titles.despan(),
            focus_history,
            middle_click_paste: middle_click_paste.despan(),
            input_modes,
            workspace_display_order,
            simple_im,
            fallback_output_mode,
        })
    }
}