]> git.proxmox.com Git - cargo.git/blob - vendor/serde_derive-1.0.11/src/fragment.rs
New upstream version 0.22.0
[cargo.git] / vendor / serde_derive-1.0.11 / src / fragment.rs
1 // Copyright 2017 Serde Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 use quote::{Tokens, ToTokens};
10
11 pub enum Fragment {
12 /// Tokens that can be used as an expression.
13 Expr(Tokens),
14 /// Tokens that can be used inside a block. The surrounding curly braces are
15 /// not part of these tokens.
16 Block(Tokens),
17 }
18
19 macro_rules! quote_expr {
20 ($($tt:tt)*) => {
21 $crate::fragment::Fragment::Expr(quote!($($tt)*))
22 }
23 }
24
25 macro_rules! quote_block {
26 ($($tt:tt)*) => {
27 $crate::fragment::Fragment::Block(quote!($($tt)*))
28 }
29 }
30
31 /// Interpolate a fragment in place of an expression. This involves surrounding
32 /// Block fragments in curly braces.
33 pub struct Expr(pub Fragment);
34 impl ToTokens for Expr {
35 fn to_tokens(&self, out: &mut Tokens) {
36 match self.0 {
37 Fragment::Expr(ref expr) => expr.to_tokens(out),
38 Fragment::Block(ref block) => {
39 out.append("{");
40 block.to_tokens(out);
41 out.append("}");
42 }
43 }
44 }
45 }
46
47 /// Interpolate a fragment as the statements of a block.
48 pub struct Stmts(pub Fragment);
49 impl ToTokens for Stmts {
50 fn to_tokens(&self, out: &mut Tokens) {
51 match self.0 {
52 Fragment::Expr(ref expr) => expr.to_tokens(out),
53 Fragment::Block(ref block) => block.to_tokens(out),
54 }
55 }
56 }
57
58 /// Interpolate a fragment as the value part of a `match` expression. This
59 /// involves putting a comma after expressions and curly braces around blocks.
60 pub struct Match(pub Fragment);
61 impl ToTokens for Match {
62 fn to_tokens(&self, out: &mut Tokens) {
63 match self.0 {
64 Fragment::Expr(ref expr) => {
65 expr.to_tokens(out);
66 out.append(",");
67 }
68 Fragment::Block(ref block) => {
69 out.append("{");
70 block.to_tokens(out);
71 out.append("}");
72 }
73 }
74 }
75 }