libremicro 0.1.0

Direct, typed access to the stock Codex Micro HID interface
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
//! Lossless typed keymap snapshots and targeted editing.

use std::collections::HashSet;
use std::fmt;

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};

use crate::{Error, KeyCode, KeyPosition, LayerId, ProfileId, Result, RgbColor};

const EXPECTED_ROW_LENGTHS: [usize; 4] = [2, 4, 4, 3];

/// SHA-256 revision of the exact text read from or prepared for the device.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Revision([u8; 32]);

impl Revision {
    /// Calculates a revision from exact UTF-8 text.
    #[must_use]
    pub fn from_text(text: &str) -> Self {
        Self(Sha256::digest(text.as_bytes()).into())
    }

    /// Returns the raw SHA-256 bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

impl fmt::Display for Revision {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.0 {
            write!(formatter, "{byte:02x}")?;
        }
        Ok(())
    }
}

/// Warning about a forward-compatible field not modeled by this crate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeymapWarning {
    path: String,
    message: String,
}

impl KeymapWarning {
    /// JSON-pointer-like location of the warning.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.path
    }

    /// Human-readable warning.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

/// Structural or semantic keymap validation error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeymapIssue {
    path: String,
    message: String,
}

impl KeymapIssue {
    /// JSON-pointer-like location of the invalid value.
    #[must_use]
    pub fn path(&self) -> &str {
        &self.path
    }

    /// Human-readable validation failure.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

/// Complete validation result for a snapshot or draft.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct KeymapValidation {
    errors: Vec<KeymapIssue>,
    warnings: Vec<KeymapWarning>,
}

impl KeymapValidation {
    /// Returns structural errors that prevent a write plan from being built.
    #[must_use]
    pub fn errors(&self) -> &[KeymapIssue] {
        &self.errors
    }

    /// Returns unknown-field compatibility warnings.
    #[must_use]
    pub fn warnings(&self) -> &[KeymapWarning] {
        &self.warnings
    }

    /// Returns whether the keymap is structurally valid for v1 editing.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.errors.is_empty()
    }
}

/// String-valued joystick mode stored in a layer layout.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct JoystickMode(String);

impl JoystickMode {
    /// Vendor notification mode used by the Codex Micro integration.
    pub const VENDOR: &'static str = "VENDOR";

    /// Creates a joystick mode while preserving future firmware values.
    ///
    /// # Errors
    ///
    /// Rejects empty, control-character, whitespace, or overlong values.
    pub fn new(value: impl Into<String>) -> Result<Self> {
        let value = value.into();
        let invalid = value.is_empty()
            || value.len() > 64
            || value
                .chars()
                .any(|character| character.is_control() || character.is_whitespace());
        if invalid {
            Err(Error::validation(
                "joystick mode",
                format!("{value:?} is not a valid firmware mode"),
            ))
        } else {
            Ok(Self(value))
        }
    }

    /// Returns the firmware mode text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl<'de> Deserialize<'de> for JoystickMode {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::new(value).map_err(serde::de::Error::custom)
    }
}

/// Editable encoder binding within the observed three-entry encoder group.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncoderControl {
    /// Counter-clockwise rotation binding.
    CounterClockwise,
    /// Clockwise rotation binding.
    Clockwise,
    /// Encoder-click binding.
    Press,
}

impl EncoderControl {
    const fn index(self) -> usize {
        match self {
            Self::CounterClockwise => 0,
            Self::Clockwise => 1,
            Self::Press => 2,
        }
    }
}

