1use super::Object;
2use crate::writer::Writer;
3use crate::Result;
4use std::io::Write;
5
6#[derive(Debug, Clone)]
7pub struct Operation {
8 pub operator: String,
9 pub operands: Vec<Object>,
10}
11
12impl Operation {
13 pub fn new(operator: &str, operands: Vec<Object>) -> Operation {
14 Operation {
15 operator: operator.to_string(),
16 operands,
17 }
18 }
19}
20
21#[derive(Debug, Clone)]
22pub struct Content<Operations: AsRef<[Operation]> = Vec<Operation>> {
23 pub operations: Operations,
24}
25
26impl<Operations: AsRef<[Operation]>> Content<Operations> {
27 pub fn encode(&self) -> Result<Vec<u8>> {
29 let mut buffer = Vec::new();
30 let mut first_operation = true;
31 for operation in self.operations.as_ref() {
32 if first_operation {
34 first_operation = false;
35 } else {
36 buffer.write_all(b"\n")?;
37 }
38 for operand in &operation.operands {
39 Writer::write_object(&mut buffer, operand)?;
40 buffer.write_all(b" ")?;
41 }
42 buffer.write_all(operation.operator.as_bytes())?;
43 }
44 Ok(buffer)
45 }
46}