td_proto_rust/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![macro_use]

macro_rules! fail {
    ($expr:expr) => (
        return Err(::std::convert::From::from($expr));
    )
}

macro_rules! ensure {
    ($expr:expr, $err_result:expr) => (
        if !($expr) { fail!($err_result) }
    )
}

macro_rules! unwrap_or {
    ($expr:expr, $or:expr) => (
        match $expr {
            Some(x) => x,
            None => { $or }
        }
    )
}

macro_rules! try_read {
    ($expr:expr, $val:expr) => (
        {
            if try!($expr) != $val {
                fail!((ErrorKind::NoLeftSpaceError, "must left space to read "));
            }
        }
    )
}

macro_rules! decode_array {
    ($expr:expr, $path:path, $match_path:path) => (
        {
            let mut value : Vec<Value> = vec![];
            loop {
                let sub_value = try!($expr);
                match sub_value {
                    Value::Nil => {
                        break;
                    }
                    $match_path(_) => {}
                    _ => fail!((ErrorKind::TypeNotMatchError, "must match type")),
                }
                value.push(sub_value);
            }
            Ok($path(value))
        }
    )
}


macro_rules! check_vailed {
    ($value:ident, $expr:expr) => (
        {
            let t = get_type_by_value($value);
            ensure!($expr == t, (ErrorKind::TypeNotMatchError, "must match type"));
        }
    )
}