Skip to content

Commit

Permalink
Genericize Content to allow AsRef<[Operation]>
Browse files Browse the repository at this point in the history
This was just Vec<Operation> before, which may be the most common
use-case and is also required for parsing, since allocation is
necessary.
When creating a document allocating a Vec per Content may not be
necessary though, so for that use-case allowing any AsRef<[Operation]>,
like just a slice, is convenient.
  • Loading branch information
z33ky committed Aug 4, 2020
1 parent a9d0b42 commit 9829c59
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ impl Operation {
}

#[derive(Debug, Clone)]
pub struct Content {
pub operations: Vec<Operation>,
pub struct Content<Operations: AsRef<[Operation]> = Vec<Operation>> {
pub operations: Operations,
}

impl Content {
impl<Operations: AsRef<[Operation]>> Content<Operations> {
/// Encode content operations.
pub fn encode(&self) -> Result<Vec<u8>> {
let mut buffer = Vec::new();
for operation in &self.operations {
for operation in self.operations.as_ref() {
for operand in &operation.operands {
Writer::write_object(&mut buffer, operand)?;
buffer.write_all(b" ")?;
Expand All @@ -38,16 +38,18 @@ impl Content {
}
Ok(buffer)
}
}

impl Content<Vec<Operation>> {
/// Decode content operations.
pub fn decode(data: &[u8]) -> Result<Content> {
pub fn decode(data: &[u8]) -> Result<Self> {
parser::content(data).ok_or(Error::ContentDecode)
}
}

impl Stream {
/// Decode content after decoding all stream filters.
pub fn decode_content(&self) -> Result<Content> {
pub fn decode_content(&self) -> Result<Content<Vec<Operation>>> {
Content::decode(&self.content)
}
}
4 changes: 2 additions & 2 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::content::Content;
use super::content::{Content, Operation};
use super::encodings::{self, bytes_to_string, string_to_bytes};
use super::{Dictionary, Object, ObjectId};
use crate::xref::Xref;
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Document {
}

/// Get decoded page content;
pub fn get_and_decode_page_content(&self, page_id: ObjectId) -> Result<Content> {
pub fn get_and_decode_page_content(&self, page_id: ObjectId) -> Result<Content<Vec<Operation>>> {
let content_data = self.get_page_content(page_id)?;
Content::decode(&content_data)
}
Expand Down
4 changes: 2 additions & 2 deletions src/nom_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ fn operation(input: &[u8]) -> NomResult<Operation> {
|(operands, operator)| Operation { operator, operands })(input)
}

fn _content(input: &[u8]) -> NomResult<Content> {
fn _content(input: &[u8]) -> NomResult<Content<Vec<Operation>>> {
preceded(content_space, map(many0(operation), |operations| Content { operations }))(input)
}

pub fn content(input: &[u8]) -> Option<Content> {
pub fn content(input: &[u8]) -> Option<Content<Vec<Operation>>> {
strip_nom(_content(input))
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn operation<'a>() -> Parser<'a, u8, Operation> {
operation.map(|(operands, operator)| Operation { operator, operands })
}

pub fn content(input: &[u8]) -> Option<Content> {
pub fn content(input: &[u8]) -> Option<Content<Vec<Operation>>> {
(content_space() * operation().repeat(0..).map(|operations| Content { operations })).parse(input).ok()
}

Expand Down

0 comments on commit 9829c59

Please sign in to comment.