An update will be applied December 9th, between 12PM and 1:00PM EST (UTC -5:00). The site may be slow during that time.

Commit 49b24634 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

macros: move into a separate module

parent fba833ba
......@@ -18,6 +18,9 @@ extern crate log;
mod error;
mod gitlab;
#[macro_use]
mod macros;
pub mod types;
pub use error::Error;
......
// Copyright 2016 Kitware, Inc.
//
// 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.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct $name(u64);
*/
impl $name {
/// Create a new id.
pub fn new(id: u64) -> Self {
$name(id)
}
}
impl Display for $name {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
macro_rules! enum_serialize {
( $name:ident -> $desc:expr, $( $value:ident => $str:expr ),+ ) => {
impl Borrow<str> for $name {
fn borrow(&self) -> &str {
match *self {
$( $name::$value => $str, )*
}
}
}
impl Serialize for $name {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_str(self.borrow())
}
}
impl Deserialize for $name {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
let val = try!(String::deserialize(deserializer));
match val.borrow() {
$( $str => Ok($name::$value), )*
v => {
error!(target: "gitlab", concat!("unknown ", $desc, " from gitlab: {}"), v);
Err(D::Error::invalid_value(concat!("invalid ", $desc)))
},
}
}
}
};
}
......@@ -19,62 +19,6 @@ use self::serde_json::Value;
use std::borrow::Borrow;
use std::fmt::{self, Display, Formatter};
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.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct $name(u64);
*/
impl $name {
/// Create a new id.
pub fn new(id: u64) -> Self {
$name(id)
}
}
impl Display for $name {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
macro_rules! enum_serialize {
( $name:ident -> $desc:expr, $( $value:ident => $str:expr ),+ ) => {
impl Borrow<str> for $name {
fn borrow(&self) -> &str {
match *self {
$( $name::$value => $str, )*
}
}
}
impl Serialize for $name {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_str(self.borrow())
}
}
impl Deserialize for $name {
fn deserialize<D: Deserializer>(deserializer: &mut D) -> Result<Self, D::Error> {
let val = try!(String::deserialize(deserializer));
match val.borrow() {
$( $str => Ok($name::$value), )*
v => {
error!(target: "gitlab", concat!("unknown ", $desc, " from gitlab: {}"), v);
Err(D::Error::invalid_value(concat!("invalid ", $desc)))
},
}
}
}
};
}
// This is only used in internal API calls.
//#[derive(Serialize, Deserialize, Debug)]
//pub struct UserSafe {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment