use pom::{DataInput, Result};
use std::io::{self, Write};
use super::{Object, Stream};
use super::parser;
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)
}
}