-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtlv.rs
114 lines (95 loc) · 2.8 KB
/
tlv.rs
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
#![allow(dead_code)]
#[macro_use]
extern crate strict_encoding_derive;
use std::collections::BTreeSet;
use internet2::tlv;
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(StrictEncode, StrictDecode)]
#[strict_encoding(by_value, repr = u16)]
enum Feature {
#[strict_encoding(value = 0b0000_0000_0000_0001)]
PermanentConnection,
#[strict_encoding(value = 0b0000_0001_0000_0000)]
ProtocolV2,
}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
#[derive(NetworkEncode, NetworkDecode)]
struct Stamp {
quality: u8,
}
impl Stamp {
pub fn iter(&self) -> Stamp { *self }
}
// We need TLV type to implement iterator, such we know whether it has value or
// not
impl Iterator for Stamp {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if *self == Self::default() {
None
} else {
Some(self.quality)
}
}
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
#[network_encoding(use_tlv)]
struct Document {
pub name: String,
pub description: String,
#[network_encoding(tlv = 0x0101)]
pub signature: Vec<u8>,
#[network_encoding(tlv = 0x0201)]
pub stamp: Stamp,
#[network_encoding(unknown_tlvs)]
pub extra_fields: tlv::Stream,
#[network_encoding(skip)]
pub internal_use: Vec<u8>,
}
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(NetworkEncode, NetworkDecode)]
enum ProtocolMessages {
Init {
source: String,
destination: String,
features: BTreeSet<Feature>,
},
Ping,
Send(Document),
}
fn main() {
/*
impl strict_encoding::StrictDecode for TlvDefault {
#[inline]
fn strict_decode<D: ::std::io::Read>(
mut d: D,
) -> ::core::result::Result<Self, strict_encoding::Error> {
use strict_encoding::StrictDecode;
let mut s = TlvDefault {
fixed: strict_encoding::StrictDecode::strict_decode(&mut d)?,
tlv: Default::default(),
};
let tlvs = BTreeMap::<usize, Box<[u8]>>::strict_decode(&mut d)?;
for (type_no, bytes) in tlvs {
match type_no {
48879usize => {
s.tlv =
strict_encoding::StrictDecode::strict_deserialize(
bytes,
)?
}
_ if type_no % 2 == 0 => {
return Err(strict_encoding::TlvError::UnknownEvenType(
type_no,
)
.into())
}
_ => {}
}
}
Ok(s)
}
}
*/
}