From 96f9aa8072a2ead32248263170085c4856ec6117 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Thu, 22 Sep 2022 15:16:13 +0300 Subject: [PATCH] conf/shortcuts.rs: add key_slice() method to shortcut structs Add a new method that returns a static slice of included shortcuts. --- src/conf/shortcuts.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/conf/shortcuts.rs b/src/conf/shortcuts.rs index 45e7cbcf5..432d2a5e7 100644 --- a/src/conf/shortcuts.rs +++ b/src/conf/shortcuts.rs @@ -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 {