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 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into feature/publish-source-api
  • Loading branch information
mkantor committed Jun 6, 2021
commit bb70a73558dcd4309f0d1e262cdfe88590d23dd8
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
7 changes: 5 additions & 2 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
You are viewing a condensed version of this merge commit. You can view the full changes here.