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
use std::fmt::{self, Debug, Formatter};
use xor_name::XorName;
use data::DataIdentifier;
use utils;
#[derive(Hash, Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, PartialOrd, Ord)]
pub struct PlainData {
name: XorName,
value: Vec<u8>,
}
impl PlainData {
pub fn new(name: XorName, value: Vec<u8>) -> PlainData {
PlainData {
name: name,
value: value,
}
}
pub fn value(&self) -> &Vec<u8> {
&self.value
}
pub fn name(&self) -> &XorName {
&self.name
}
pub fn payload_size(&self) -> usize {
self.value.len()
}
pub fn identifier(&self) -> DataIdentifier {
DataIdentifier::Plain(*self.name())
}
}
impl Debug for PlainData {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
write!(formatter,
"PlainData {{ name: {}, value: {} }}",
self.name,
utils::format_binary_array(&self.value))
}
}
#[cfg(test)]
mod test {
use super::PlainData;
use itertools::Itertools;
use rand::{self, Rng};
use rustc_serialize::hex::ToHex;
#[test]
fn basic_check() {
let name1 = rand::random();
let name2 = rand::random();
let value1 = rand::thread_rng().gen_iter().take(1025).collect_vec();
let value2 = rand::thread_rng().gen_iter().take(1025).collect_vec();
let plain_data1 = PlainData::new(name1, value1.clone());
let plain_data2 = PlainData::new(name2, value2.clone());
assert!(plain_data1.name() != plain_data2.name());
assert!(plain_data1.value().to_hex() != plain_data2.value().to_hex());
assert_eq!(plain_data1.value().to_hex(), value1.to_hex());
assert_eq!(plain_data2.value().to_hex(), value2.to_hex());
}
}