-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.rs
140 lines (112 loc) · 3.61 KB
/
mod.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright 2023 EverX Labs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use ever_block::{
fail, BuilderData, Cell, Deserializable, IBitstring, Result, Serializable, SliceData,
};
#[derive(Debug, thiserror::Error)]
pub enum DeserializationError {
#[error("unexpected tlb tag")]
UnexpectedTLBTag,
}
#[derive(Default, Debug, PartialEq, Eq)]
pub struct TVC {
pub code: Option<Cell>,
pub desc: Option<String>,
}
impl TVC {
const TVC_TAG: u32 = 0xa2f0b81c;
pub fn new(code: Option<Cell>, desc: Option<String>) -> Self {
Self { code, desc }
}
}
fn builder_store_bytes_ref(b: &mut BuilderData, data: &[u8]) -> Result<()> {
const CELL_LEN: usize = 127;
let mut tpb = BuilderData::new();
let mut len = data.len();
let mut cap = match len % CELL_LEN {
0 => CELL_LEN,
x => x,
};
while len > 0 {
len -= cap;
tpb.append_raw(&data[len..len + cap], cap * 8)?;
if len > 0 {
let mut nb = BuilderData::new();
nb.checked_append_reference(tpb.clone().into_cell()?)?;
cap = std::cmp::min(CELL_LEN, len);
tpb = nb;
}
}
b.checked_append_reference(tpb.into_cell()?)?;
Ok(())
}
pub fn builder_store_string_ref(builder: &mut BuilderData, data: &str) -> Result<()> {
builder_store_bytes_ref(builder, data.as_bytes())
}
pub fn slice_load_bytes_ref(slice: &mut SliceData) -> Result<Vec<u8>> {
let mut bytes: Vec<u8> = Vec::new();
let rrb = slice.remaining_references();
let mut curr: Cell = Cell::construct_from(slice)?;
assert_eq!(
rrb - 1,
slice.remaining_references(),
"ref not loaded from slice"
);
loop {
let cs = SliceData::load_cell(curr)?;
let bb = cs.get_bytestring(0);
bytes.append(&mut bb.clone());
if cs.remaining_references() > 0 {
curr = cs.reference(0)?;
} else {
break;
}
}
Ok(bytes)
}
pub fn slice_load_string_ref(slice: &mut SliceData) -> Result<String> {
Ok(String::from_utf8(slice_load_bytes_ref(slice)?)?)
}
impl Serializable for TVC {
fn write_to(&self, builder: &mut BuilderData) -> Result<()> {
builder.append_u32(Self::TVC_TAG)?;
if let Some(c) = &self.code {
builder.append_bit_one()?;
builder.checked_append_reference(c.to_owned())?;
} else {
builder.append_bit_zero()?;
}
if let Some(s) = &self.desc {
builder.append_bit_one()?;
builder_store_string_ref(builder, s)?;
} else {
builder.append_bit_zero()?;
}
Ok(())
}
}
impl Deserializable for TVC {
fn read_from(&mut self, slice: &mut SliceData) -> Result<()> {
let tag = slice.get_next_u32()?;
if tag != Self::TVC_TAG {
fail!(DeserializationError::UnexpectedTLBTag);
}
if slice.get_next_bit()? {
self.code = Some(Cell::construct_from(slice)?);
}
if slice.get_next_bit()? {
self.desc = Some(slice_load_string_ref(slice)?);
}
Ok(())
}
}