@ -1,23 +1,18 @@
use crate ::parser ::Body ;
use crate ::parser ::DustTag ;
use crate ::parser ::Template ;
use crate ::parser ::{ Path , TemplateElement } ;
use crate ::parser ::TemplateElement ;
use std ::collections ::HashMap ;
pub struct InlinePartialTreeValue < ' a > {
explicit_context : & ' a Option < Path < ' a > > ,
body : & ' a Option < Body < ' a > > ,
}
pub struct InlinePartialTreeElement < ' a > {
parent : Option < & ' a InlinePartialTreeElement < ' a > > ,
blocks : HashMap < & ' a str , InlinePartialTreeValue < ' a > > ,
blocks : HashMap < & ' a str , & ' a Option < Body < ' a > > > ,
}
impl < ' a > InlinePartialTreeElement < ' a > {
pub fn new (
parent : Option < & ' a InlinePartialTreeElement < ' a > > ,
blocks : HashMap < & ' a str , InlinePartialTreeValue < ' a > > ,
blocks : HashMap < & ' a str , & ' a Option < Body < ' a > > > ,
) -> InlinePartialTreeElement < ' a > {
InlinePartialTreeElement {
parent : parent ,
@ -25,40 +20,21 @@ impl<'a> InlinePartialTreeElement<'a> {
}
}
fn get_value ( & self , name : & str ) -> Option < & InlinePartialTreeValue < ' a > > {
pub fn get_block ( & self , name : & str ) -> Option < & ' a Option < Body < ' a > > > {
match self . blocks . get ( name ) {
None = > match self . parent {
None = > None ,
Some ( parent_tree_element ) = > parent_tree_element . get_ value ( name ) ,
Some ( parent_tree_element ) = > parent_tree_element . get_ block ( name ) ,
} ,
Some ( interior ) = > Some ( interior ) ,
}
}
/// Recursively get the body of a block by name
///
/// Returning a double-option because there is a functional
/// difference between not finding the block and not having a
/// body.
pub fn get_block ( & self , name : & str ) -> Option < & ' a Option < Body < ' a > > > {
self . get_value ( name ) . map ( | interior | interior . body )
}
/// Recursively get the explicit from the inline partial by name
///
/// Returning a double-option because there is a functional
/// difference between not finding the block and not having a
/// body.
pub fn get_explicit_context ( & self , name : & str ) -> Option < & Option < Path < ' a > > > {
self . get_value ( name )
. map ( | interior | interior . explicit_context )
}
}
pub fn extract_inline_partials < ' a > (
template : & ' a Template < ' a > ,
) -> HashMap < & ' a str , InlinePartialTreeValue < ' a > > {
let mut blocks : HashMap < & ' a str , InlinePartialTreeValue < ' a > > = HashMap ::new ( ) ;
) -> HashMap < & ' a str , & ' a Option < Body < ' a > > > {
let mut blocks : HashMap < & ' a str , & ' a Option < Body < ' a > > > = HashMap ::new ( ) ;
extract_inline_partials_from_body ( & mut blocks , & template . contents ) ;
@ -66,7 +42,7 @@ pub fn extract_inline_partials<'a>(
}
fn extract_inline_partials_from_body < ' a , ' b > (
blocks : & ' b mut HashMap < & ' a str , InlinePartialTreeValue < ' a > > ,
blocks : & ' b mut HashMap < & ' a str , & ' a Option < Body < ' a > > > ,
body : & ' a Body < ' a > ,
) {
for elem in & body . elements {
@ -81,7 +57,7 @@ fn extract_inline_partials_from_body<'a, 'b>(
}
fn extract_inline_partials_from_tag < ' a , ' b > (
blocks : & ' b mut HashMap < & ' a str , InlinePartialTreeValue < ' a > > ,
blocks : & ' b mut HashMap < & ' a str , & ' a Option < Body < ' a > > > ,
tag : & ' a DustTag ,
) {
match tag {
@ -121,13 +97,7 @@ fn extract_inline_partials_from_tag<'a, 'b>(
}
DustTag ::DTPartial ( .. ) = > ( ) ,
DustTag ::DTInlinePartial ( named_block ) = > {
blocks . insert (
& named_block . name ,
InlinePartialTreeValue {
explicit_context : & named_block . explicit_context ,
body : & named_block . contents ,
} ,
) ;
blocks . insert ( & named_block . name , & named_block . contents ) ;
}
DustTag ::DTBlock ( .. ) = > ( ) ,
DustTag ::DTHelperEquals ( parameterized_block ) = > {