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
use std::fmt::Display;
use syn::Ident;
pub trait IdentExt {
fn append<S>(&self, suffix: S) -> Ident
where
S: Display;
fn prepend<S>(&self, prefix: S) -> Ident
where
S: Display;
}
impl IdentExt for Ident {
fn append<S>(&self, suffix: S) -> Ident
where
S: Display,
{
let appended = format!("{}{}", self, suffix);
Ident::new(&appended, self.span())
}
fn prepend<S>(&self, suffix: S) -> Ident
where
S: Display,
{
let prepended = format!("{}{}", suffix, self);
Ident::new(&prepended, self.span())
}
}
#[cfg(test)]
mod tests {
use proc_macro2::Span;
use syn::Ident;
use super::IdentExt;
#[test]
fn append_str_returns_appended_ident() {
let one = Ident::new("One", Span::call_site());
assert_eq!(Ident::new("OneTwo", Span::call_site()), one.append("Two"));
}
#[test]
fn append_ident_returns_appended_ident() {
let one = Ident::new("One", Span::call_site());
let two = Ident::new("Two", Span::call_site());
assert_eq!(Ident::new("OneTwo", Span::call_site()), one.append(two));
}
#[test]
fn append_ident_ref_returns_appended_ident() {
let one = Ident::new("One", Span::call_site());
let two = Ident::new("Two", Span::call_site());
assert_eq!(Ident::new("OneTwo", Span::call_site()), one.append(&two));
}
#[test]
fn prepend_str_returns_prepended_ident() {
let one = Ident::new("One", Span::call_site());
assert_eq!(Ident::new("TwoOne", Span::call_site()), one.prepend("Two"));
}
#[test]
fn prepend_ident_returns_prepended_ident() {
let one = Ident::new("One", Span::call_site());
let two = Ident::new("Two", Span::call_site());
assert_eq!(Ident::new("TwoOne", Span::call_site()), one.prepend(two));
}
#[test]
fn prepend_ident_ref_returns_prepended_ident() {
let one = Ident::new("One", Span::call_site());
let two = Ident::new("Two", Span::call_site());
assert_eq!(Ident::new("TwoOne", Span::call_site()), one.prepend(&two));
}
}