Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
progmboy committed Jan 16, 2024
1 parent 786d132 commit 8f16353
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod test {
"{{#if}}hello{{else~}}world{{/if}}",
"{{#if}}hello{{~^~}}world{{/if}}",
"{{#if}}{{/if}}",
"{{#if}}hello{{else if}}world{{else}}test{{/if}}"
"{{#if}}hello{{else if}}world{{else}}test{{/if}}",
];
for i in s.iter() {
assert_rule!(Rule::helper_block, i);
Expand Down
10 changes: 8 additions & 2 deletions src/helpers/helper_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ mod test {
assert_eq!(
"3".to_owned(),
handlebars
.render_template("{{#if a}}1{{else if b}}2{{else if c}}3{{else if d}}4{{else}}0{{/if}}", &json!({"c": 1, "d":1}))
.render_template(
"{{#if a}}1{{else if b}}2{{else if c}}3{{else if d}}4{{else}}0{{/if}}",
&json!({"c": 1, "d":1})
)
.unwrap()
);
}
Expand All @@ -127,7 +130,10 @@ mod test {
assert_eq!(
"4".to_owned(),
handlebars
.render_template("{{#if a}}1{{else if b}}2{{else if c}}3{{else if d}}4{{/if}}", &json!({"d":1}))
.render_template(
"{{#if a}}1{{else if b}}2{{else if c}}3{{else if d}}4{{/if}}",
&json!({"d":1})
)
.unwrap()
);
}
Expand Down
8 changes: 7 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,13 @@ impl Parameter {
}

impl Renderable for HelperTemplate {
fn render<'reg: 'rc, 'rc>(&'rc self, registry: &'reg Registry<'reg>, ctx: &'rc Context, rc: &mut RenderContext<'reg, 'rc>, out: &mut dyn Output) -> Result<(), RenderError> {
fn render<'reg: 'rc, 'rc>(
&'rc self,
registry: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> Result<(), RenderError> {
render_helper(self, registry, ctx, rc, out)
}
}
Expand Down
47 changes: 15 additions & 32 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl HelperTemplate {
block,
template: None,
inverse: None,
chain: false
chain: false,
}
}

Expand All @@ -162,7 +162,7 @@ impl HelperTemplate {
block,
template: None,
inverse: None,
chain: true
chain: true,
}
}

Expand All @@ -176,29 +176,25 @@ impl HelperTemplate {
template: None,
inverse: None,
block: false,
chain: false
chain: false,
}
}

pub(crate) fn is_name_only(&self) -> bool {
!self.block && self.params.is_empty() && self.hash.is_empty()
}

fn insert_inverse_node(&mut self, mut node: Box<HelperTemplate>)
{
fn insert_inverse_node(&mut self, mut node: Box<HelperTemplate>) {
// Create a list in "inverse" member to hold the else-chain.
// Here we create the new template to save the else-chain node.
// The template render could render it successfully without any code add.
let mut new_chain_template = Template::new();
node.inverse = self.inverse.take();
new_chain_template.elements.push(HelperBlock(node));
self.inverse = Some(new_chain_template);

}


fn ref_chain_head_mut(&mut self) -> Option<&mut Box<HelperTemplate>>
{
fn ref_chain_head_mut(&mut self) -> Option<&mut Box<HelperTemplate>> {
if self.chain {
if let Some(inverse_tmpl) = &mut self.inverse {
assert_eq!(inverse_tmpl.elements.len(), 1);
Expand All @@ -210,30 +206,24 @@ impl HelperTemplate {
None
}

fn set_chain_template(&mut self, tmpl: Option<Template>)
{
fn set_chain_template(&mut self, tmpl: Option<Template>) {
if let Some(hepler) = self.ref_chain_head_mut() {
hepler.template = tmpl;
}else{
} else {
self.template = tmpl;
}

}

fn revert_chain_and_set(&mut self, inverse: Option<Template>)
{
if self.chain{

fn revert_chain_and_set(&mut self, inverse: Option<Template>) {
if self.chain {
let mut prev = None;

if let Some(head) = self.ref_chain_head_mut() {
if head.template.is_some() {

// Here the prev will hold the head inverse template.
// It will be set when reverse the chain.
prev = inverse;
}else{

} else {
// If the head already has template. set the inverse template.
head.template = inverse;
}
Expand All @@ -250,29 +240,24 @@ impl HelperTemplate {
}

self.inverse = prev;

}else{

} else {
// If the helper has no else chain.
// set the template to self.
if self.template.is_some() {
self.inverse = inverse;
}else{
} else {
self.template = inverse;
}
}
}

fn set_chained(&mut self)
{
fn set_chained(&mut self) {
self.chain = true;
}

pub fn is_chained(&self) -> bool
{
pub fn is_chained(&self) -> bool {
self.chain
}

}

#[derive(PartialEq, Eq, Clone, Debug)]
Expand Down Expand Up @@ -806,7 +791,7 @@ impl Template {

let t = template_stack.pop_front().unwrap();
let h = helper_stack.front_mut().unwrap();

if rule == Rule::invert_chain_tag {
h.set_chained();
}
Expand All @@ -815,7 +800,6 @@ impl Template {
if rule == Rule::invert_chain_tag {
h.insert_inverse_node(Box::new(HelperTemplate::new_chain(exp, true)));
}

}

Rule::raw_block_text => {
Expand Down Expand Up @@ -904,7 +888,6 @@ impl Template {
let mut h = helper_stack.pop_front().unwrap();
let close_tag_name = exp.name.as_name();
if h.name.as_name() == close_tag_name {

let prev_t = template_stack.pop_front().unwrap();
h.revert_chain_and_set(Some(prev_t));

Expand Down

0 comments on commit 8f16353

Please sign in to comment.