Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions near-plugins-derive/src/access_control_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ pub fn derive_access_control_role(input: TokenStream) -> TokenStream {
ident, variants, ..
} = input;

let variants = variants.into_iter().collect::<Vec<_>>();
let variant_idents = variants.into_iter().map(|v| v.ident).collect::<Vec<_>>();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a bug to not do map(|v| v.ident). The test contract used only plain variants like LevelA without comments, data or custom discriminants, which prevented the bug from surfacing.

let variant_idxs: Vec<_> =
(0..u8::try_from(variants.len()).expect("Too many enum variants")).collect();
let variant_names: Vec<_> = variants.iter().map(|v| format!("{}", v.ident)).collect();
(0..u8::try_from(variant_idents.len()).expect("Too many enum variants")).collect();
let variant_names: Vec<_> = variant_idents.iter().map(|v| format!("{}", v)).collect();

let boundchecker_type = Ident::new(DEFAULT_BOUNDCHECKER_TYPE_NAME, ident.span());
let bitflags_type_ident = new_bitflags_type_ident(Span::call_site());
Expand Down Expand Up @@ -95,7 +95,7 @@ pub fn derive_access_control_role(input: TokenStream) -> TokenStream {
fn from(value: #ident) -> Self {
match value {
#(
#ident::#variants => #variant_idxs,
#ident::#variant_idents => #variant_idxs,
)*
}
}
Expand All @@ -107,7 +107,7 @@ pub fn derive_access_control_role(input: TokenStream) -> TokenStream {
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
#(
#variant_idxs => Ok(#ident::#variants),
#variant_idxs => Ok(#ident::#variant_idents),
)*
_ => Err("Value does not correspond to a variant"),
}
Expand All @@ -118,7 +118,7 @@ pub fn derive_access_control_role(input: TokenStream) -> TokenStream {
fn from(value: #ident) -> Self {
match value {
#(
#ident::#variants => #variant_names,
#ident::#variant_idents => #variant_names,
)*
}
}
Expand All @@ -128,7 +128,7 @@ pub fn derive_access_control_role(input: TokenStream) -> TokenStream {
fn from(value: #ident) -> Self {
match value {
#(
#ident::#variants => #variant_names.to_string(),
#ident::#variant_idents => #variant_names.to_string(),
)*
}
}
Expand All @@ -140,7 +140,7 @@ pub fn derive_access_control_role(input: TokenStream) -> TokenStream {
fn try_from(value: &str) -> Result<#ident, Self::Error> {
match value {
#(
#variant_names => Ok(#ident::#variants),
#variant_names => Ok(#ident::#variant_idents),
)*
_ => Err("Value does not correspond to a variant"),
}
Expand Down
4 changes: 4 additions & 0 deletions near-plugins/tests/contracts/access_controllable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, log, near_bindgen, AccountId};
use std::collections::HashMap;

/// Roles are represented by enum variants.
#[derive(AccessControlRole, Deserialize, Serialize, Copy, Clone)]
#[serde(crate = "near_sdk::serde")]
pub enum Role {
/// Represents LevelA.
LevelA,
/// Represents LevelB.
LevelB,
/// Represents LevelC.
Comment on lines +7 to +15
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding not-so-meaningful comments to show it's now working and to avoid regressions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, we can add these comments in an enum in some of the examples to test the regression.

LevelC,
}

Expand Down