| Crates.io | keymap_derive |
| lib.rs | keymap_derive |
| version | 1.0.0-rc.3 |
| created_at | 2025-06-10 17:28:08.340355+00 |
| updated_at | 2025-07-11 16:29:26.381575+00 |
| description | A derive macro to generate compile-time validated key mappings for enums, enabling declarative and ergonomic key binding support. |
| homepage | |
| repository | https://github.com/rezigned/keymap-rs |
| max_upload_size | |
| id | 1707447 |
| size | 25,040 |
This crate provides a derive macro #[derive(KeyMap)] for Rust enums to easily generate implementations for:
TryFrom<KeyMap> and TryFrom<Vec<KeyMap>>: Allowing conversion from parsed key events to your enum variants.KeyMapConfig<YourEnum>: Providing the derived keybindings as default configurations.This is part of the keymap-rs workspace.
Add keymap with the derive feature to your Cargo.toml:
[dependencies]
keymap = { version = "0.5", features = ["derive"] } # Check for the latest version
Then, use the KeyMap derive macro on your enum:
use keymap::KeyMap; // For TryFrom<KeyMap> and parse_keymap
use keymap_derive::KeyMap; // For the derive macro itself
#[derive(Debug, PartialEq, KeyMap)]
enum Action {
#[key("c", description = "Create an item")]
Create,
#[key("d", "del", description = "Delete an item")]
Delete,
#[key("q", "ctrl-c", description = "Quit the application")]
Quit,
}
fn main() {
// Example: Parsing a key string and converting to Action
// In a real application, KeyMap would come from an event loop or config.
let keymap_create = keymap::parse_keymap("c").unwrap();
let action_create = Action::try_from(keymap_create).unwrap();
assert_eq!(action_create, Action::Create);
let keymap_delete_del = keymap::parse_keymap("del").unwrap();
let action_delete_del = Action::try_from(keymap_delete_del).unwrap();
assert_eq!(action_delete_del, Action::Delete);
println!("Successfully derived and used KeyMap!");
// The derive also generates KeyMapConfig<Action>
// For more details on how this is used with DerivedConfig
// to allow user overrides, please see the main [keymap-rs documentation](../../README.md#advanced-customizing-keymaps-with-derivedconfig).
}
#[key("key1", "key2", ..., description = "Human-readable description")]:
description is optional but recommended, especially when using DerivedConfig as it will be part of the default Item generated.For more advanced usage, including how these derived defaults can be customized by users with DerivedConfig, please see the main keymap-rs README.