1use std::collections::HashMap;
3
4#[derive(PartialEq,Eq,Debug,Hash,Clone)]
7pub enum BasicValue {
8 Byte(u8),
9 Boolean(bool),
10 Int16(i16),
11 Uint16(u16),
12 Int32(i32),
13 Uint32(u32),
14 Int64(i64),
15 Uint64(u64),
16 String(String),
17 ObjectPath(Path),
18 Signature(Signature),
19}
20
21#[derive(Clone,PartialEq,Eq,Debug,Hash)]
22pub struct Path(pub String);
23
24#[derive(Clone,PartialEq,Eq,Debug,Hash)]
25pub struct Signature(pub String);
26
27impl BasicValue {
28 pub fn get_signature(&self) -> &str {
30 match self {
31 &BasicValue::Byte(_) => "y",
32 &BasicValue::Boolean(_) => "b",
33 &BasicValue::Int16(_) => "n",
34 &BasicValue::Uint16(_) => "q",
35 &BasicValue::Int32(_) => "i",
36 &BasicValue::Uint32(_) => "u",
37 &BasicValue::Int64(_) => "x",
38 &BasicValue::Uint64(_) => "t",
39 &BasicValue::String(_) => "s",
40 &BasicValue::ObjectPath(_) => "o",
41 &BasicValue::Signature(_) => "g",
42 }
43 }
44}
45
46#[derive(PartialEq,Debug,Clone)]
50pub struct Struct {
51 pub objects: Vec<Value>,
52 pub signature: Signature
53}
54
55#[derive(PartialEq,Debug,Clone)]
59pub struct Variant {
60 pub object: Box<Value>,
61 pub signature: Signature
62}
63
64impl Variant {
65 pub fn new (v: Value, s: &str) -> Variant {
67 Variant {
68 object: Box::new(v),
69 signature: Signature(s.to_string())
70 }
71 }
72}
73
74#[derive(Clone,Debug,PartialEq)]
77pub struct Array {
78 pub objects: Vec<Value>,
79 signature: Signature
80}
81
82impl Array {
83 pub fn new(objects: Vec<Value>) -> Array {
90 let inner_sig = objects.iter().next().unwrap().get_signature().to_string();
91 let sig = "a".to_string() + &inner_sig;
92 Array {
93 objects: objects,
94 signature: Signature(sig)
95 }
96 }
97
98 pub fn new_with_sig(objects: Vec<Value>, sig: String) -> Array {
102 Array {
103 objects: objects,
104 signature: Signature(sig)
105 }
106 }
107}
108
109#[derive(Clone,Debug,PartialEq)]
110pub struct Dictionary {
111 pub map: HashMap<BasicValue,Value>,
112 signature: Signature
113}
114
115impl Dictionary {
116 pub fn new(map: HashMap<BasicValue,Value>) -> Dictionary {
123 let key_type = map.keys().next().unwrap().get_signature().to_string();
124 let val_type = map.values().next().unwrap().get_signature().to_string();
125 let sig = "a{".to_string() + &key_type + &val_type + "}";
126 Dictionary {
127 map: map,
128 signature: Signature(sig)
129 }
130 }
131
132 pub fn new_with_sig(map: HashMap<BasicValue,Value>, sig: String) -> Dictionary {
136 Dictionary {
137 map: map,
138 signature: Signature(sig)
139 }
140 }
141}
142
143#[derive(PartialEq,Debug,Clone)]
145pub enum Value {
146 BasicValue(BasicValue),
147 Double(f64),
148 Array(Array),
149 Variant(Variant),
150 Struct(Struct),
151 Dictionary(Dictionary)
152}
153
154impl Value {
155 pub fn get_signature(&self) -> &str {
157 match self {
158 &Value::BasicValue(ref x) => x.get_signature(),
159 &Value::Double(_) => "d",
160 &Value::Array(ref x) => &x.signature.0,
161 &Value::Variant(_) => "v",
162 &Value::Struct(ref x) => &x.signature.0,
163 &Value::Dictionary(ref x) => &x.signature.0
164 }
165 }
166}
167
168#[test]
169fn test_from () {
170 let x = Value::from(12);
171 assert_eq!(x, Value::BasicValue(BasicValue::Int32(12)));
172 let y = Value::from("foobar");
173 assert_eq!(y, Value::BasicValue(BasicValue::String("foobar".to_string())));
174}