conf/shortcuts.rs: add key_slice() method to shortcut structs

Add a new method that returns a static slice of included shortcuts.
feature/perform-shortcut
Manos Pitsidianakis 2022-09-22 15:16:13 +03:00
parent 0ef4dde939
commit 96f9aa8072
1 changed files with 17 additions and 2 deletions

View File

@ -86,8 +86,7 @@ macro_rules! shortcut_key_values {
pub struct $name:ident { $($fname:ident |> $fdesc:literal |> $default:expr),* }) => {
$(#[$outer])*
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
#[serde(rename = $cname)]
#[serde(default, deny_unknown_fields, rename = $cname)]
pub struct $name {
$(pub $fname : Key),*
}
@ -100,12 +99,28 @@ macro_rules! shortcut_key_values {
_ => unreachable!()
}
}
/// Returns a hashmap of all shortcuts and their values
pub fn key_values(&self) -> IndexMap<&'static str, Key> {
[
$((stringify!($fname),(self.$fname).clone()),)*
].iter().cloned().collect()
}
/// Returns a slice of all shortcuts.
pub fn key_slice(&self) -> &'static [&'static str] {
use std::sync::Once;
static mut VAL: Vec<&'static str> = vec![];
static INIT: Once = Once::new();
unsafe {
INIT.call_once(|| {
$(VAL.push(stringify!($fname));)*
});
VAL.as_ref()
}
}
}
impl Default for $name {