ink_ir/ir/event/
config.rs1use crate::{
16 ast,
17 utils::duplicate_config_err,
18};
19
20#[derive(Debug, PartialEq, Eq)]
22pub struct EventConfig {
23 anonymous: bool,
27
28 signature_topic_hex: Option<String>,
30}
31
32impl TryFrom<ast::AttributeArgs> for EventConfig {
33 type Error = syn::Error;
34
35 fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> {
36 let mut anonymous: Option<syn::Path> = None;
37 let mut signature_topic: Option<syn::LitStr> = None;
38 for arg in args.into_iter() {
39 if arg.name().is_ident("anonymous") {
40 if let Some(lit_bool) = anonymous {
41 return Err(duplicate_config_err(lit_bool, arg, "anonymous", "event"));
42 }
43 if let ast::Meta::Path(path) = arg {
44 anonymous = Some(path)
45 } else {
46 return Err(format_err_spanned!(
47 arg,
48 "encountered an unexpected value for `anonymous` ink! event item configuration argument. \
49 Did you mean #[ink::event(anonymous)] ?",
50 ));
51 }
52 } else if arg.name().is_ident("signature_topic") {
53 if anonymous.is_some() {
54 return Err(format_err_spanned!(
55 arg,
56 "cannot specify `signature_topic` with `anonymous` in ink! event item configuration argument",
57 ));
58 }
59
60 if let Some(lit_str) = signature_topic {
61 return Err(duplicate_config_err(
62 lit_str,
63 arg,
64 "signature_topic",
65 "event",
66 ));
67 }
68 if let Some(lit_str) = arg.value().and_then(ast::MetaValue::as_lit_string)
69 {
70 signature_topic = Some(lit_str.clone())
71 } else {
72 return Err(format_err_spanned!(
73 arg,
74 "expected a string literal value for `signature_topic` ink! event item configuration argument",
75 ));
76 }
77 } else {
78 return Err(format_err_spanned!(
79 arg,
80 "encountered unknown or unsupported ink! event item configuration argument",
81 ));
82 }
83 }
84
85 Ok(EventConfig::new(
86 anonymous.is_some(),
87 signature_topic.map(|lit_str| lit_str.value()),
88 ))
89 }
90}
91
92impl EventConfig {
93 pub fn new(anonymous: bool, signature_topic_hex: Option<String>) -> Self {
95 Self {
96 anonymous,
97 signature_topic_hex,
98 }
99 }
100
101 pub fn anonymous(&self) -> bool {
103 self.anonymous
104 }
105
106 pub fn signature_topic_hex(&self) -> Option<&str> {
108 self.signature_topic_hex.as_deref()
109 }
110}