forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tag.rs
96 lines (76 loc) Β· 1.74 KB
/
tag.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
use super::*;
#[derive(Copy, Clone, Debug)]
pub(super) enum Tag {
Body = 0,
Flags = 2,
Rune = 4,
Limit = 6,
Term = 8,
Deadline = 10,
DefaultOutput = 12,
Claim = 14,
#[allow(unused)]
Cenotaph = 126,
Divisibility = 1,
Spacers = 3,
Symbol = 5,
#[allow(unused)]
Nop = 127,
}
impl Tag {
pub(super) fn take(self, fields: &mut HashMap<u128, u128>) -> Option<u128> {
fields.remove(&self.into())
}
pub(super) fn encode(self, value: u128, payload: &mut Vec<u8>) {
varint::encode_to_vec(self.into(), payload);
varint::encode_to_vec(value, payload);
}
}
impl From<Tag> for u128 {
fn from(tag: Tag) -> Self {
tag as u128
}
}
impl PartialEq<u128> for Tag {
fn eq(&self, other: &u128) -> bool {
u128::from(*self) == *other
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_u128() {
assert_eq!(0u128, Tag::Body.into());
assert_eq!(2u128, Tag::Flags.into());
}
#[test]
fn partial_eq() {
assert_eq!(Tag::Body, 0);
assert_eq!(Tag::Flags, 2);
}
#[test]
fn take() {
let mut fields = vec![(2, 3)].into_iter().collect::<HashMap<u128, u128>>();
assert_eq!(Tag::Flags.take(&mut fields), Some(3));
assert!(fields.is_empty());
assert_eq!(Tag::Flags.take(&mut fields), None);
}
#[test]
fn encode() {
let mut payload = Vec::new();
Tag::Flags.encode(3, &mut payload);
assert_eq!(payload, [2, 3]);
Tag::Rune.encode(5, &mut payload);
assert_eq!(payload, [2, 3, 4, 5]);
}
#[test]
fn burn_and_nop_are_one_byte() {
let mut payload = Vec::new();
Tag::Cenotaph.encode(0, &mut payload);
assert_eq!(payload.len(), 2);
let mut payload = Vec::new();
Tag::Nop.encode(0, &mut payload);
assert_eq!(payload.len(), 2);
}
}