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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use crate::{
ra_ap_syntax::{
ast::{self, Expr, HasName, NameOrNameRef},
AstNode,
},
semver::{SemVerError, Version as SemverVersion},
utils::{normalize, INTERNAL_ERR},
Semantics, Upgrader,
};
use anyhow::Result as AnyResult;
use paste::paste;
use std::{collections::HashMap as Map, ops::Deref};
pub(crate) type Hook<T> = Box<dyn Fn(&mut Upgrader, &T, &Semantics)>;
pub(crate) struct Hooks<T>(Map<String, Map<String, Vec<Hook<T>>>>);
impl<T> Default for Hooks<T> {
fn default() -> Self {
Self(Map::new())
}
}
impl<T> Deref for Hooks<T> {
type Target = Map<String, Map<String, Vec<Hook<T>>>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> Hooks<T> {
pub(crate) fn insert(&mut self, path: &str, name: &str, hook: Hook<T>) {
let path = path.to_string();
let name = name.to_string();
if !self.0.contains_key(&path) {
self.0.insert(path.clone(), Map::new());
}
let hook_map = self.0.get_mut(&path).expect(INTERNAL_ERR);
if !hook_map.contains_key(&name) {
hook_map.insert(name.clone(), vec![]);
}
hook_map.get_mut(&name).expect(INTERNAL_ERR).push(hook);
}
}
macro_rules! members {
($($node:ident,)*) => {
paste! {
pub struct Version {
pub(crate) version: SemverVersion,
pub(crate) peers: Vec<String>,
pub(crate) init: Option<Box<dyn Fn(&mut Upgrader, &SemverVersion) -> AnyResult<()>>>,
$(
pub(crate) [<hook_ $node:snake>]: Vec<Hook<ast::$node>>,
pub(crate) [<hook_ $node:snake _on>]: Hooks<ast::$node>,
)*
}
impl Version {
pub fn new(version: &str) -> Result<Self, SemVerError> {
Ok(Self {
version: SemverVersion::parse(version)?,
peers: vec![],
init: None,
$(
[<hook_ $node:snake>]: Vec::new(),
[<hook_ $node:snake _on>]: Hooks::default(),
)*
})
}
}
}
};
}
macro_rules! methods {
($($node:ident,)*) => {
paste! {
$(
pub fn [<hook_ $node:snake>]<F>(mut self, f: F) -> Self
where
F: Fn(&mut Upgrader, &ast::$node, &Semantics) + 'static,
{
self.[<hook_ $node:snake>].push(Box::new(f));
self
}
pub fn [<hook_ $node:snake _on>]<F>(mut self, path: &str, name: &str, f: F) -> Self
where
F: Fn(&mut Upgrader, &ast::$node, &Semantics) + 'static
{
self.[<hook_ $node:snake _on>].insert(path, name, Box::new(f));
self
}
)*
}
};
}
members!(
MethodCallExpr,
CallExpr,
IdentPat,
Path,
PathExpr,
PathPat,
FieldExpr,
RecordPat,
RecordExpr,
RecordExprField,
RecordPatField,
TupleStructPat,
);
impl Version {
pub fn peers(mut self, peers: &[&str]) -> Self {
self.peers = peers.to_vec().iter().map(|x| normalize(*x)).collect();
self
}
pub fn init<F>(mut self, init: F) -> Self
where
F: Fn(&mut Upgrader, &SemverVersion) -> AnyResult<()> + 'static,
{
self.init = Some(Box::new(init));
self
}
pub fn rename_structs(mut self, name: &str, map: &'static [[&str; 2]]) -> Self {
for rename in map.into_iter() {
self = self.hook_path_on(name, rename[0], move |u, n, _| {
u.replace(n.segment(), rename[1]);
})
}
self
}
pub fn rename_methods(mut self, name: &str, map: &'static [[&str; 2]]) -> Self {
for rename in map.into_iter() {
self = self
.hook_method_call_expr_on(name, rename[0], move |u, n, _| {
u.replace(n.name_ref(), rename[1]);
})
.hook_path_expr_on(name, rename[0], move |u, n, _| {
u.replace(n.path(), rename[1]);
});
}
self
}
pub fn rename_members(mut self, name: &str, map: &'static [[&str; 2]]) -> Self {
for rename in map.into_iter() {
self = self
.hook_field_expr_on(name, rename[0], move |u, n, _| {
u.replace(n.name_ref(), rename[1]);
})
.hook_record_pat_field_on(name, rename[0], move |u, n, _| match n.field_name() {
Some(NameOrNameRef::Name(_)) => {
u.replace(n.syntax().text_range(), format!("{}: {}", rename[1], n))
}
Some(NameOrNameRef::NameRef(name_ref)) => {
u.replace(name_ref.syntax().text_range(), rename[1])
}
_ => {}
})
.hook_record_expr_field_on(name, rename[0], move |u, n, _| {
if let Some(name_ref) = n.name_ref() {
u.replace(name_ref.syntax().text_range(), rename[1]);
} else if let Some(Expr::PathExpr(path_expr)) = n.expr() {
u.replace(
path_expr.syntax().text_range(),
format!("{}: {}", rename[1], rename[0]),
);
}
});
}
self
}
pub fn rename_variants(mut self, name: &str, map: &'static [[&str; 2]]) -> Self {
for rename in map.into_iter() {
self = self
.hook_path_expr_on(name, rename[0], move |u, n, _| {
u.replace(n.path(), rename[1]);
})
.hook_path_pat_on(name, rename[0], move |u, n, _| {
u.replace(n.path(), rename[1]);
})
.hook_record_pat_on(name, rename[0], move |u, n, _| {
u.replace(n.path(), rename[1]);
})
.hook_record_expr_on(name, rename[0], move |u, n, _| {
u.replace(n.path(), rename[1]);
})
.hook_tuple_struct_pat_on(name, rename[0], move |u, n, _| {
u.replace(n.path(), rename[1]);
})
.hook_ident_pat_on(name, rename[0], move |u, n, _| {
u.replace(n.name(), rename[1]);
});
}
self
}
}
impl Version {
methods!(
MethodCallExpr,
CallExpr,
IdentPat,
Path,
PathExpr,
PathPat,
FieldExpr,
RecordPat,
RecordExpr,
RecordExprField,
RecordPatField,
TupleStructPat,
);
}