Debian Codegraph @

README.md

raw symbols SCIP
1 # Macros for all your token pasting needs
2
3 [<img alt="github" src="https://img.shields.io/badge/github-as1100k/pastey-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/as1100k/pastey)
4 [<img alt="crates.io" src="https://img.shields.io/crates/v/pastey.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/pastey)
5 [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-pastey-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/pastey)
6 [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/as1100k/pastey/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/as1100k/pastey/actions?query=branch%master)
7
8 **_`pastey` is the fork of `paste` and is aimed to be a drop-in replacement with additional features for
9 `paste` crate_**
10 <details>
11 <summary>Migrating from <code>paste</code> crate</summary>
12
13 Migrating from `paste` crate to `pastey` is super simple, just change the following
14 in your `Cargo.toml`
15
16 ```diff
17 [dependencies]
18 - paste = "1"
19 + pastey = "*" # Or any specific version of pastey
20 ```
21
22 Or even better way:
23
24 ```diff
25 [dependencies]
26 - paste = "1"
27 + paste = { package = "pastey", version = "*" }
28 ```
29
30 </details>
31
32 ## Quick Start
33
34 Add `pastey` as your dependency in `Cargo.toml`
35
36 ```toml
37 [dependencies]
38 # TODO: Replace with latest version available on crates.io
39 pastey = "*"
40 ```
41
42 This approach works with any Rust compiler 1.54+.
43
44 ## Pasting identifiers
45
46 Within the `paste!` macro, identifiers inside `[<`...`>]` are pasted together to
47 form a single identifier.
48
49 ```rust
50 use pastey::paste;
51
52 paste! {
53 // Defines a const called `QRST`.
54 const [<Q R S T>]: &str = "success!";
55 }
56
57 fn main() {
58 assert_eq!(
59 paste! { [<Q R S T>].len() },
60 8,
61 );
62 }
63 ```
64
65 ## More elaborate example
66
67 The next example shows a macro that generates accessor methods for some struct
68 fields. It demonstrates how you might find it useful to bundle a paste
69 invocation inside of a macro\_rules macro.
70
71 ```rust
72 use pastey::paste;
73
74 macro_rules! make_a_struct_and_getters {
75 ($name:ident { $($field:ident),* }) => {
76 // Define a struct. This expands to:
77 //
78 // pub struct S {
79 // a: String,
80 // b: String,
81 // c: String,
82 // }
83 pub struct $name {
84 $(
85 $field: String,
86 )*
87 }
88
89 // Build an impl block with getters. This expands to:
90 //
91 // impl S {
92 // pub fn get_a(&self) -> &str { &self.a }
93 // pub fn get_b(&self) -> &str { &self.b }
94 // pub fn get_c(&self) -> &str { &self.c }
95 // }
96 paste! {
97 impl $name {
98 $(
99 pub fn [<get_ $field>](&self) -> &str {
100 &self.$field
101 }
102 )*
103 }
104 }
105 }
106 }
107
108 make_a_struct_and_getters!(S { a, b, c });
109
110 fn call_some_getters(s: &S) -> bool {
111 s.get_a() == s.get_b() && s.get_c().is_empty()
112 }
113 let s = S {
114 a: String::from("x"),
115 b: String::from("x"),
116 c: String::new(),
117 };
118 assert!(call_some_getters(&s));
119 let s2 = S {
120 a: String::from("x"),
121 b: String::from("y"),
122 c: String::new(),
123 };
124 assert!(!call_some_getters(&s2));
125 ```
126
127 ## Case conversion
128
129 The `pastey` crate supports the following case modfiers:
130
131 | Modifier | Description |
132 |------------------------------------|---------------------------------------|
133 | `$var:lower` | Lower Case |
134 | `$var:upper` | Upper Case |
135 | `$var:snake` | Snake Case |
136 | `$var:camel` or `$var:upper_camel` | Upper Camel Case |
137 | `$var:lower_camel` | Lower Camel Case [#4] |
138 | `$var:camel_edge` | Covers Edge cases of Camel Case. [#3] |
139
140 _**NOTE: The pastey crate is going to be a drop in replacement to paste crate,
141 and will not change the behaviour of existing modifier like `lower`, `upper`,
142 `snake` and `camel`. For modifying the behaviour new modifiers will be created,
143 like `camel_edge`**_
144
145 You can also use multiple of these modifers like `$var:snake:upper` would give you
146 `SCREAMING_SNAKE_CASE`.
147
148 Example
149
150 ```rust
151 use pastey::paste;
152
153 paste! {
154 const [<LIB env!("CARGO_PKG_NAME"):snake:upper>]: &str = "libpastey";
155
156 let _ = LIBPASTEY;
157 }
158 ```
159
160 The precise Unicode conversions are as defined by [`str::to_lowercase`] and
161 [`str::to_uppercase`].
162
163 [#3]: https://github.com/AS1100K/pastey/issues/3
164 [#4]: https://github.com/AS1100K/pastey/issues/4
165 [`str::to_lowercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase
166 [`str::to_uppercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_uppercase
167
168 ## Replace modifier
169
170 The `replace` modifier allows you to perform string replacement on identifiers,
171 using the same semantics as [`str::replace`]. This is useful for transforming
172 identifiers by removing or substituting substrings.
173
174 [`str::replace`]: https://doc.rust-lang.org/std/primitive.str.html#method.replace
175 ```rust
176 use pastey::paste;
177
178 macro_rules! m {
179 ($(($command:ident, $from:ident)),+) => {
180 paste! {
181 $(pub struct $command {})*
182
183 pub enum Command {
184 $(
185 [< $command:replace($from, "") >] ( $command )
186 ),*
187 }
188 }
189 }
190 }
191
192 m! {
193 (CommandFoo, Command),
194 (CommandBar, Command),
195 (HelloWorld, Hello)
196 }
197
198 let command_bar = Command::Bar(CommandBar {});
199 let command_foo = Command::Foo(CommandFoo {});
200 let command_hello = Command::World(HelloWorld {});
201 ```
202
203 ## Raw Identifier Generation
204
205 `pastey` now supports raw identifiers using a special raw mode. By prefixing a
206 token with `#` inside the paste syntax, it treats that token as a raw identifier.
207
208 ```rust
209 use pastey::paste;
210
211 macro_rules! define_struct_and_impl {
212 ($name:ident $(- $name_tail:ident)*) => {
213 paste!{
214 struct [< # $name:camel $( $name_tail)* >]; // '#' signals a raw identifier
215
216 impl [< # $name:camel $( $name_tail)* >] {
217 fn [< # $name:snake $( _ $name_tail:snake)* >]() {}
218 }
219
220 }
221 }
222 }
223
224 define_struct_and_impl!(loop);
225 define_struct_and_impl!(loop - xyz);
226
227 fn test_fn() {
228 let _ = Loop::r#loop();
229 let _ = Loopxyz::loop_xyz();
230 }
231 test_fn();
232 ```
233
234 ## Pasting documentation strings
235
236 Within the `paste!` macro, arguments to a #\[doc ...\] attribute are implicitly
237 concatenated together to form a coherent documentation string.
238
239 ```rust
240 use pastey::paste;
241
242 macro_rules! method_new {
243 ($ret:ident) => {
244 paste! {
245 #[doc = "Create a new `" $ret "` object."]
246 pub fn new() -> $ret { $ret {} }
247 }
248 };
249 }
250
251 pub struct Pastey {}
252
253 method_new!(Pastey); // expands to #[doc = "Create a new `Paste` object"]
254 let _ = new();
255 ```
256
257 #### Credits
258
259 <sup>
260 This crate is the fork of <a href="https://github.com/dtolnay/paste"><code>paste</code></a>
261 and I appreciate the efforts of <a href="https://github.com/dtolnay">@dtolnay</a>
262 and other contributors.
263 </sup>
264
265 #### License
266
267 <sup>
268 Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
269 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
270 </sup>
271
272 <br>
273
274 <sub>
275 Unless you explicitly state otherwise, any contribution intentionally submitted
276 for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
277 be dual licensed as above, without any additional terms or conditions.
278 </sub>