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
174
175
176
177
178
179
180
181
182
183
184
use super::{
Encoding,
Encoder
};
use super::nom::{
IResult,
ErrorKind,
be_u16
};
use std::mem;
#[repr(u16)]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum StatusField {
NoError = 0,
KeyNotFound = 1,
KeyExists = 2,
ValueTooLarge = 3,
InvalidArguments = 4,
ItemNotStored = 5,
IncrDecrNonNumeric = 6,
VBucketNotHere = 7,
AuthError = 8,
AuthContinue = 9,
UnknownCommand = 0x81,
OutOfMemory = 0x82,
NotSupported = 0x83,
InternalError = 0x84,
Busy = 0x85,
TemporaryFailure = 0x86
}
impl StatusField {
#[inline(always)]
pub fn check_status(&self) -> Result<(), StatusField> {
match *self {
StatusField::NoError => Ok(()),
x => Err(x)
}
}
}
impl Into<u16> for StatusField {
#[inline(always)]
fn into(self) -> u16 {
unsafe{ mem::transmute(self) }
}
}
impl Encoding for StatusField {
#[inline(always)]
fn encode(&self, buffer: &mut Encoder) {
let val: u16 = self.clone().into();
val.encode(buffer);
}
}
#[inline(always)]
fn from_u16(x: u16) -> StatusField {
unsafe{ mem::transmute(x) }
}
#[inline(always)]
fn valid_status(val: u16) -> bool {
val <= 9
||
(val >= 0x81 && val <= 0x86)
}
#[allow(dead_code)]
#[inline(always)]
pub fn status_parse<'a>(i: &'a [u8])
-> IResult<&'a [u8], StatusField> {
match be_u16(i) {
IResult::Done(rem,val) => if valid_status(val) {
IResult::Done(rem,from_u16(val))
} else {
IResult::Error(ErrorKind::Custom(0x83))
},
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(n) => IResult::Incomplete(n)
}
}
#[test]
fn test_status_field() {
use super::{Fault,ParseResult};
macro_rules! ot {
($a: expr, $b: ident) => {
let value: u16 = $a;
let sf: StatusField = StatusField::$b;
assert!(valid_status(value));
assert_eq!(from_u16(value), sf);
let mut v = unsafe{Encoder::with_capacity(20)};
sf.encode(&mut v);
v.encode_u8(0);
assert_eq!(v.len(), 3);
let dut = match ParseResult::from(status_parse(v.as_slice())) {
ParseResult::Ok(x) => x,
ParseResult::Err(e) => panic!("Status {:?} should not return error {:?}",value,e)
};
assert_eq!(dut, sf);
if sf == StatusField::NoError {
assert!(sf.check_status().is_ok());
} else {
assert_eq!(sf.check_status().err(), Option::Some(sf));
}
}
}
macro_rules! bt {
($a: expr) => {
let val: u16 = $a;
assert!( ! valid_status(val));
let mut v = unsafe{Encoder::with_capacity(100)};
val.encode(&mut v);
(0u16).encode(&mut v);
match ParseResult::from(status_parse(v.as_slice())) {
ParseResult::Ok(x) => panic!("u16 {:?} should not be opcode {:?}", val, x),
ParseResult::Err(Fault::BadStatus) => { },
ParseResult::Err(e) => panic!("u16 {:?} should retun `Fault::BadStatus` not {:?}", val, e)
};
}
}
ot!(0, NoError);
ot!(1, KeyNotFound);
ot!(2, KeyExists);
ot!(3, ValueTooLarge);
ot!(4, InvalidArguments);
ot!(5, ItemNotStored);
ot!(6, IncrDecrNonNumeric);
ot!(7, VBucketNotHere);
ot!(8, AuthError);
ot!(9, AuthContinue);
ot!(0x81, UnknownCommand);
ot!(0x82, OutOfMemory);
ot!(0x83, NotSupported);
ot!(0x84, InternalError);
ot!(0x85, Busy);
ot!(0x86, TemporaryFailure);
for code in 10u16..0x81u16 {
bt!(code);
}
for code in 0x87u16..0xFFFFu16 {
bt!(code);
}
bt!(0xFFFFu16);
}