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
//! # `KeyMap` Library
//!
//! This library provides a unified abstraction over key input representations from multiple backends,
//! such as [`crossterm`](https://crates.io/crates/crossterm), WebAssembly environments, and more.
//! It enables parsing, transforming, and serializing key input events to and from a common `KeyMap`
//! format, which is represented by a node tree from the `keymap_parser` crate.
//!
//! The main goal is to decouple application logic from backend-specific input handling, enabling easier
//! testing, configuration, and cross-platform support.
use ;
/// A type alias for a parsed keymap node tree.
///
/// This represents a keymap in an abstract format, using the [`Node`] type
/// from the `keymap_parser` crate.
pub type KeyMap = Node;
/// A trait for converting a [`KeyMap`] into a backend-specific key event type.
///
/// This trait should be implemented by types such as `crossterm::event::KeyEvent` or other
/// platform-native key event types. It allows translating a backend-agnostic keymap (typically
/// parsed from user configuration) into a format usable by a specific input backend.
///
/// # Errors
///
/// Returns [`Error::Parse`] if the `KeyMap` is invalid, or [`Error::UnsupportedKey`] if it
/// contains keys or structures not supported by the target backend.
/// A trait for converting a backend-specific key event into a [`KeyMap`].
///
/// This trait should be implemented by types that represent native input events
/// from a backend, such as `crossterm`, web (WASM), or others. It provides a way
/// to unify platform-specific key events into a common `KeyMap` representation.
///
/// # Errors
///
/// Returns an [`Error`] if the conversion fails due to an unsupported or invalid key.
/// A trait for converting a backend-specific key type into a [`KeyMap`].
///
/// This is typically implemented by types like `crossterm::event::KeyEvent`,
/// allowing the transformation of native input representations into the
/// abstract `KeyMap` format. This is useful for tasks such as exporting key
/// configurations, logging, or interfacing with cross-platform logic.
///
/// # Errors
///
/// Returns an [`Error`] if the conversion fails due to unsupported or unrepresentable keys.
/// A trait for types that can be extracted from a matched key group node.
///
/// When a variant field is bound via a key group (e.g. `@digit`, `@any`), the
/// derive macro calls `KeyGroupValue::from_keymap_node` on the matched [`KeyMap`]
/// node to produce the field value. This replaces the old string-based type
/// inspection, so type aliases (e.g. `type Bar = u32`) work transparently as
/// long as the underlying type implements this trait.
///
/// # Built-in implementations
///
/// | Type | Behaviour |
/// |---------|------------------------------------------------------------|
/// | `char` | Returns the matched character, or `'\0'` as the default. |
/// | `u8` | Parses the digit character as a decimal number. |
/// | `u16` | Same as `u8`, widened to `u16`. |
/// | `u32` | Same as `u8`, widened to `u32`. |
/// | `u64` | Same as `u8`, widened to `u64`. |
/// | `usize` | Same as `u8`, widened to `usize`. |
///
/// # Example
///
/// ```ignore
/// use keymap::KeyGroupValue;
///
/// type MyDigit = u32;
///
/// #[derive(keymap::KeyMap)]
/// enum Action {
/// #[key("@digit")]
/// Count(MyDigit), // works because u32 implements KeyGroupValue
/// }
/// ```
impl_key_group_value_uint!;
/// Represents errors that can occur during keymap parsing or conversion.