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
use ::serialize::txt::*;
use ::serialize::binary::*;
use ::error::*;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct NULL { anything: Option<Vec<u8>> }
impl NULL {
pub fn new() -> NULL {
NULL { anything: None }
}
pub fn with(anything: Vec<u8>) -> NULL {
NULL { anything: Some(anything) }
}
pub fn get_anything(&self) -> Option<&Vec<u8>> {
self.anything.as_ref()
}
}
pub fn read(decoder: &mut BinDecoder, rdata_length: u16) -> DecodeResult<NULL> {
if rdata_length > 0 {
let mut anything: Vec<u8> = Vec::with_capacity(rdata_length as usize);
for _ in 0..rdata_length {
if let Ok(byte) = decoder.pop() {
anything.push(byte);
} else {
return Err(DecodeError::EOF);
}
}
Ok(NULL::with(anything))
} else {
Ok(NULL::new())
}
}
pub fn emit(encoder: &mut BinEncoder, nil: &NULL) -> EncodeResult {
if let Some(ref anything) = nil.get_anything() {
for b in anything.iter() {
try!(encoder.emit(*b));
}
}
Ok(())
}
#[allow(unused)]
pub fn parse(tokens: &Vec<Token>) -> ParseResult<NULL> {
unimplemented!()
}
#[test]
pub fn test() {
let rdata = NULL::with(vec![0,1,2,3,4,5,6,7]);
let mut bytes = Vec::new();
let mut encoder: BinEncoder = BinEncoder::new(&mut bytes);
assert!(emit(&mut encoder, &rdata).is_ok());
let bytes = encoder.as_bytes();
println!("bytes: {:?}", bytes);
let mut decoder: BinDecoder = BinDecoder::new(bytes);
let read_rdata = read(&mut decoder, bytes.len() as u16);
assert!(read_rdata.is_ok(), format!("error decoding: {:?}", read_rdata.unwrap_err()));
assert_eq!(rdata, read_rdata.unwrap());
}