/// Lossless typed representation of `keymap.json`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KeymapDocument {
    version: u32,
    #[serde(rename = "activeProfileId")]
    active_profile_id: ProfileId,
    profiles: Vec<KeymapProfile>,
    #[serde(rename = "linkedApps", default)]
    linked_apps: Vec<Value>,
    #[serde(default)]
    macros: Vec<Value>,
    #[serde(rename = "macrosGroups", default)]
    macro_groups: Vec<Value>,
    #[serde(rename = "multiActions", default)]
    multi_actions: Vec<Value>,
    #[serde(rename = "multiActionsGroups", default)]
    multi_action_groups: Vec<Value>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl KeymapDocument {
    /// Configuration schema version reported in the document.
    #[must_use]
    pub const fn version(&self) -> u32 {
        self.version
    }

    /// Active profile identifier stored in the document.
    #[must_use]
    pub const fn active_profile_id(&self) -> ProfileId {
        self.active_profile_id
    }

    /// Existing profiles, in device order.
    #[must_use]
    pub fn profiles(&self) -> &[KeymapProfile] {
        &self.profiles
    }

    /// Opaque linked-application records preserved during edits.
    #[must_use]
    pub fn linked_apps(&self) -> &[Value] {
        &self.linked_apps
    }

    /// Opaque macro records preserved during edits.
    #[must_use]
    pub fn macros(&self) -> &[Value] {
        &self.macros
    }

    /// Opaque macro-group records preserved during edits.
    #[must_use]
    pub fn macro_groups(&self) -> &[Value] {
        &self.macro_groups
    }

    /// Opaque multi-action records preserved during edits.
    #[must_use]
    pub fn multi_actions(&self) -> &[Value] {
        &self.multi_actions
    }

    /// Opaque multi-action-group records preserved during edits.
    #[must_use]
    pub fn multi_action_groups(&self) -> &[Value] {
        &self.multi_action_groups
    }

    /// Unknown top-level fields preserved during edits.
    #[must_use]
    pub const fn extra_fields(&self) -> &Map<String, Value> {
        &self.extra
    }
}

/// Existing profile in a keymap document.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KeymapProfile {
    id: ProfileId,
    name: String,
    layers: Vec<KeymapLayer>,
    #[serde(rename = "macrosUsed", default)]
    macros_used: Vec<Value>,
    #[serde(rename = "multiActionsUsed", default)]
    multi_actions_used: Vec<Value>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl KeymapProfile {
    /// Stable profile identifier.
    #[must_use]
    pub const fn id(&self) -> ProfileId {
        self.id
    }

    /// User-visible profile name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Existing layers, in device order.
    #[must_use]
    pub fn layers(&self) -> &[KeymapLayer] {
        &self.layers
    }

    /// Opaque macro references preserved during edits.
    #[must_use]
    pub fn macros_used(&self) -> &[Value] {
        &self.macros_used
    }

    /// Opaque multi-action references preserved during edits.
    #[must_use]
    pub fn multi_actions_used(&self) -> &[Value] {
        &self.multi_actions_used
    }

    /// Unknown profile fields preserved during edits.
    #[must_use]
    pub const fn extra_fields(&self) -> &Map<String, Value> {
        &self.extra
    }
}

/// Existing layer in a keymap profile.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KeymapLayer {
    id: LayerId,
    name: String,
    #[serde(default)]
    color: Option<RgbColor>,
    layout: KeymapLayout,
    #[serde(default)]
    lights: Option<Value>,
    #[serde(default)]
    os: Option<u32>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl KeymapLayer {
    /// Stable layer identifier.
    #[must_use]
    pub const fn id(&self) -> LayerId {
        self.id
    }

    /// User-visible layer name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Optional layer color metadata.
    #[must_use]
    pub const fn color(&self) -> Option<RgbColor> {
        self.color
    }

    /// Physical-control layout.
    #[must_use]
    pub const fn layout(&self) -> &KeymapLayout {
        &self.layout
    }

    /// Opaque layer lighting metadata preserved during edits.
    #[must_use]
    pub const fn lights(&self) -> Option<&Value> {
        self.lights.as_ref()
    }

    /// Optional operating-system selector preserved during edits.
    #[must_use]
    pub const fn os(&self) -> Option<u32> {
        self.os
    }

    /// Unknown layer fields preserved during edits.
    #[must_use]
    pub const fn extra_fields(&self) -> &Map<String, Value> {
        &self.extra
    }
}

