tools: change constnamemap to a more automatic constnamedbitmap

We only used this for the privileges for now, and there it's a
nuisance to alter all bit definitions manually if something is added.

This change makes it count the bits up automatically.

Rename the macro to indicate that this is not a generic name map but
a more specific named bit mapping.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2020-10-06 12:08:52 +02:00 committed by Dietmar Maurer
parent 19dc729b7b
commit 82172720a4
2 changed files with 31 additions and 22 deletions

View File

@ -1,19 +1,23 @@
/// A macro to generate a list of pub const variabales and /// A macro to generate a list of pub const variabales, and
/// an accompaning static array of a given name + value /// an accompaning static array of a given name, the values are automatically
/// (with doc comments) /// assigned to a bit (with doc comments)
/// ///
/// Example: /// Example:
/// ``` /// ```
/// # use proxmox::constnamemap; /// # use proxmox::constnamedbitmap;
/// ///
/// constnamemap! { /// constnamedbitmap! {
/// /// A list of privileges /// /// A list of privileges
/// PRIVS: u64 => { /// PRIVS: u64 => {
/// /// Some comment for Priv1 /// /// Some comment for Priv1
/// PRIV1("Priv1") = 1; /// PRIV1("Priv1");
/// PRIV2("Priv2") = 2; /// PRIV2("Priv2");
/// PRIV3("Priv3");
/// } /// }
/// } /// }
/// # assert!(PRIV1 == 1<<0);
/// # assert!(PRIV2 == 1<<1);
/// # assert!(PRIV3 == 1<<2);
/// ``` /// ```
/// ///
/// this will generate the following variables: /// this will generate the following variables:
@ -21,15 +25,17 @@
/// /// Some comment for Priv1 /// /// Some comment for Priv1
/// pub const PRIV1: u64 = 1; /// pub const PRIV1: u64 = 1;
/// pub const PRIV2: u64 = 2; /// pub const PRIV2: u64 = 2;
/// pub const PRIV3: u64 = 4;
/// ///
/// /// A list of privileges /// /// A list of privileges
/// pub const PRIVS: &[(&str, u64)] = &[ /// pub const PRIVS: &[(&str, u64)] = &[
/// ("Priv1", 1), /// ("Priv1", PRIV1),
/// ("Priv2", 2), /// ("Priv2", PRIV2),
/// ("Priv3", PRIV3),
/// ]; /// ];
/// ``` /// ```
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! constnamemap { macro_rules! constnamedbitmap {
( (
$(#[$outer:meta])* $(#[$outer:meta])*
$name:ident : $type:ty => { $name:ident : $type:ty => {
@ -37,7 +43,7 @@ macro_rules! constnamemap {
} }
) => { ) => {
__constnamemap_consts! { __constnamemap_consts! {
$type => $($content)+ ($type) (0) => $($content)+
} }
$(#[$outer])* $(#[$outer])*
@ -51,17 +57,20 @@ macro_rules! constnamemap {
#[doc(hidden)] #[doc(hidden)]
#[macro_export(local_inner_macros)] #[macro_export(local_inner_macros)]
macro_rules! __constnamemap_consts { macro_rules! __constnamemap_consts {
(($type:ty) ($counter:expr) => ) => {};
( (
$type:ty => ($type:ty) ($counter:expr) =>
$(#[$outer:meta])*
$name:ident($text:expr);
$( $(
$(#[$outer:meta])* $content:tt
$name:ident($text:expr) = $value:expr; )*
)+
) => { ) => {
$( $(#[$outer])*
$(#[$outer])* pub const $name: $type = 1 << ($counter);
pub const $name: $type = $value; __constnamemap_consts! {
)+ ($type) (1+$counter) => $($content)*
}
} }
} }
@ -71,11 +80,11 @@ macro_rules! __constnamemap_entries {
( (
$( $(
$(#[$outer:meta])* $(#[$outer:meta])*
$name:ident($text:expr) = $value:expr; $name:ident($text:expr);
)* )*
) => { ) => {
&[ &[
$(($text,$value),)* $(($text,$name),)*
] ]
} }
} }

View File

@ -9,7 +9,7 @@ pub mod as_any;
pub mod borrow; pub mod borrow;
pub mod byte_buffer; pub mod byte_buffer;
pub mod common_regex; pub mod common_regex;
pub mod constnamemap; pub mod constnamedbitmap;
pub mod email; pub mod email;
pub mod fd; pub mod fd;
pub mod fs; pub mod fs;