use super::Object; use crate::writer::Writer; use crate::Result; use std::io::Write; #[derive(Debug, Clone)] pub struct Operation { pub operator: String, pub operands: Vec, } impl Operation { pub fn new(operator: &str, operands: Vec) -> Operation { Operation { operator: operator.to_string(), operands, } } } #[derive(Debug, Clone)] pub struct Content = Vec> { pub operations: Operations, } impl> Content { /// Encode content operations. pub fn encode(&self) -> Result> { let mut buffer = Vec::new(); let mut first_operation = true; for operation in self.operations.as_ref() { // Add new line after each operation except the last one. if first_operation { first_operation = false; } else { buffer.write_all(b"\n")?; } for operand in &operation.operands { Writer::write_object(&mut buffer, operand)?; buffer.write_all(b" ")?; } buffer.write_all(operation.operator.as_bytes())?; } Ok(buffer) } }