/// Typed physical-control layout for a layer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KeymapLayout {
    #[serde(default)]
    encoders: Vec<Vec<KeyCode>>,
    #[serde(default)]
    buttons: Vec<Value>,
    keymap: Vec<Vec<KeyCode>>,
    #[serde(default)]
    joystick: Option<KeymapJoystick>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl KeymapLayout {
    /// Encoder binding groups.
    #[must_use]
    pub fn encoders(&self) -> &[Vec<KeyCode>] {
        &self.encoders
    }

    /// Opaque button metadata preserved during edits.
    #[must_use]
    pub fn buttons(&self) -> &[Value] {
        &self.buttons
    }

    /// Firmware key matrix in its native `[2, 4, 4, 3]` row layout.
    #[must_use]
    pub fn key_matrix(&self) -> &[Vec<KeyCode>] {
        &self.keymap
    }

    /// Joystick configuration, when present.
    #[must_use]
    pub const fn joystick(&self) -> Option<&KeymapJoystick> {
        self.joystick.as_ref()
    }

    /// Unknown layout fields preserved during edits.
    #[must_use]
    pub const fn extra_fields(&self) -> &Map<String, Value> {
        &self.extra
    }
}

/// Joystick configuration inside a keymap layout.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KeymapJoystick {
    #[serde(rename = "type")]
    mode: JoystickMode,
    #[serde(default)]
    sectors: Vec<Value>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl KeymapJoystick {
    /// Current joystick mode.
    #[must_use]
    pub const fn mode(&self) -> &JoystickMode {
        &self.mode
    }

    /// Opaque radial sectors preserved during edits.
    #[must_use]
    pub fn sectors(&self) -> &[Value] {
        &self.sectors
    }

    /// Unknown joystick fields preserved during edits.
    #[must_use]
    pub const fn extra_fields(&self) -> &Map<String, Value> {
        &self.extra
    }
}

/// Immutable keymap text, typed document, revision, and diagnostics read from
/// the device.
#[derive(Debug, Clone)]
pub struct KeymapSnapshot {
    original: String,
    revision: Revision,
    document: KeymapDocument,
    validation: KeymapValidation,
}

impl KeymapSnapshot {
    /// Parses exact `keymap.json` text without discarding unknown fields.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Protocol`] when the text is not a document matching
    /// the minimally known keymap schema.
    pub fn from_json(text: impl Into<String>) -> Result<Self> {
        let original = text.into();
        let document: KeymapDocument = serde_json::from_str(&original)
            .map_err(|error| Error::protocol(format!("invalid keymap.json: {error}")))?;
        let validation = validate_document(&document);
        for warning in validation.warnings() {
            tracing::warn!(
                path = warning.path(),
                message = warning.message(),
                "unknown keymap JSON field"
            );
        }
        Ok(Self {
            revision: Revision::from_text(&original),
            original,
            document,
            validation,
        })
    }

    /// Exact text received from the firmware.
    #[must_use]
    pub fn original_json(&self) -> &str {
        &self.original
    }

    /// SHA-256 revision of [`Self::original_json`].
    #[must_use]
    pub const fn revision(&self) -> Revision {
        self.revision
    }

    /// Parsed lossless typed document.
    #[must_use]
    pub const fn document(&self) -> &KeymapDocument {
        &self.document
    }

    /// Structural errors and unknown-field warnings.
    #[must_use]
    pub const fn validation(&self) -> &KeymapValidation {
        &self.validation
    }

    /// Creates an editable copy tied to this exact snapshot revision.
    #[must_use]
    pub fn draft(&self) -> KeymapDraft {
        KeymapDraft {
            original: self.original.clone(),
            base_revision: self.revision,
            document: self.document.clone(),
            dirty: false,
        }
    }
}

/// Editable key bindings for existing profiles and layers.
#[derive(Debug, Clone)]
pub struct KeymapDraft {
    original: String,
    base_revision: Revision,
    document: KeymapDocument,
    dirty: bool,
}

impl KeymapDraft {
    /// Current typed draft document.
    #[must_use]
    pub const fn document(&self) -> &KeymapDocument {
        &self.document
    }

    /// Validates geometry, identifiers, and compatibility warnings.
    #[must_use]
    pub fn validation(&self) -> KeymapValidation {
        validate_document(&self.document)
    }

