Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common Expand trait #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/template/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::{ExpandError, ParseError};
pub use crate::template::raw::Expand;
use crate::VariableMap;

mod raw;
Expand Down Expand Up @@ -252,7 +253,7 @@ impl<'a> ByteTemplate<'a> {
///
/// This will substitute all variables in the template with the values from the given map.
///
/// You can pass either a [`HashMap`][std::collections::HashMap], [`BTreeMap`][std::collections::BTreeMap] or [`Env`][crate::Env] as the `variables` parameter.
/// You can pass either a [`HashMap`][std::collections::HashMap], [`BTreeMap`][std::collections::BTreeMap], [`IndexMap`][indexmap::IndexMap] or [`Env`][crate::Env] as the `variables` parameter.
/// The maps must have [`&str`] or [`String`] keys, and the values must be [`AsRef<[u8]>`].
pub fn expand<'b, M>(&self, variables: &'b M) -> Result<Vec<u8>, ExpandError>
where
Expand Down
21 changes: 18 additions & 3 deletions src/template/raw/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ use super::{Part, Template, Variable};
use crate::error::{self, ExpandError};
use crate::VariableMap;

impl Template {
/// Common `expand` prototype, e.g., for variables and templates
pub trait Expand {
/// Expand into the output vector.
fn expand<'a, M, F>(
&self,
output: &mut Vec<u8>,
source: &[u8],
variables: &'a M,
to_bytes: &F,
) -> Result<(), ExpandError>
where
M: VariableMap<'a> + ?Sized,
F: Fn(&M::Value) -> &[u8];
}

impl Expand for Template {
/// Expand the template into the output vector.
pub fn expand<'a, M, F>(
fn expand<'a, M, F>(
&self,
output: &mut Vec<u8>,
source: &[u8],
Expand All @@ -27,7 +42,7 @@ impl Template {
}
}

impl Variable {
impl Expand for Variable {
/// Expand the variable into the output vector.
fn expand<'a, M, F>(
&self,
Expand Down
2 changes: 2 additions & 0 deletions src/template/raw/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod expand;
mod parse;

pub use expand::Expand;

/// Raw template that doesn't know track the original source.
///
/// Internally, this keeps a bunch of offsets into the original source.
Expand Down
4 changes: 2 additions & 2 deletions src/template/raw/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl Variable {
}

// If there is no matching un-escaped closing brace, it's missing.
let end = finger + find_closing_brace(&source[finger..])
.ok_or(error::MissingClosingBrace { position: finger + 1 })?;
let end = finger
+ find_closing_brace(&source[finger..]).ok_or(error::MissingClosingBrace { position: finger + 1 })?;

let variable = Variable {
name: name_start..name_end,
Expand Down
Loading