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
use super::{fold::FoldSerializer, op_code::OpCode, sigma_byte_writer::SigmaByteWrite};
use crate::ast::{CollMethods, Constant, ConstantPlaceholder, Expr};
use crate::serialization::{
sigma_byte_reader::SigmaByteRead, SerializationError, SigmaSerializable,
};
use std::io;
impl SigmaSerializable for Expr {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> Result<(), io::Error> {
match self {
Expr::Const(c) => match w.constant_store() {
Some(cs) => {
let ph = cs.put(c.clone());
ph.op_code().sigma_serialize(w)?;
ph.sigma_serialize(w)
}
None => c.sigma_serialize(w),
},
expr => {
let op_code = self.op_code();
op_code.sigma_serialize(w)?;
match expr {
Expr::CollM(cm) => match cm {
CollMethods::Fold { .. } => FoldSerializer::sigma_serialize(expr, w),
},
Expr::ConstPlaceholder(cp) => cp.sigma_serialize(w),
_ => panic!(format!("don't know how to serialize {:?}", expr)),
}
}
}
}
fn sigma_parse<R: SigmaByteRead>(r: &mut R) -> Result<Self, SerializationError> {
let first_byte = match r.peek_u8() {
Ok(b) => Ok(b),
Err(_) => {
let res = r.get_u8();
assert!(res.is_err());
res
}
}?;
if first_byte <= OpCode::LAST_CONSTANT_CODE.value() {
let constant = Constant::sigma_parse(r)?;
Ok(Expr::Const(constant))
} else {
let op_code = OpCode::sigma_parse(r)?;
match op_code {
FoldSerializer::OP_CODE => FoldSerializer::sigma_parse(r),
ConstantPlaceholder::OP_CODE => {
let cp = ConstantPlaceholder::sigma_parse(r)?;
if r.substitute_placeholders() {
let c = r.constant_store().get(cp.id).unwrap();
Ok(Expr::Const(c.clone()))
} else {
Ok(Expr::ConstPlaceholder(cp))
}
}
o => Err(SerializationError::NotImplementedOpCode(o.value())),
}
}
}
}