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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
extern crate ordered_float;

use std::cmp::Ordering;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use ordered_float::OrderedFloat;
//use std::collections::BTreeMap;


#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    Null,
    // ClassDef,
    Bool(bool),
    Int(i32),
    Long(i64),
    Double(f64),
    Bytes(Vec<u8>),
    String(String),
    Ref(u32),
    List(Vec<Value>),
    Map(HashMap<Value, Value>),
}

impl PartialOrd for Value {
    fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
        Some(self.cmp(&other))
    }
}

impl Eq for Value {}

// Although we impl Hash for Map and List, we shouldn't use container type as key
impl Hash for Value {
    fn hash<H: Hasher>(&self, state: &mut H) {
        use self::Value::*;

        match *self {
            Null => ().hash(state),
            Bool(b) => b.hash(state),
            Int(i) => i.hash(state),
            Long(l)=>l.hash(state),
            Double(d) => OrderedFloat(d).hash(state),
            Bytes(ref bytes) => bytes.hash(state),
            String(ref s) => s.hash(state),
            Ref(i) => i.hash(state),
            List(ref l) => l.hash(state),
            // Hash each key-value is too expensive.
            Map(ref m) => std::ptr::hash(m, state),
        }
    }
}


impl Ord for Value {
    fn cmp(&self, other: &Value) -> Ordering {
        use self::Value::*;

        match *self {
            Null => match *other {
                Null => Ordering::Equal,
                _ => Ordering::Less,
            },
            Bool(b) => match *other {
                Null => Ordering::Greater,
                Int(i) => (b as i32).cmp(&i),
                Long(l) => (b as i64).cmp(&l),
                Double(d) => float_ord(b as i64 as f64, d),
                _ => Ordering::Less,
            },
            Int(i) => match *other {
                Null => Ordering::Greater,
                Bool(b) => i.cmp(&(b as i32)),
                Int(i2) => i.cmp(&i2),
                Long(l) => (i as i64).cmp(&l),
                Double(d) => float_ord(i as f64, d),
                _ => Ordering::Less,
            },
            Long(l) => match *other {
                Null => Ordering::Greater,
                Bool(b) => l.cmp(&(b as i64)),
                Int(i2) => l.cmp(&(i2 as i64)),
                Long(l2) => l.cmp(&l2),
                Double(d) => float_ord(l as f64, d),
                _ => Ordering::Less,
            },
            Double(d) => match *other {
                Null => Ordering::Greater,
                Bool(b) => float_ord(d, b as i64 as f64),
                Int(i) => float_ord(d, i as f64),
                Long(l) => float_ord(d, l as f64),
                Double(d2) => float_ord(d, d2),
                _ => Ordering::Less,
            },
            Bytes(ref bs) => match *other {
                String(_) | List(_) | Ref(_) | Map(_) => Ordering::Less,
                Bytes(ref bs2) => bs.cmp(bs2),
                _ => Ordering::Greater,
            },
            String(ref s) => match *other {
                Ref(_) | List(_) | Map(_) => Ordering::Less,
                String(ref s2) => s.cmp(s2),
                _ => Ordering::Greater,
            },
            Ref(i) => match *other {
                List(_) | Map(_) => Ordering::Less,
                Ref(i2) => i.cmp(&i2),
                _ => Ordering::Greater,
            },
            List(ref l) => match other {
                Map(_) => Ordering::Less,
                List(l2) => l.cmp(l2),
                _ => Ordering::Greater,
            },
            Map(ref m) => match other {
                Map(m2) => {
                    let v1: Vec<_> = m.iter().collect();
                    let v2: Vec<_> = m2.iter().collect();
                    v1.cmp(&v2)
                }
                _ => Ordering::Greater,
            },
        }
    }
}


fn float_ord(f: f64, g: f64) -> Ordering {
    match f.partial_cmp(&g) {
        Some(o) => o,
        None => Ordering::Less,
    }
}

pub trait ToHessian {
    fn to_hessian(self) -> Value;
}


macro_rules! to_hessian (
    ($t:ty, $v:expr) => (
        impl ToHessian for $t {
        fn to_hessian(self) -> Value {
            $v(self)
        }
    }
    );
);

to_hessian!(bool, Value::Bool);
to_hessian!(i32, Value::Int);
to_hessian!(i64, Value::Long);
to_hessian!(f64, Value::Double);
to_hessian!(String, Value::String);
to_hessian!(Vec<u8>, Value::Bytes);

impl ToHessian for () {
    fn to_hessian(self) -> Value {
        Value::Null
    }
}

impl<'a> ToHessian for &'a str {
    fn to_hessian(self) -> Value {
        Value::String(self.to_owned())
    }
}

impl<'a> ToHessian for &'a [u8] {
    fn to_hessian(self) -> Value {
        Value::Bytes(self.to_owned())
    }
}