Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! This crates provides a single, easy to use macro, that allows you to actually use concatenated
2 //! identifiers in Rust.
3
4 extern crate proc_macrorust-analyzer cargo concat-idents 1.1.5 proc_macro/;
5
6 use proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#;
7
8 use quoterust-analyzer cargo quote 1.0.47 crate/::quoterust-analyzer cargo quote 1.0.47 quote!;
9 use synrust-analyzer cargo syn 2.0.119 crate/::{
10 Blockrust-analyzer cargo syn 2.0.119 stmt/Block#, Identrust-analyzer cargo proc-macro2 1.0.107 Ident#, LitBoolrust-analyzer cargo syn 2.0.119 lit/LitBool#, LitByterust-analyzer cargo syn 2.0.119 lit/LitByte#, LitByteStrrust-analyzer cargo syn 2.0.119 lit/LitByteStr#,
11 LitCharrust-analyzer cargo syn 2.0.119 lit/LitChar#, LitFloatrust-analyzer cargo syn 2.0.119 lit/LitFloat#, LitIntrust-analyzer cargo syn 2.0.119 lit/LitInt#, LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#, parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/, Tokenrust-analyzer cargo syn 2.0.119 token/Token!, visit_mutrust-analyzer cargo syn 2.0.119 gen/visit_mut/,
12 };
13 use synrust-analyzer cargo syn 2.0.119 crate/::parserust-analyzer cargo syn 2.0.119 parse/::{selfrust-analyzer cargo syn 2.0.119 parse/, Parserust-analyzer cargo syn 2.0.119 parse/Parse#, ParseStreamrust-analyzer cargo syn 2.0.119 parse/ParseStream#};
14 use synrust-analyzer cargo syn 2.0.119 crate/::spannedrust-analyzer cargo syn 2.0.119 spanned/::Spannedrust-analyzer cargo syn 2.0.119 spanned/Spanned#;
15 use synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Underscorerust-analyzer cargo syn 2.0.119 token/Underscore#;
16 use synrust-analyzer cargo syn 2.0.119 crate/::visit_mutrust-analyzer cargo syn 2.0.119 gen/visit_mut/::VisitMutrust-analyzer cargo syn 2.0.119 gen/visit_mut/VisitMut#;
17
18 #[cfg(test)]
19 mod testsrust-analyzer cargo concat-idents 1.1.5 tests/ {
20 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
21 fn testrust-analyzer cargo concat-idents 1.1.5 tests/test().() {
22 let t = trybuildrust-analyzer cargo trybuild 1.0.120 crate/::TestCasesrust-analyzer cargo trybuild 1.0.120 TestCases#::newrust-analyzer cargo trybuild 1.0.120 impl#[TestCases]new().();
23 t.passrust-analyzer cargo trybuild 1.0.120 impl#[TestCases]pass().("tests/pass.rs");
24 t.compile_failrust-analyzer cargo trybuild 1.0.120 impl#[TestCases]compile_fail().("tests/fail/*.rs");
25 }
26 }
27
28 /// A helper struct that implements [`Parse`] and extracts the `replace_ident`, the `concatenated_ident` and
29 /// the code `block` from the original macro input
30 /// ```text
31 /// concat_idents!(
32 /// ident = ident1, _, ident2 { /* code */ }
33 /// ^^^^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
34 /// | | code block
35 /// | |
36 /// | concatenated-ident (in this case: 'ident1_ident2')
37 /// |
38 /// replace-ident
39 /// );
40 /// ```
41 struct InputParserrust-analyzer cargo concat-idents 1.1.5 InputParser# {
42 replace_identrust-analyzer cargo concat-idents 1.1.5 InputParser#replace_ident.: Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
43 concatenated_identrust-analyzer cargo concat-idents 1.1.5 InputParser#concatenated_ident.: Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
44 blockrust-analyzer cargo concat-idents 1.1.5 InputParser#block.: Blockrust-analyzer cargo syn 2.0.119 stmt/Block#,
45 }
46
47 impl Parserust-analyzer cargo syn 2.0.119 parse/Parse# for InputParserrust-analyzer cargo concat-idents 1.1.5 InputParser# {
48 fn parserust-analyzer cargo concat-idents 1.1.5 impl#[InputParser][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.119 parse/ParseStream#) -> parserust-analyzer cargo syn 2.0.119 parse/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<Selfrust-analyzer cargo concat-idents 1.1.5 impl#[InputParser][Parse]> {
49 let replace_ident: Identrust-analyzer cargo proc-macro2 1.0.107 Ident# = input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
50 let _: Tokenrust-analyzer cargo syn 2.0.119 token/Token!![=] = input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
51 let IdentParserrust-analyzer cargo concat-idents 1.1.5 IdentParser#(concatenated_ident) = input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
52 let block: Blockrust-analyzer cargo syn 2.0.119 stmt/Block# = input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
53
54 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(InputParserrust-analyzer cargo concat-idents 1.1.5 InputParser# {
55 replace_identrust-analyzer cargo concat-idents 1.1.5 InputParser#replace_ident.,
56 concatenated_identrust-analyzer cargo concat-idents 1.1.5 InputParser#concatenated_ident.,
57 blockrust-analyzer cargo concat-idents 1.1.5 InputParser#block.,
58 })
59 }
60 }
61
62 /// A helper struct that implements [`Parse`] and makes one [`Ident`] from a comma separated list
63 /// of idents, literals and underscores
64 /// ```text
65 /// ident1, ident2, _, 3, _, true
66 /// => ident1ident2_3_true
67 /// ```
68 struct IdentParserrust-analyzer cargo concat-idents 1.1.5 IdentParser#(Identrust-analyzer cargo proc-macro2 1.0.107 Ident#);
69
70 impl Parserust-analyzer cargo syn 2.0.119 parse/Parse# for IdentParserrust-analyzer cargo concat-idents 1.1.5 IdentParser# {
71 fn parserust-analyzer cargo concat-idents 1.1.5 impl#[IdentParser][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.119 parse/ParseStream#) -> parserust-analyzer cargo syn 2.0.119 parse/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentParser][Parse]> {
72 let mut ident_parts = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
73
74 while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Bracerust-analyzer cargo syn 2.0.119 token/Brace().) {
75 ident_parts.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::parserust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]parse().(input)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
76
77 if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(Tokenrust-analyzer cargo syn 2.0.119 token/Token!![,]) {
78 input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().::<Tokenrust-analyzer cargo syn 2.0.119 token/Token!![,]>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
79 } else {
80 break;
81 }
82 }
83
84 let span = match ident_parts.firstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first().() {
85 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Identrust-analyzer cargo concat-idents 1.1.5 IdentPart#Ident#(i)) => i.spanrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]span().(),
86 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Underscorerust-analyzer cargo concat-idents 1.1.5 IdentPart#Underscore#(u)) => u.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().(),
87 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Strrust-analyzer cargo concat-idents 1.1.5 IdentPart#Str#(s)) => s.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]span().(),
88 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Charrust-analyzer cargo concat-idents 1.1.5 IdentPart#Char#(c)) => c.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitChar]span().(),
89 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Boolrust-analyzer cargo concat-idents 1.1.5 IdentPart#Bool#(b)) if ident_parts.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 => b.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitBool]span().(),
90
91 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Boolrust-analyzer cargo concat-idents 1.1.5 IdentPart#Bool#(b)) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
92 b.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitBool]span().(),
93 "Identifiers cannot consist of only one bool",
94 )),
95 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Intrust-analyzer cargo concat-idents 1.1.5 IdentPart#Int#(i)) if ident_parts.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
96 i.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitInt]span().(),
97 "Identifiers cannot start with integers",
98 )),
99 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Intrust-analyzer cargo concat-idents 1.1.5 IdentPart#Int#(i)) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
100 i.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitInt]span().(),
101 "Identifiers cannot start nor consist only of integers with integers",
102 )),
103 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
104 input.spanrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]span().(),
105 "Expected at least one identifier",
106 ))
107 };
108
109 let mut ident = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
110
111 for part in ident_parts {
112 match part {
113 IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Identrust-analyzer cargo concat-idents 1.1.5 IdentPart#Ident#(i) => ident.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().("r#")),
114 IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Underscorerust-analyzer cargo concat-idents 1.1.5 IdentPart#Underscore#(_) => ident.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('_'),
115 IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Intrust-analyzer cargo concat-idents 1.1.5 IdentPart#Int#(i) => ident.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()),
116 IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Boolrust-analyzer cargo concat-idents 1.1.5 IdentPart#Bool#(b) => ident.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(b.valuerust-analyzer cargo syn 2.0.119 lit/LitBool#value..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()),
117 IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Strrust-analyzer cargo concat-idents 1.1.5 IdentPart#Str#(s) => ident.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(s.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()),
118 IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart#::Charrust-analyzer cargo concat-idents 1.1.5 IdentPart#Char#(c) => ident.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitChar]value().())
119 }
120 }
121
122 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentParser][Parse](Identrust-analyzer cargo proc-macro2 1.0.107 Ident#::newrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]new().(ident.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), span)))
123 }
124 }
125
126 /// A helper struct, that represents a valid part of an identifier. Does not guarantee, that
127 /// this specific part is a fully qualified identifier.
128 ///
129 /// ```text
130 /// ident1, ident2, _, 3, _, true
131 /// => ident1, ident2, _, 3, _, true
132 /// ```
133 enum IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart# {
134 Underscorerust-analyzer cargo concat-idents 1.1.5 IdentPart#Underscore#(Underscorerust-analyzer cargo syn 2.0.119 token/Underscore#),
135 Identrust-analyzer cargo concat-idents 1.1.5 IdentPart#Ident#(Identrust-analyzer cargo proc-macro2 1.0.107 Ident#),
136 Intrust-analyzer cargo concat-idents 1.1.5 IdentPart#Int#(LitIntrust-analyzer cargo syn 2.0.119 lit/LitInt#),
137 Boolrust-analyzer cargo concat-idents 1.1.5 IdentPart#Bool#(LitBoolrust-analyzer cargo syn 2.0.119 lit/LitBool#),
138 Strrust-analyzer cargo concat-idents 1.1.5 IdentPart#Str#(LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#),
139 Charrust-analyzer cargo concat-idents 1.1.5 IdentPart#Char#(LitCharrust-analyzer cargo syn 2.0.119 lit/LitChar#),
140 }
141
142 impl Parserust-analyzer cargo syn 2.0.119 parse/Parse# for IdentPartrust-analyzer cargo concat-idents 1.1.5 IdentPart# {
143 fn parserust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]parse().(input: ParseStreamrust-analyzer cargo syn 2.0.119 parse/ParseStream#) -> parserust-analyzer cargo syn 2.0.119 parse/::Resultrust-analyzer cargo syn 2.0.119 error/Result#<Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]> {
144 if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(Identrust-analyzer cargo syn 2.0.119 ident/Ident().) {
145 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]::Identrust-analyzer cargo concat-idents 1.1.5 IdentPart#Ident#(input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().))
146 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(Tokenrust-analyzer cargo syn 2.0.119 token/Token!![_]) {
147 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]::Underscorerust-analyzer cargo concat-idents 1.1.5 IdentPart#Underscore#(input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().))
148 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitIntrust-analyzer cargo syn 2.0.119 lit/LitInt().) {
149 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]::Intrust-analyzer cargo concat-idents 1.1.5 IdentPart#Int#(input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().))
150 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitBoolrust-analyzer cargo syn 2.0.119 lit/LitBool().) {
151 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]::Boolrust-analyzer cargo concat-idents 1.1.5 IdentPart#Bool#(input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().))
152 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr().) {
153 let string = input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().::<LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
154 if string.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(|c: char| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_ascii_alphanumericrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_alphanumeric().() && c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().'_') {
155 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
156 string.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]span().(),
157 "Identifier parts can only contain [a-zA-Z0-9_]",
158 ))
159 } else {
160 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]::Strrust-analyzer cargo concat-idents 1.1.5 IdentPart#Str#(string))
161 }
162 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitCharrust-analyzer cargo syn 2.0.119 lit/LitChar().) {
163 let char = input.parserust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]parse().::<LitCharrust-analyzer cargo syn 2.0.119 lit/LitChar#>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
164 let c = char.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitChar]value().();
165 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_ascii_alphanumericrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_alphanumeric().() && c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]ne().'_' {
166 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
167 char.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitChar]span().(),
168 "Identifier parts can only contain [a-zA-Z0-9_]",
169 ))
170 } else {
171 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentPart][Parse]::Charrust-analyzer cargo concat-idents 1.1.5 IdentPart#Char#(char))
172 }
173 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitByteStrrust-analyzer cargo syn 2.0.119 lit/LitByteStr().) {
174 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(input.spanrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]span().(), "Identifiers cannot contain byte string"))
175 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitByterust-analyzer cargo syn 2.0.119 lit/LitByte().) {
176 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(input.spanrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]span().(), "Identifiers cannot contain bytes"))
177 } else if input.peekrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]peek().(LitFloatrust-analyzer cargo syn 2.0.119 lit/LitFloat().) {
178 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(input.spanrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]span().(), "Identifiers cannot contain floats"))
179 } else {
180 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::newrust-analyzer cargo syn 2.0.119 error/impl#[Error]new().(
181 input.spanrust-analyzer cargo syn 2.0.119 parse/impl#[`ParseBuffer<'a>`]span().(),
182 "Expected either an identifies, a `_`, an int, a bool, \
183 a string-literal, or a character-literal.\n\
184 Note: To create an Identifies from a reserved keywords like `struct`, or `return`, \
185 wrap it quotes, i.e. `\"struct\"`, or escape them with `r#`, i.e. `r#struct` .",
186 ))
187 }
188 }
189 }
190
191 /// A helper struct that implements [`VisitMut`] and is responsible for replacing the `replace_ident`
192 /// with the `concatenated_ident`.
193 struct IdentReplacerrust-analyzer cargo concat-idents 1.1.5 IdentReplacer# {
194 replace_identrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#replace_ident.: Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
195 concatenated_identrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#concatenated_ident.: Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
196 code_blockrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#code_block.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Blockrust-analyzer cargo syn 2.0.119 stmt/Block#>,
197 }
198
199 impl IdentReplacerrust-analyzer cargo concat-idents 1.1.5 IdentReplacer# {
200 /// Creates a new Instance of IdentReplacer from an InputParser
201 fn from_input_parserrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer]from_input_parser().(input_parser: InputParserrust-analyzer cargo concat-idents 1.1.5 InputParser#) -> Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer] {
202 Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer] {
203 replace_identrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#replace_ident.: input_parser.replace_identrust-analyzer cargo concat-idents 1.1.5 InputParser#replace_ident.,
204 concatenated_identrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#concatenated_ident.: input_parser.concatenated_identrust-analyzer cargo concat-idents 1.1.5 InputParser#concatenated_ident.,
205 code_blockrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#code_block.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(input_parser.blockrust-analyzer cargo concat-idents 1.1.5 InputParser#block.),
206 }
207 }
208
209 /// Replaces all `replace_idents` in the `code_block` with the `concatenated_ident`
210 fn replace_identsrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer]replace_idents().(mut self) -> Selfrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer] {
211 let mut code = self.code_blockrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#code_block.
212 .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().()
213 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
214 self.visit_block_mutrust-analyzer cargo syn 2.0.119 gen/visit_mut/VisitMut#visit_block_mut().(&mut code);
215 self.code_blockrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#code_block. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(code);
216
217 self
218 }
219
220 /// generates a TokenStream from the code-block
221 fn produce_token_streamrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer]produce_token_stream().(self) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
222 let statements = self.code_blockrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#code_block..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().stmtsrust-analyzer cargo syn 2.0.119 stmt/Block#stmts.;
223 (quoterust-analyzer cargo quote 1.0.47 quote!! { #( #statements )* }).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
224 }
225 }
226
227 impl VisitMutrust-analyzer cargo syn 2.0.119 gen/visit_mut/VisitMut# for IdentReplacerrust-analyzer cargo concat-idents 1.1.5 IdentReplacer# {
228 fn visit_ident_mutrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer][VisitMut]visit_ident_mut().(&mut self, node: &mut Identrust-analyzer cargo proc-macro2 1.0.107 Ident#) {
229 if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().node rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<Self>`]eq().==rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<Self>`]eq(). rust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][`PartialEq<Self>`]eq().self.replace_identrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#replace_ident. {
230 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().node = self.concatenated_identrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#concatenated_ident..clonerust-analyzer cargo proc-macro2 1.0.107 impl#[Ident][Clone]clone().();
231 }
232
233 // Delegate to the default impl
234 visit_mutrust-analyzer cargo syn 2.0.119 gen/visit_mut/::visit_ident_mutrust-analyzer cargo syn 2.0.119 gen/visit_mut/visit_ident_mut().(self, node);
235 }
236 }
237
238 /// This macros makes it possible to concatenate identifiers at compile time and use them as normal.
239 /// It's an extension/replacement of `std::concat_idents`, since in comprassion to the std-solution,
240 /// the idents here can be used everywhere.
241 ///
242 /// # Usage:
243 /// ### Basic usage
244 /// ```
245 /// use concat_idents::concat_idents;
246 ///
247 /// concat_idents!(fn_name = foo_, _, bar {
248 /// fn fn_name() {
249 /// // --snip--
250 /// }
251 /// });
252 ///
253 /// foo__bar();
254 /// ```
255 ///
256 /// ### Generating Tests
257 /// ```
258 ///# use concat_idents::concat_idents;
259 ///# use std::ops::{Add, Sub};
260 /// macro_rules! generate_test {
261 /// ($method:ident($lhs:ident, $rhs:ident)) => {
262 /// concat_idents!(test_name = $method, _, $lhs, _, $rhs {
263 /// #[test]
264 /// fn test_name() {
265 /// let _ = $lhs::default().$method($rhs::default());
266 /// }
267 /// });
268 /// };
269 /// }
270 ///
271 /// #[derive(Default)]
272 /// struct S(i32);
273 /// impl Add<i32> for S {
274 /// // --snip--
275 ///# type Output = S;
276 ///# fn add(self,rhs: i32) -> Self::Output { S(self.0 + rhs) }
277 /// }
278 /// impl Sub<i32> for S {
279 /// // --snip--
280 ///# type Output = S;
281 ///# fn sub(self,rhs: i32) -> Self::Output { S(self.0 - rhs) }
282 /// }
283 ///
284 /// generate_test!(add(S, i32));
285 /// generate_test!(sub(S, i32));
286 /// ```
287 #[proc_macro]
288 pub fn concat_identsrust-analyzer cargo concat-idents 1.1.5 concat_idents!(item: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
289 let input_parser = parse_macro_inputrust-analyzer cargo syn 2.0.119 parse_macro_input/parse_macro_input!!(item as InputParserrust-analyzer cargo concat-idents 1.1.5 InputParser#);
290
291 IdentReplacerrust-analyzer cargo concat-idents 1.1.5 IdentReplacer#::from_input_parserrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer]from_input_parser().(input_parser)
292 .replace_identsrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer]replace_idents().()
293 .produce_token_streamrust-analyzer cargo concat-idents 1.1.5 impl#[IdentReplacer]produce_token_stream().()
294 }