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

Publish source api #415

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
35 changes: 16 additions & 19 deletions src/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,25 @@ use crate::template::Template;

pub(crate) const PARTIAL_BLOCK: &str = "@partial-block";

#[inline]
fn resolve_partial<'reg: 'rc, 'rc: 'p, 'p>(
tname: &str,
d: &Decorator<'reg, 'rc>,
fn find_partial<'reg: 'rc, 'rc: 'a, 'a>(
rc: &'a RenderContext<'reg, 'rc>,
r: &'reg Registry<'reg>,
rc: &'p RenderContext<'reg, 'rc>,
) -> Result<Option<Cow<'p, Template>>, RenderError> {
let mut partial = rc.get_partial(tname).map(Cow::Borrowed);

// try fetch from registry with the same name
if partial.is_none() {
if let Some(tpl) = r.get_template(tname) {
let tpl = tpl.map_err(RenderError::from)?;
partial = Some(tpl);
}
d: &Decorator<'reg, 'rc>,
name: &str,
) -> Result<Option<Cow<'a, Template>>, RenderError> {
if let Some(ref partial) = rc.get_partial(name) {
return Ok(Some(Cow::Borrowed(partial)));
}

if let Some(tpl) = r.get_or_load_template_optional(name) {
return tpl.map(Option::Some);
}

// fallback to decorator's content
if partial.is_none() {
partial = d.template().map(Cow::Borrowed);
if let Some(tpl) = d.template() {
return Ok(Some(Cow::Borrowed(tpl)));
}

Ok(partial)
Ok(None)
}

pub fn expand_partial<'reg: 'rc, 'rc>(
Expand All @@ -56,7 +52,8 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
return Err(RenderError::new("Cannot include self in >"));
}

let partial = resolve_partial(tname, d, r, rc)?;
// if tname == PARTIAL_BLOCK
let partial = find_partial(rc, r, d, tname)?;

if let Some(t) = partial {
// clone to avoid lifetime issue
Expand Down
35 changes: 32 additions & 3 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,12 @@ impl<'reg> Registry<'reg> {
}

#[inline]
fn get_or_load_template(&'reg self, name: &str) -> Result<Cow<'reg, Template>, RenderError> {
pub(crate) fn get_or_load_template_optional(
&'reg self,
name: &str,
) -> Option<Result<Cow<'reg, Template>, RenderError>> {
if let Some(source) = self.template_sources.get(name) {
source
let r = source
.load()
.map_err(|e| TemplateError::from((e, name.to_owned())))
.and_then(|tpl_str| Template::compile_with_name(tpl_str, name.to_owned()))
Expand Down Expand Up @@ -614,8 +617,9 @@ mod test {
use crate::render::{Helper, RenderContext, Renderable};
use crate::support::str::StringWriter;
use crate::template::Template;
use crate::Source;
use std::fs::File;
use std::io::Write;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Write};
use tempfile::tempdir;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -665,6 +669,31 @@ mod test {
);
}

#[test]
fn test_register_template_source() {
let mut r = Registry::new();

impl Source for &'static str {
type Item = String;
type Error = IoError;
fn load(&self) -> Result<Self::Item, Self::Error> {
Ok(String::from(*self))
}
}
r.register_template_source("test", "<h1>Hello world!</h1>");
assert_eq!("<h1>Hello world!</h1>", r.render("test", &()).unwrap());

impl Source for () {
type Item = String;
type Error = IoError;
fn load(&self) -> Result<Self::Item, Self::Error> {
Err(IoError::from(IoErrorKind::Other))
}
}
r.register_template_source("test2", ());
assert!(r.render("test2", &()).is_err());
}

#[test]
#[cfg(feature = "dir_source")]
fn test_register_templates_directory() {
Expand Down