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
use std::mem::transmute;
use serde_cbor::{to_vec, Value};
use pointer::{
Pointer,
};
use pointer::Referenceable;
use error::Error;
pub trait Responsable {
fn to_response(self) -> Pointer;
}
impl<V: Into<Value>> Responsable for Result<V, Error> {
fn to_response(self: Result<V, Error>) -> Pointer {
let (return_code, mut return_value) = match self {
Ok(value) => (0, to_vec(&value.into()).unwrap()),
Err(error) => {
(error.code, to_vec(&Value::String(error.message.to_string())).unwrap())
},
};
let mut return_code_bytes = unsafe{transmute::<u32, [u8; 4]>(return_code as u32)}.to_vec();
return_code_bytes.append(&mut return_value);
return_code_bytes.as_pointer()
}
}
pub trait Bytes<T> {
fn value(&self) -> T;
}
impl Bytes<u64> for Vec<u8> {
fn value(&self) -> u64 {
if self.len() == 8 {
let mut slice: [u8; 8] = [0; 8];
slice.copy_from_slice(&self[..]);
unsafe{transmute::<[u8; 8], u64>(slice)}
} else {
0
}
}
}
impl Bytes<u32> for Vec<u8> {
fn value(&self) -> u32 {
if self.len() == 4 {
let mut slice: [u8; 4] = [0; 4];
slice.copy_from_slice(&self[..]);
unsafe{transmute::<[u8; 4], u32>(slice)}
} else {
0
}
}
}