diff --git a/src/template/mod.rs b/src/template/mod.rs index e7cf74c..c7b32af 100644 --- a/src/template/mod.rs +++ b/src/template/mod.rs @@ -1,4 +1,5 @@ use crate::error::{ExpandError, ParseError}; +pub use crate::template::raw::Expand; use crate::VariableMap; mod raw; @@ -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, ExpandError> where diff --git a/src/template/raw/expand.rs b/src/template/raw/expand.rs index d6bb16a..3e833c3 100644 --- a/src/template/raw/expand.rs +++ b/src/template/raw/expand.rs @@ -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, + 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, source: &[u8], @@ -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, diff --git a/src/template/raw/mod.rs b/src/template/raw/mod.rs index 184d454..4334b6b 100644 --- a/src/template/raw/mod.rs +++ b/src/template/raw/mod.rs @@ -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. diff --git a/src/template/raw/parse.rs b/src/template/raw/parse.rs index bad1a91..025fc9c 100644 --- a/src/template/raw/parse.rs +++ b/src/template/raw/parse.rs @@ -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,