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
use super::parser;
use super::{Object, Stream};
use pom::{DataInput, Result};
use std::io::{self, Write};
use writer::Writer;
#[derive(Debug, Clone)]
pub struct Operation {
pub operator: String,
pub operands: Vec<Object>,
}
impl Operation {
pub fn new(operator: &str, operands: Vec<Object>) -> Operation {
Operation {
operator: operator.to_string(),
operands: operands,
}
}
}
#[derive(Debug, Clone)]
pub struct Content {
pub operations: Vec<Operation>,
}
impl Content {
pub fn encode(&self) -> io::Result<Vec<u8>> {
let mut buffer = Vec::new();
for operation in &self.operations {
for operand in &operation.operands {
Writer::write_object(&mut buffer, operand)?;
buffer.write_all(b" ")?;
}
buffer.write_all(operation.operator.as_bytes())?;
buffer.write_all(b"\n")?;
}
Ok(buffer)
}
pub fn decode(data: &[u8]) -> Result<Content> {
let mut input = DataInput::new(data);
parser::content().parse(&mut input)
}
}
impl Stream {
pub fn decode_content(&self) -> Result<Content> {
Content::decode(&self.content)
}
}