macros.rs 2.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! impl_id {
    ( $name:ident ) => {
        /* This bit of the macro handles the repetitive nature of creating new identifiers.
         * Unfortunately, it doesn't work with serde_codegen, so until either plugins are stable or
         * nightly is required, just implement things by hand.
12
        #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
13
14
15
16
17
        pub struct $name(u64);
         */

        impl $name {
            /// Create a new id.
DCjanus's avatar
DCjanus committed
18
            pub const fn new(id: u64) -> Self {
19
20
                $name(id)
            }
21
22

            /// The value of the id.
DCjanus's avatar
DCjanus committed
23
            pub const fn value(&self) -> u64 {
24
25
                self.0
            }
26
27
28
29
30
31
32
33
34
35
36
        }

        impl Display for $name {
            fn fmt(&self, f: &mut Formatter) -> fmt::Result {
                write!(f, "{}", self.0)
            }
        }
    };
}

macro_rules! enum_serialize {
37
    ( $name:ident -> $desc:expr, $( $value:ident => $str:expr $( ; $opt:expr )*, )+ ) => {
38
        impl $name {
Ben Boeckel's avatar
Ben Boeckel committed
39
            /// String representation of the variant.
40
            pub fn as_str(&self) -> &'static str {
41
42
43
44
45
46
47
                match *self {
                    $( $name::$value => $str, )*
                }
            }
        }

        impl Serialize for $name {
48
49
50
            fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
                where S: Serializer,
            {
51
                serializer.serialize_str(self.as_str())
52
53
54
            }
        }

55
        impl<'de> Deserialize<'de> for $name {
56
            fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
57
                where D: Deserializer<'de>,
58
            {
59
                let val = String::deserialize(deserializer)?;
60

61
                match val.as_str() {
62
                    $( $str $( | $opt )* => Ok($name::$value), )*
63
64
                    v => {
                        error!(target: "gitlab", concat!("unknown ", $desc, " from gitlab: {}"), v);
65
66
                        Err(D::Error::invalid_value(Unexpected::Other("enumeration value"),
                                                    &concat!("invalid ", $desc)))
67
68
69
70
71
72
                    },
                }
            }
        }
    };
}
Ben Boeckel's avatar
Ben Boeckel committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

macro_rules! with_event {
    { $method: ident, $name: ident } => {
        pub fn $method(mut self) -> Self {
            self.$name = true;
            self
        }
    };
}

macro_rules! get_event {
    { $name: ident } => {
        pub fn $name(&self) -> bool {
            self.$name
        }
    };
}
90
91
92
93
94
95

macro_rules! query_param_slice {
    ( $( $x:expr ),* ) => (
        &[$($x),*] as QueryParamSlice
    )
}