    /// Changes one physical key in an existing profile and layer.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] if the profile, layer, or expected
    /// `[2, 4, 4, 3]` key geometry is absent.
    pub fn set_key_binding(
        &mut self,
        profile: ProfileId,
        layer: LayerId,
        position: KeyPosition,
        keycode: KeyCode,
    ) -> Result<()> {
        let layer = find_layer_mut(&mut self.document, profile, layer)?;
        let (row, column) = matrix_location(position);
        let binding = layer
            .layout
            .keymap
            .get_mut(row)
            .and_then(|row_values| row_values.get_mut(column))
            .ok_or_else(|| {
                Error::validation(
                    "keymap geometry",
                    format!("physical position {} is missing", position.get()),
                )
            })?;
        if *binding != keycode {
            *binding = keycode;
            self.dirty = true;
        }
        Ok(())
    }

    /// Changes one of the three bindings in the existing encoder group.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] if the profile, layer, or expected
    /// encoder group is absent.
    pub fn set_encoder_binding(
        &mut self,
        profile: ProfileId,
        layer: LayerId,
        control: EncoderControl,
        keycode: KeyCode,
    ) -> Result<()> {
        let layer = find_layer_mut(&mut self.document, profile, layer)?;
        let binding = layer
            .layout
            .encoders
            .first_mut()
            .and_then(|encoder| encoder.get_mut(control.index()))
            .ok_or_else(|| Error::validation("encoder layout", "expected one three-entry group"))?;
        if *binding != keycode {
            *binding = keycode;
            self.dirty = true;
        }
        Ok(())
    }

    /// Changes the existing joystick mode without replacing its sectors or
    /// unknown metadata.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] if the selected layer has no joystick
    /// configuration.
    pub fn set_joystick_mode(
        &mut self,
        profile: ProfileId,
        layer: LayerId,
        mode: JoystickMode,
    ) -> Result<()> {
        let layer = find_layer_mut(&mut self.document, profile, layer)?;
        let joystick = layer.layout.joystick.as_mut().ok_or_else(|| {
            Error::validation("joystick layout", "selected layer has no joystick")
        })?;
        if joystick.mode != mode {
            joystick.mode = mode;
            self.dirty = true;
        }
        Ok(())
    }

    /// Validates and serializes the draft into a compare-before-write plan.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Validation`] when any structural errors remain or
    /// serialization unexpectedly fails.
    pub fn into_write_plan(self) -> Result<KeymapWritePlan> {
        let validation = validate_document(&self.document);
        if let Some(issue) = validation.errors().first() {
            return Err(Error::validation(
                "keymap draft",
                format!("{}: {}", issue.path(), issue.message()),
            ));
        }
        let candidate = if self.dirty {
            serde_json::to_string(&self.document).map_err(|error| {
                Error::validation("keymap draft", format!("serialization failed: {error}"))
            })?
        } else {
            self.original.clone()
        };
        Ok(KeymapWritePlan {
            expected_revision: self.base_revision,
            candidate_revision: Revision::from_text(&candidate),
            original: self.original,
            candidate,
        })
    }
}

/// Validated compare-before-write keymap transaction input.
#[derive(Debug, Clone)]
pub struct KeymapWritePlan {
    expected_revision: Revision,
    candidate_revision: Revision,
    original: String,
    candidate: String,
}

impl KeymapWritePlan {
    /// Revision that must still be live before applying the plan.
    #[must_use]
    pub const fn expected_revision(&self) -> Revision {
        self.expected_revision
    }

    /// Revision expected after a successful apply.
    #[must_use]
    pub const fn candidate_revision(&self) -> Revision {
        self.candidate_revision
    }

    /// Exact original text that callers should durably back up.
    #[must_use]
    pub fn original_json(&self) -> &str {
        &self.original
    }

    /// Exact candidate text that would be written.
    #[must_use]
    pub fn candidate_json(&self) -> &str {
        &self.candidate
    }

    /// Returns whether applying the plan requires no device write.
    #[must_use]
    pub fn is_unchanged(&self) -> bool {
        self.original == self.candidate
    }
}

/// Failure encountered while attempting to restore the original keymap.
#[cfg(feature = "persistent-writes")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RollbackFailure {
    message: String,
}

#[cfg(feature = "persistent-writes")]
impl RollbackFailure {
    pub(crate) fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }

    /// Human-readable rollback failure.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

