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 crate::*;
#[derive(Clone, Hash, PartialEq, Eq, Debug, serde::Serialize, serde::Deserialize)]
/// Generic value.
pub enum Value {
Empty,
Int(i64),
String(LString),
Binary(LVec<u8>),
List(LVec<Value>),
Enum(usize, LBox<Value>),
VarVal(u64, u64),
IList(LVec<i64>),
}
impl Value {
// Get mut reference to List LVec ( Value must be List )
pub fn list_mut(&mut self) -> &mut LVec<Value> {
match self {
Value::List(list) => list,
_ => panic!(),
}
}
// Get reference to List LVec ( Value must be List )
pub fn list(&self) -> &LVec<Value> {
match self {
Value::List(list) => list,
_ => panic!(),
}
}
pub fn en(&self) -> (&usize, &Value) {
match self {
Value::Enum(tag, bx) => (tag, &**bx),
_ => panic!(),
}
}
// Get reference to IList LVec ( Value must be IList )
pub fn ilist(&self) -> &LVec<i64> {
match self {
Value::IList(list) => list,
_ => panic!(),
}
}
// Get mut reference to IList LVec ( Value must be IList )
pub fn ilist_mut(&mut self) -> &mut LVec<i64> {
match self {
Value::IList(list) => list,
_ => panic!(),
}
}
// Get reference to LString ( Value must be String )
pub fn string(&self) -> &LString {
match self {
Value::String(s) => s,
_ => panic!(),
}
}
// Get reference to LVec ( Value must be Binary )
pub fn binary(&self) -> &LVec<u8> {
match self {
Value::Binary(b) => b,
_ => panic!(),
}
}
pub fn int(&self) -> i64 {
match self {
Value::Int(x) => *x,
_ => panic!(),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
postcard::to_stdvec(self).unwrap()
}
pub fn from_bytes(b: &[u8]) -> Self {
postcard::from_bytes(b).unwrap()
}
}
#[test]
fn test_value() {
/*
use pstd::veca;
let v1 = Value::List(veca![
Value::String(LString::from("George Barwood")),
Value::String(LString::from("george.barwood@gmail.com")),
Value::String(LString::from("33 Sandpipe Close, GL2 4LZ")),
Value::Int(-47),
Value::Int(99),
Value::VarVal(100, 20),
Value::IList(veca![1, 2, 3, 4]),
]);
let bytes = v1.to_bytes();
println!("len={} bytes={:?}", bytes.len(), bytes);
let v2 = Value::from_bytes(&bytes);
println!("v2={:?}", v2);
*/
}