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<Object>,
}
impl Operation {
pub fn new(operator: &str, operands: Vec<Object>) -> Operation {
Operation {
operator: operator.to_string(),
operands,
}
}
}
#[derive(Debug, Clone)]
pub struct Content<Operations: AsRef<[Operation]> = Vec<Operation>> {
pub operations: Operations,
}
impl<Operations: AsRef<[Operation]>> Content<Operations> {
pub fn encode(&self) -> Result<Vec<u8>> {
let mut buffer = Vec::new();
let mut first_operation = true;
for operation in self.operations.as_ref() {
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)
}
}