/// Final state reported by a feature-gated keymap apply operation.
#[cfg(feature = "persistent-writes")]
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeymapApplyOutcome {
    /// Candidate and original were identical, so no write occurred.
    Unchanged,
    /// Candidate was written and verified exactly.
    Applied {
        /// Verified candidate revision.
        revision: Revision,
    },
    /// Candidate verification failed, but the exact original was restored and
    /// verified.
    RolledBack {
        /// Failure that triggered rollback.
        cause: String,
    },
    /// Candidate verification failed and the rollback could not be verified.
    RollbackFailed {
        /// Failure that triggered rollback.
        cause: String,
        /// Rollback failure details.
        rollback: RollbackFailure,
    },
}

fn find_layer_mut(
    document: &mut KeymapDocument,
    profile_id: ProfileId,
    layer_id: LayerId,
) -> Result<&mut KeymapLayer> {
    let profile = document
        .profiles
        .iter_mut()
        .find(|profile| profile.id == profile_id)
        .ok_or_else(|| {
            Error::validation("profile id", format!("profile {profile_id} does not exist"))
        })?;
    profile
        .layers
        .iter_mut()
        .find(|layer| layer.id == layer_id)
        .ok_or_else(|| {
            Error::validation(
                "layer id",
                format!("layer {layer_id} does not exist in profile {profile_id}"),
            )
        })
}

const fn matrix_location(position: KeyPosition) -> (usize, usize) {
    match position.get() {
        0..=1 => (0, position.get() as usize),
        2..=5 => (1, (position.get() - 2) as usize),
        6..=9 => (2, (position.get() - 6) as usize),
        10..=12 => (3, (position.get() - 10) as usize),
        _ => (usize::MAX, usize::MAX),
    }
}

fn validate_document(document: &KeymapDocument) -> KeymapValidation {
    let mut validation = KeymapValidation::default();
    collect_unknown_fields(&mut validation.warnings, "", &document.extra);

    if document.profiles.is_empty() {
        validation
            .errors
            .push(issue("/profiles", "at least one profile is required"));
    }

    let mut profile_ids = HashSet::new();
    for (profile_index, profile) in document.profiles.iter().enumerate() {
        let profile_path = format!("/profiles/{profile_index}");
        if !profile_ids.insert(profile.id) {
            validation.errors.push(issue(
                format!("{profile_path}/id"),
                format!("duplicate profile id {}", profile.id),
            ));
        }
        collect_unknown_fields(&mut validation.warnings, &profile_path, &profile.extra);
        if profile.layers.is_empty() {
            validation.errors.push(issue(
                format!("{profile_path}/layers"),
                "at least one layer is required",
            ));
        }
        let mut layer_ids = HashSet::new();
        for (layer_index, layer) in profile.layers.iter().enumerate() {
            let layer_path = format!("{profile_path}/layers/{layer_index}");
            if !layer_ids.insert(layer.id) {
                validation.errors.push(issue(
                    format!("{layer_path}/id"),
                    format!("duplicate layer id {}", layer.id),
                ));
            }
            collect_unknown_fields(&mut validation.warnings, &layer_path, &layer.extra);
            validate_layout(&layer.layout, &layer_path, &mut validation);
        }
    }

    if !document
        .profiles
        .iter()
        .any(|profile| profile.id == document.active_profile_id)
    {
        validation.errors.push(issue(
            "/activeProfileId",
            format!("profile {} does not exist", document.active_profile_id),
        ));
    }
    validation
}

fn validate_layout(layout: &KeymapLayout, layer_path: &str, validation: &mut KeymapValidation) {
    let layout_path = format!("{layer_path}/layout");
    collect_unknown_fields(&mut validation.warnings, &layout_path, &layout.extra);
    if layout.keymap.len() != EXPECTED_ROW_LENGTHS.len() {
        validation.errors.push(issue(
            format!("{layout_path}/keymap"),
            format!("expected 4 rows, found {}", layout.keymap.len()),
        ));
    }
    for (row_index, expected) in EXPECTED_ROW_LENGTHS.iter().copied().enumerate() {
        if let Some(row) = layout
            .keymap
            .get(row_index)
            .filter(|row| row.len() != expected)
        {
            validation.errors.push(issue(
                format!("{layout_path}/keymap/{row_index}"),
                format!("expected {expected} entries, found {}", row.len()),
            ));
        }
    }
    if layout.encoders.len() != 1 || layout.encoders.first().map(Vec::len) != Some(3) {
        validation.errors.push(issue(
            format!("{layout_path}/encoders"),
            "expected one three-entry encoder group",
        ));
    }
    if let Some(joystick) = &layout.joystick {
        collect_unknown_fields(
            &mut validation.warnings,
            &format!("{layout_path}/joystick"),
            &joystick.extra,
        );
    }
}

