Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
simpligy argument parsing
  • Loading branch information
SkymanOne committed Dec 21, 2023
commit ac0d28c3ae7bd0d908b456f9ef65a5c3288ea9b9
5 changes: 2 additions & 3 deletions crates/ink/codegen/src/generator/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ impl GenerateCode for Event<'_> {

impl Event<'_> {
fn generate_signature_topic(&self) -> TokenStream2 {
let signature_topic = if let Some(bytes) = self.item.signature_topic() {
let hash_string = hex::encode(bytes);
let signature_topic = if let Some(hash) = self.item.signature_topic_hash() {
quote! {
#[::ink::signature_topic(hash = #hash_string)]
#[::ink::signature_topic(hash = #hash)]
}
} else if self.item.anonymous() {
quote! {}
Expand Down
33 changes: 17 additions & 16 deletions crates/ink/ir/src/ir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ use crate::{
},
};

use super::SignatureTopicArg;

/// An extension trait for [`syn::Attribute`] in order to query for documentation.
pub trait IsDocAttribute {
/// Returns `true` if the attribute is a Rust documentation attribute.
Expand Down Expand Up @@ -287,10 +285,10 @@ impl InkAttribute {
}

/// Returns the signature topic of the ink! attribute if any.
pub fn signature_topic(&self) -> Option<SignatureTopicArg> {
pub fn signature_topic_hash(&self) -> Option<String> {
self.args().find_map(|arg| {
if let ir::AttributeArg::SignatureTopic(topic) = arg.kind() {
return Some(*topic);
if let ir::AttributeArg::SignatureTopic(hash) = arg.kind() {
return Some(hash.clone());
}
None
})
Expand Down Expand Up @@ -433,7 +431,7 @@ pub enum AttributeArg {
/// that is invoked if no other ink! message matches a given selector.
Selector(SelectorOrWildcard),
/// `#[ink(signature_topic = "DEADBEEF")]`
SignatureTopic(SignatureTopicArg),
SignatureTopic(String),
/// `#[ink(namespace = "my_namespace")]`
///
/// Applied on ink! trait implementation blocks to disambiguate other trait
Expand Down Expand Up @@ -525,8 +523,8 @@ impl core::fmt::Display for AttributeArg {
Self::Constructor => write!(f, "constructor"),
Self::Payable => write!(f, "payable"),
Self::Selector(selector) => core::fmt::Display::fmt(&selector, f),
Self::SignatureTopic(sig_topic) => {
write!(f, "signature_topic = {:?}", sig_topic.signature_topic())
Self::SignatureTopic(hash) => {
write!(f, "signature_topic = {:?}", hash)
}
Self::Function(function) => {
write!(f, "function = {:?}", function.into_u16())
Expand Down Expand Up @@ -944,14 +942,20 @@ impl Parse for AttributeFrag {
SelectorOrWildcard::try_from(&name_value.value)
.map(AttributeArg::Selector)
}
"signature_topic" => {
SignatureTopicArg::try_from(&name_value.value)
.map(AttributeArg::SignatureTopic)
}
"namespace" => {
Namespace::try_from(&name_value.value)
.map(AttributeArg::Namespace)
}
"signature_topic" => {
if let Some(hash) = name_value.value.as_string() {
Ok(AttributeArg::SignatureTopic(hash))
} else {
Err(format_err_spanned!(
name_value.value,
"expected String type for `S` in #[ink(signature_topic = S)]",
))
}
}
"function" => {
if let Some(lit_int) = name_value.value.as_lit_int() {
let id = lit_int.base10_parse::<u16>()
Expand Down Expand Up @@ -1537,15 +1541,12 @@ mod tests {
}
#[test]
fn signature_topic_works() {
let bytes = [17u8; 32];
let s = "11".repeat(32);
assert_attribute_try_from(
syn::parse_quote! {
#[ink(signature_topic = #s)]
},
Ok(test::Attribute::Ink(vec![AttributeArg::SignatureTopic(
SignatureTopicArg::from(&bytes),
)])),
Ok(test::Attribute::Ink(vec![AttributeArg::SignatureTopic(s)])),
);
}
}
34 changes: 17 additions & 17 deletions crates/ink/ir/src/ir/event/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use syn::spanned::Spanned;

use crate::{
ast,
utils::duplicate_config_err,
};

use super::SignatureTopicArg;

/// The configuration arguments to the `#[ink::event(..)]` attribute macro.
#[derive(Debug, PartialEq, Eq)]
pub struct EventConfig {
Expand All @@ -29,16 +25,16 @@ pub struct EventConfig {
/// This is the default value.
anonymous: bool,

/// Manually specified signature topic.
signature_topic: Option<[u8; 32]>,
/// Manually specified signature topic hash.
signature_topic_hash: Option<String>,
}

impl TryFrom<ast::AttributeArgs> for EventConfig {
type Error = syn::Error;

fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> {
let mut anonymous: Option<syn::LitBool> = None;
let mut signature_topic: Option<SignatureTopicArg> = None;
let mut signature_topic: Option<syn::LitStr> = None;
for arg in args.into_iter() {
if arg.name.is_ident("anonymous") {
if let Some(lit_bool) = anonymous {
Expand All @@ -60,13 +56,17 @@ impl TryFrom<ast::AttributeArgs> for EventConfig {
));
}

if signature_topic.is_some() {
return Err(format_err!(
arg.span(),
"encountered duplicate ink! configuration argument"
if let Some(lit_str) = signature_topic {
return Err(duplicate_config_err(lit_str, arg, "anonymous", "event"));
}
if let ast::MetaValue::Lit(syn::Lit::Str(lis_str)) = &arg.value {
signature_topic = Some(lis_str.clone())
} else {
return Err(format_err_spanned!(
arg,
"expected a bool literal for `anonymous` ink! event item configuration argument",
));
}
signature_topic = Some(SignatureTopicArg::try_from(&arg.value)?);
} else {
return Err(format_err_spanned!(
arg,
Expand All @@ -77,17 +77,17 @@ impl TryFrom<ast::AttributeArgs> for EventConfig {

Ok(EventConfig::new(
anonymous.map(|lit_bool| lit_bool.value).unwrap_or(false),
signature_topic,
signature_topic.map(|lit_str| lit_str.value()),
))
}
}

impl EventConfig {
/// Construct a new [`EventConfig`].
pub fn new(anonymous: bool, signature_topic: Option<SignatureTopicArg>) -> Self {
pub fn new(anonymous: bool, signature_topic_hash: Option<String>) -> Self {
Self {
anonymous,
signature_topic: signature_topic.map(|s| s.signature_topic()),
signature_topic_hash,
}
}

Expand All @@ -97,7 +97,7 @@ impl EventConfig {
}

/// Returns the manually specified signature topic.
pub fn signature_topic(&self) -> Option<[u8; 32]> {
self.signature_topic
pub fn signature_topic_hash(&self) -> Option<String> {
self.signature_topic_hash.clone()
}
}
15 changes: 6 additions & 9 deletions crates/ink/ir/src/ir/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ use crate::{
utils::extract_cfg_attributes,
};

pub use signature_topic::{
SignatureTopic,
SignatureTopicArg,
};
pub use signature_topic::SignatureTopic;

/// A checked ink! event with its configuration.
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -101,13 +98,13 @@ impl Event {
self.config.anonymous()
}

/// Return manually specified signature topic.
/// Return manually specified signature topic hash.
///
/// # Note
///
/// Conflicts with `anonymous`
pub fn signature_topic(&self) -> Option<[u8; 32]> {
self.config.signature_topic()
pub fn signature_topic_hash(&self) -> Option<String> {
self.config.signature_topic_hash()
}

/// Returns a list of `cfg` attributes if any.
Expand Down Expand Up @@ -142,7 +139,7 @@ impl TryFrom<syn::ItemStruct> for Event {
}
},
)?;
if ink_attrs.is_anonymous() && ink_attrs.signature_topic().is_some() {
if ink_attrs.is_anonymous() && ink_attrs.signature_topic_hash().is_some() {
return Err(format_err_spanned!(
item_struct,
"cannot use use `anonymous` with `signature_topic`",
Expand All @@ -155,7 +152,7 @@ impl TryFrom<syn::ItemStruct> for Event {
},
config: EventConfig::new(
ink_attrs.is_anonymous(),
ink_attrs.signature_topic(),
ink_attrs.signature_topic_hash(),
),
})
}
Expand Down
1 change: 0 additions & 1 deletion crates/ink/ir/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ pub use self::{
event::{
Event,
SignatureTopic,
SignatureTopicArg,
},
ink_test::InkTest,
item::{
Expand Down