-
Notifications
You must be signed in to change notification settings - Fork 3
/
experimental-templates.toml
56 lines (55 loc) · 2.11 KB
/
experimental-templates.toml
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
# Those templates are expected to demand significant adjustments to modified code.
# They should probably not be part of an unsupervised workflow (e.g. CI).
[if-continue-filter-neg]
match='for :[var] in :[iter] { if !:[cond] { continue; } :[rest] }'
rewrite='for :[var] in :[iter].iter().filter(|:[var]| :[cond]) {:[rest]}'
[if-let-ok-map]
match='if let Ok(:[var]) = :[opt] { Ok(:[out]) } else { Err(:[e]) }'
rewrite=':[opt].map_or_else(|| :[e], |:[var]| { :[out] })'
rule='where :[var] != :[out]'
[map-unwrap]
match='.map(:[map_pre].unwrap()):[sp].collect:[typ]():[end\n]'
rewrite='.map(:[map_pre]):[sp].collect::<Result<_, _>>()?:[end]'
[runwrap-or-else-backward]
match='match :[target] { Ok(:[var]) => :[var], Err(:[e]) => :[foo], }'
rewrite=':[target].unwrap_or_else(|:[e]| { :[foo] })'
[runwrap-or-else-forward]
match='match :[target] { Err(:[e]) => :[foo], Ok(:[var]) => :[var], }'
rewrite=':[target].unwrap_or_else(|:[e]| { :[foo] })'
[unwrap-or-else-backward]
match='match :[target] { Some(:[var]) => :[var], None => :[foo], }'
rewrite=':[target].unwrap_or_else(|| { :[foo] })'
rule='''
where match :[foo] {
| ":[_~return .+]" -> false
| ":[_]" -> true
}
'''
[unwrap-or-else-forward]
match='match :[target] { None => :[foo], Some(:[var]) => :[var], }'
rewrite=':[target].unwrap_or_else(|| { :[foo] })'
rule='''
where match :[foo] {
| ":[_~return .+]" -> false
| ":[_]" -> true
}
'''
[zero-range]
match=':[[s]][0..:[[end]]]'
rewrite=':[[s]][..:[[end]]]'
# Those do not work on Iterator traits, but may work on arrays, vecs ...
[iter-any-equals-right]
match='.any(|:[var]| :[var] == :[name])'
rewrite='.contains(:[name])'
[iter-any-equals-left]
match='.any(|:[var]| :[name] == :[var])'
rewrite='.contains(:[name])'
[if-then-some-forward]
match='if :[cond] { Some(:[foo]) } else { None }'
rewrite='(:[cond]).then_some(:[foo])'
[if-then-some-backward]
match='if :[cond] { None } else { Some(:[foo]) }'
rewrite='(!:[cond]).then_some(:[foo])'
[if-let-else-return]
match='if let :[foo](:[pat]) = :[assign] { :[cond1] } else { return :[cond] }'
rewrite='let :[foo](:[pat]) = :[assign] else { return :[cond] }; :[cond1]'