fn collect_unknown_fields(
    warnings: &mut Vec<KeymapWarning>,
    parent: &str,
    fields: &Map<String, Value>,
) {
    warnings.extend(fields.keys().map(|field| KeymapWarning {
        path: format!("{parent}/{}", escape_json_pointer(field)),
        message: "field is not modeled by libremicro v1 and was preserved".to_owned(),
    }));
}

fn escape_json_pointer(value: &str) -> String {
    value.replace('~', "~0").replace('/', "~1")
}

fn issue(path: impl Into<String>, message: impl Into<String>) -> KeymapIssue {
    KeymapIssue {
        path: path.into(),
        message: message.into(),
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{Value, json};

    use super::*;

    const FIXTURE: &str = include_str!("../tests/fixtures/keymap-0.6.2.json");

    fn snapshot() -> KeymapSnapshot {
        KeymapSnapshot::from_json(FIXTURE)
            .unwrap_or_else(|error| panic!("fixture should parse: {error}"))
    }

    #[test]
    fn fixture_should_match_expected_codex_micro_geometry() {
        let snapshot = snapshot();

        assert!(snapshot.validation().is_valid());
    }

    #[test]
    fn from_json_should_report_unknown_fields_with_json_pointer() {
        let mut value: Value = serde_json::from_str(FIXTURE)
            .unwrap_or_else(|error| panic!("fixture should parse as JSON: {error}"));
        value["future/field"] = json!({"enabled": true});
        let snapshot = KeymapSnapshot::from_json(value.to_string())
            .unwrap_or_else(|error| panic!("extended fixture should parse: {error}"));

        assert_eq!(snapshot.validation().warnings()[0].path(), "/future~1field");
    }

    #[test]
    fn edited_plan_should_preserve_unknown_json_values() {
        let mut value: Value = serde_json::from_str(FIXTURE)
            .unwrap_or_else(|error| panic!("fixture should parse as JSON: {error}"));
        value["future"] = json!({"nested": [1, 2, 3]});
        let snapshot = KeymapSnapshot::from_json(value.to_string())
            .unwrap_or_else(|error| panic!("extended fixture should parse: {error}"));
        let mut draft = snapshot.draft();
        draft
            .set_key_binding(
                ProfileId::new(0),
                LayerId::new(0),
                KeyPosition::new(3)
                    .unwrap_or_else(|error| panic!("position should be valid: {error}")),
                KeyCode::new("KC_B")
                    .unwrap_or_else(|error| panic!("keycode should be valid: {error}")),
            )
            .unwrap_or_else(|error| panic!("edit should succeed: {error}"));
        let plan = draft
            .into_write_plan()
            .unwrap_or_else(|error| panic!("plan should build: {error}"));
        let candidate: Value = serde_json::from_str(plan.candidate_json())
            .unwrap_or_else(|error| panic!("candidate should be JSON: {error}"));

        assert_eq!(candidate["future"], json!({"nested": [1, 2, 3]}));
    }

    #[test]
    fn edited_plan_should_preserve_opaque_configuration_sections() {
        let mut value: Value = serde_json::from_str(FIXTURE)
            .unwrap_or_else(|error| panic!("fixture should parse as JSON: {error}"));
        value["linkedApps"] = json!([{"application": "example.invalid", "profileId": 0}]);
        value["macros"] = json!([{"id": 7, "steps": [{"futureStep": true}]}]);
        value["multiActions"] = json!([{"id": 8, "action": {"futureAction": 9}}]);
        let expected_linked_apps = value["linkedApps"].clone();
        let expected_macros = value["macros"].clone();
        let expected_actions = value["multiActions"].clone();
        let snapshot = KeymapSnapshot::from_json(value.to_string())
            .unwrap_or_else(|error| panic!("extended fixture should parse: {error}"));
        let mut draft = snapshot.draft();
        draft
            .set_key_binding(
                ProfileId::new(0),
                LayerId::new(0),
                KeyPosition::new(0)
                    .unwrap_or_else(|error| panic!("position should be valid: {error}")),
                KeyCode::new("KC_A")
                    .unwrap_or_else(|error| panic!("keycode should be valid: {error}")),
            )
            .unwrap_or_else(|error| panic!("edit should succeed: {error}"));
        let plan = draft
            .into_write_plan()
            .unwrap_or_else(|error| panic!("plan should build: {error}"));
        let candidate: Value = serde_json::from_str(plan.candidate_json())
            .unwrap_or_else(|error| panic!("candidate should be JSON: {error}"));

        assert_eq!(candidate["linkedApps"], expected_linked_apps);
        assert_eq!(candidate["macros"], expected_macros);
        assert_eq!(candidate["multiActions"], expected_actions);
    }

    #[test]
    fn set_key_binding_should_target_firmware_row_major_position() {
        let snapshot = snapshot();
        let mut draft = snapshot.draft();
        draft
            .set_key_binding(
                ProfileId::new(0),
                LayerId::new(0),
                KeyPosition::new(3)
                    .unwrap_or_else(|error| panic!("position should be valid: {error}")),
                KeyCode::new("KC_B")
                    .unwrap_or_else(|error| panic!("keycode should be valid: {error}")),
            )
            .unwrap_or_else(|error| panic!("edit should succeed: {error}"));

        assert_eq!(
            draft.document().profiles()[0].layers()[0]
                .layout()
                .key_matrix()[1][1]
                .as_str(),
            "KC_B"
        );
    }

    #[test]
    fn unchanged_draft_should_reuse_exact_original_text() {
        let snapshot = snapshot();
        let plan = snapshot
            .draft()
            .into_write_plan()
            .unwrap_or_else(|error| panic!("plan should build: {error}"));

        assert!(plan.is_unchanged());
    }

    #[test]
    fn malformed_geometry_should_prevent_write_plan() {
        let mut value: Value = serde_json::from_str(FIXTURE)
            .unwrap_or_else(|error| panic!("fixture should parse as JSON: {error}"));
        value["profiles"][0]["layers"][0]["layout"]["keymap"][0] = json!(["KC_A"]);
        let snapshot = KeymapSnapshot::from_json(value.to_string())
            .unwrap_or_else(|error| panic!("structurally bad fixture should still parse: {error}"));

        let error = snapshot
            .draft()
            .into_write_plan()
            .expect_err("invalid geometry must block planning");

        assert!(matches!(error, Error::Validation { .. }));
    }

    #[test]
    fn set_encoder_binding_should_preserve_other_encoder_bindings() {
        let snapshot = snapshot();
        let mut draft = snapshot.draft();
        draft
            .set_encoder_binding(
                ProfileId::new(0),
                LayerId::new(0),
                EncoderControl::Press,
                KeyCode::new("KC_MUTE")
                    .unwrap_or_else(|error| panic!("keycode should be valid: {error}")),
            )
            .unwrap_or_else(|error| panic!("encoder edit should succeed: {error}"));

        assert_eq!(
            draft.document().profiles()[0].layers()[0]
                .layout()
                .encoders()[0][2]
                .as_str(),
            "KC_MUTE"
        );
    }

    #[test]
    fn set_joystick_mode_should_preserve_sectors() {
        let snapshot = snapshot();
        let mut draft = snapshot.draft();
        draft
            .set_joystick_mode(
                ProfileId::new(0),
                LayerId::new(0),
                JoystickMode::new("RADIAL")
                    .unwrap_or_else(|error| panic!("mode should be valid: {error}")),
            )
            .unwrap_or_else(|error| panic!("joystick edit should succeed: {error}"));
        let joystick = draft.document().profiles()[0].layers()[0]
            .layout()
            .joystick()
            .unwrap_or_else(|| panic!("fixture should contain a joystick"));

        assert_eq!(
            (joystick.mode().as_str(), joystick.sectors().len()),
            ("RADIAL", 0)
        );
    }

    #[test]
    fn revision_should_change_when_exact_text_changes() {
        let first = Revision::from_text("{}");
        let second = Revision::from_text("{}\n");

        assert_ne!(first, second);
    }
}