1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
//! ink! message IR.
use ink_analyzer_macro::{FromInkAttribute, FromSyntax};
use ra_ap_syntax::ast;
use crate::traits::{FromInkAttribute, FromSyntax, IsInkCallable, IsInkFn};
use crate::{InkAttrData, InkAttribute};
/// An ink! message.
#[derive(Debug, Clone, PartialEq, Eq, FromInkAttribute, FromSyntax)]
pub struct Message {
/// ink! attribute IR data.
#[arg_kind(Message)]
ink_attr: InkAttrData<ast::Fn>,
}
impl IsInkFn for Message {
fn fn_item(&self) -> Option<&ast::Fn> {
self.ink_attr.parent_ast()
}
}
impl IsInkCallable for Message {}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
use test_utils::quote_as_str;
#[test]
fn cast_works() {
for (code, is_payable, has_selector, is_default) in [
(
quote_as_str! {
#[ink(message)]
pub fn my_message(&self) {}
},
false,
false,
false,
),
(
quote_as_str! {
#[ink(message, payable, default, selector=1)]
pub fn my_message(&self) {}
},
true,
true,
true,
),
(
quote_as_str! {
#[ink(message)]
#[ink(payable, default, selector=1)]
pub fn my_message(&self) {}
},
true,
true,
true,
),
(
quote_as_str! {
#[ink(message)]
#[ink(payable)]
#[ink(default)]
#[ink(selector=1)]
pub fn my_message(&self) {}
},
true,
true,
true,
),
(
quote_as_str! {
#[ink(message, selector=0xA)]
pub fn my_message(&self) {}
},
false,
true,
false,
),
(
quote_as_str! {
#[ink(message, selector=_)]
pub fn my_message(&self) {}
},
false,
true,
false,
),
] {
let ink_attr = parse_first_ink_attribute(code);
let message = Message::cast(ink_attr).unwrap();
// `payable` argument exists.
assert_eq!(message.payable_arg().is_some(), is_payable);
// `selector` argument exists.
assert_eq!(message.selector_arg().is_some(), has_selector);
// `default` argument exists.
assert_eq!(message.default_arg().is_some(), is_default);
// composed selector exists.
assert!(message.composed_selector().is_some());
// `fn` item exists.
assert!(message.fn_item().is_some());
}
}
}