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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! ink! environment IR.
use ra_ap_syntax::{ast, SyntaxKind, TextRange};
use crate::{InkArg, InkArgKind};
/// A chain environment.
///
/// Ref: <https://use.ink/basics/chain-environment-types>.
#[derive(Debug, Clone)]
pub struct Environment(ast::Adt);
impl Environment {
/// Creates an environment.
pub fn new(adt: ast::Adt) -> Self {
Self(adt)
}
/// Returns the `adt` for the environment.
pub fn adt(&self) -> &ast::Adt {
&self.0
}
}
/// An ink! environment argument.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnvArg {
/// The kind of the ink! environment.
kind: EnvArgKind,
/// The ink! attribute argument for the environment.
arg: InkArg,
}
impl EnvArg {
/// Returns true if the ink! argument can be converted into an ink! environment argument.
pub fn can_cast(arg: &InkArg) -> bool {
matches!(arg.kind(), InkArgKind::Env | InkArgKind::Environment)
}
/// Converts an ink! attribute argument into an inK! environment argument.
pub fn cast(arg: InkArg) -> Option<Self> {
Self::can_cast(&arg).then(|| Self {
kind: if let Some(value) = arg.value() {
match value.kind() {
SyntaxKind::PATH => EnvArgKind::Path,
_ => EnvArgKind::Other,
}
} else {
EnvArgKind::Other
},
arg,
})
}
/// Returns the ink! attribute argument for ink! environment.
pub fn arg(&self) -> &InkArg {
&self.arg
}
/// Returns true if the value is a path.
pub fn is_path(&self) -> bool {
self.kind == EnvArgKind::Path
}
/// Converts the value if it's an integer literal (decimal or hexadecimal) into a `u32`.
pub fn as_path_with_inaccurate_text_range(&self) -> Option<ast::Path> {
self.arg.value()?.as_path_with_inaccurate_text_range()
}
/// Returns the text range of the ink! environment argument.
pub fn text_range(&self) -> TextRange {
self.arg.text_range()
}
}
// The ink! environment argument kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum EnvArgKind {
Path,
Other,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::*;
use test_utils::{quote_as_str, remove_whitespace};
#[test]
fn cast_arg_works() {
for (code, expected_kind, expected_is_path, expected_path_value) in [
// Paths are detected.
(
quote_as_str! {
#[ink(env=crate::Environment)]
},
EnvArgKind::Path,
true,
Some("crate::Environment"),
),
(
quote_as_str! {
#[ink(environment=crate::Environment)]
},
EnvArgKind::Path,
true,
Some("crate::Environment"),
),
(
quote_as_str! {
#[ink(env=external::Environment)]
},
EnvArgKind::Path,
true,
Some("external::Environment"),
),
(
quote_as_str! {
#[ink(env=Environment)]
},
EnvArgKind::Path,
true,
Some("Environment"),
),
// Non-paths are not detected as paths.
(
quote_as_str! {
#[ink(env=1)]
},
EnvArgKind::Other,
false,
None,
),
(
quote_as_str! {
#[ink(env="crate::Environment")]
},
EnvArgKind::Other,
false,
None,
),
(
quote_as_str! {
#[ink(env=_)]
},
EnvArgKind::Other,
false,
None,
),
(
quote_as_str! {
#[ink(env=struct)]
},
EnvArgKind::Other,
false,
None,
),
] {
// Parse ink! environment argument.
let selector_arg = EnvArg::cast(
parse_first_ink_attribute(code)
.args()
.iter()
.find(|arg| matches!(arg.kind(), InkArgKind::Env | InkArgKind::Environment))
.unwrap()
.clone(),
)
.unwrap();
assert_eq!(selector_arg.kind, expected_kind);
assert_eq!(selector_arg.is_path(), expected_is_path);
assert_eq!(
selector_arg
.as_path_with_inaccurate_text_range()
.map(|path| remove_whitespace(path.to_string()))
.as_deref(),
expected_path_value
);
}
}
}