use std::iter::Iterator;
macro_rules! optional_add {
($map:ident, $sn:expr, $field:expr, $val: ident, $ex:expr) => {
match $sn {
Some(ref $val) => { $map.insert($field.to_owned(), $ex); }
_ => ()
}
};
($map:ident, $sn:expr, $field:expr) => {
optional_add!($map, $sn, $field, value, value.to_json());
};
}
macro_rules! get_json_thing {
($r:ident,$f:expr,$t:ident) => {
$r.find($f)
.expect(concat!("No field '", stringify!($f), "'"))
.$t()
.expect(concat!("Field '",
stringify!($f),
"' is not of type ",
stringify!($t)))
}
}
macro_rules! get_json_object {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_object)
}
}
macro_rules! get_json_array {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_array)
}
}
macro_rules! get_json_string {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_string).to_owned()
}
}
macro_rules! get_json_i64 {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_i64)
}
}
macro_rules! get_json_u64 {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_u64)
}
}
macro_rules! get_json_bool {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_boolean)
}
}
macro_rules! get_json_f64 {
($r:ident,$f:expr) => {
get_json_thing!($r,$f,as_f64)
}
}
macro_rules! optional_json_thing {
($r:ident,$f:expr,$t:ident) => {
$r.find($f).and_then(|v| {
v.$t()
})
}
}
macro_rules! optional_json_string {
($r:ident,$f:expr) => {
optional_json_thing!($r, $f, as_string).and_then(|str| {
Some(str.to_owned())
})
}
}
macro_rules! optional_json_f64 {
($r:ident,$f:expr) => {
optional_json_thing!($r, $f, as_f64).and_then(|f| Some(f))
}
}
pub trait StrJoin {
fn join(self, join: &str) -> String;
}
impl<I, S> StrJoin for I where
S: AsRef<str>,
I: Iterator<Item=S> {
fn join(self, join: &str) -> String {
let mut s = String::new();
for f in self {
s.push_str(f.as_ref());
s.push_str(join);
}
s.pop();
s
}
}
macro_rules! from_exp {
($ft:ty, $dt:ident, $pi:ident, $ex:expr) => {
impl From<$ft> for $dt {
fn from($pi: $ft) -> $dt {
$ex
}
}
}
}
macro_rules! from {
($ft:ty, $dt:ident, $ev:ident, $pi:ident) => {
from_exp!($ft, $dt, $pi, $dt::$ev($pi));
};
($ft:ty, $dt:ident, $ev:ident) => {
from!($ft, $dt, $ev, from);
};
}