Got rid of most of the Clone traits on the parser types since some of the parser results now contain owned values rather than just references.

This commit is contained in:
Tom Alexander 2020-05-31 19:01:51 -04:00
parent 15c8e3bf28
commit f1ec0ffb9e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 48 additions and 52 deletions

View File

@ -3,8 +3,9 @@ extern crate nom;
use crate::renderer::CompareContextElement; use crate::renderer::CompareContextElement;
use parser::Filter; use parser::Filter;
use parser::OwnedLiteral; use parser::OwnedLiteral;
use parser::Template;
use renderer::compile_template; use renderer::compile_template;
use renderer::CompiledTemplate; use renderer::CompileError;
use renderer::ContextElement; use renderer::ContextElement;
use renderer::DustRenderer; use renderer::DustRenderer;
use renderer::IntoContextElement; use renderer::IntoContextElement;
@ -38,18 +39,18 @@ fn main() {
(p.to_string(), template_content) (p.to_string(), template_content)
}) })
.collect(); .collect();
let compiled_templates: Vec<CompiledTemplate> = template_contents // let compiled_templates: Vec<CompiledTemplate> = template_contents
.iter() // .iter()
.map(|(p, contents)| template_from_file(p, contents)) // .map(|(p, contents)| template_from_file(p, contents))
.collect(); // .collect();
let mut dust_renderer = DustRenderer::new(); // let mut dust_renderer = DustRenderer::new();
compiled_templates.iter().for_each(|template| { // compiled_templates.iter().for_each(|template| {
dust_renderer.load_source(template); // dust_renderer.load_source(template);
}); // });
let main_template_name = &compiled_templates // let main_template_name = &compiled_templates
.first() // .first()
.expect("There should be more than 1 template") // .expect("There should be more than 1 template")
.name; // .name;
// let breadcrumbs = vec![&context as &dyn IntoContextElement]; // let breadcrumbs = vec![&context as &dyn IntoContextElement];
// println!( // println!(
// "{}", // "{}",
@ -59,11 +60,18 @@ fn main() {
// ); // );
} }
fn template_from_file<'a>(file_path: &str, file_contents: &'a str) -> CompiledTemplate<'a> { fn template_from_file<'a>(
file_path: &str,
file_contents: &'a str,
) -> Result<(String, Template<'a>), CompileError> {
let path: &Path = Path::new(file_path); let path: &Path = Path::new(file_path);
let name = path.file_stem().unwrap(); let name = path.file_stem().ok_or(CompileError {
compile_template(file_contents, name.to_string_lossy().to_string()) message: format!("Failed to get file stem on {}", file_path),
.expect("Failed to compile template") })?;
Ok((
name.to_string_lossy().to_string(),
compile_template(file_contents)?,
))
} }
fn read_context_from_stdin() -> serde_json::Value { fn read_context_from_stdin() -> serde_json::Value {

View File

@ -23,7 +23,7 @@ use nom::sequence::terminated;
use nom::sequence::tuple; use nom::sequence::tuple;
use nom::IResult; use nom::IResult;
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum DustTag<'a> { pub enum DustTag<'a> {
DTSpecial(Special), DTSpecial(Special),
DTComment(Comment<'a>), DTComment(Comment<'a>),
@ -52,12 +52,12 @@ pub enum Special {
RightCurlyBrace, RightCurlyBrace,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum IgnoredWhitespace<'a> { pub enum IgnoredWhitespace<'a> {
StartOfLine(&'a str), StartOfLine(&'a str),
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Comment<'a> { pub struct Comment<'a> {
value: &'a str, value: &'a str,
} }
@ -65,12 +65,12 @@ pub struct Comment<'a> {
/// A series of keys separated by '.' to reference a variable in the context /// A series of keys separated by '.' to reference a variable in the context
/// ///
/// Special case: If the path is just "." then keys will be an empty vec /// Special case: If the path is just "." then keys will be an empty vec
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Path<'a> { pub struct Path<'a> {
pub keys: Vec<&'a str>, pub keys: Vec<&'a str>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Reference<'a> { pub struct Reference<'a> {
pub path: Path<'a>, pub path: Path<'a>,
pub filters: Vec<Filter>, pub filters: Vec<Filter>,
@ -87,12 +87,12 @@ pub enum Filter {
JsonParse, JsonParse,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Span<'a> { pub struct Span<'a> {
pub contents: &'a str, pub contents: &'a str,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct ParameterizedBlock<'a> { pub struct ParameterizedBlock<'a> {
pub path: Path<'a>, pub path: Path<'a>,
pub explicit_context: Option<Path<'a>>, pub explicit_context: Option<Path<'a>>,
@ -101,33 +101,33 @@ pub struct ParameterizedBlock<'a> {
pub else_contents: Option<Body<'a>>, pub else_contents: Option<Body<'a>>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Partial<'a> { pub struct Partial<'a> {
pub name: Vec<PartialNameElement>, pub name: Vec<PartialNameElement>,
pub explicit_context: Option<Path<'a>>, pub explicit_context: Option<Path<'a>>,
pub params: Vec<KVPair<'a>>, pub params: Vec<KVPair<'a>>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum OwnedLiteral { pub enum OwnedLiteral {
LString(String), LString(String),
LPositiveInteger(u64), LPositiveInteger(u64),
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum RValue<'a> { pub enum RValue<'a> {
RVPath(Path<'a>), RVPath(Path<'a>),
RVTemplate(Vec<PartialNameElement>), RVTemplate(Vec<PartialNameElement>),
RVLiteral(OwnedLiteral), RVLiteral(OwnedLiteral),
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct KVPair<'a> { pub struct KVPair<'a> {
pub key: &'a str, pub key: &'a str,
pub value: RValue<'a>, pub value: RValue<'a>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum PartialNameElement { pub enum PartialNameElement {
PNSpan { PNSpan {
contents: String, contents: String,
@ -138,17 +138,17 @@ pub enum PartialNameElement {
}, },
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Body<'a> { pub struct Body<'a> {
pub elements: Vec<TemplateElement<'a>>, pub elements: Vec<TemplateElement<'a>>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Template<'a> { pub struct Template<'a> {
pub contents: Body<'a>, pub contents: Body<'a>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum TemplateElement<'a> { pub enum TemplateElement<'a> {
TESpan(Span<'a>), TESpan(Span<'a>),
TETag(DustTag<'a>), TETag(DustTag<'a>),

View File

@ -22,5 +22,5 @@ pub use errors::CompileError;
pub use errors::RenderError; pub use errors::RenderError;
pub use errors::WalkError; pub use errors::WalkError;
pub use renderer::compile_template; pub use renderer::compile_template;
pub use renderer::CompiledTemplate; // pub use renderer::CompiledTemplate;
pub use renderer::DustRenderer; pub use renderer::DustRenderer;

View File

@ -9,27 +9,16 @@ use crate::renderer::errors::CompileError;
use crate::renderer::tree_walking::walk_path; use crate::renderer::tree_walking::walk_path;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct CompiledTemplate<'a> {
template: Template<'a>,
pub name: String,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct DustRenderer<'a> { pub struct DustRenderer<'a> {
templates: HashMap<String, &'a Template<'a>>, templates: HashMap<String, &'a Template<'a>>,
} }
pub fn compile_template<'a>( pub fn compile_template<'a>(source: &'a str) -> Result<Template<'a>, CompileError> {
source: &'a str, let (_remaining, parsed_template) = template(source).map_err(|err| CompileError {
name: String, message: "Failed to compile template".to_owned(),
) -> Result<CompiledTemplate<'a>, CompileError> { })?;
// TODO: This could use better error management Ok(parsed_template)
let (_remaining, parsed_template) = template(source).expect("Failed to compile template");
Ok(CompiledTemplate {
template: parsed_template,
name: name,
})
} }
impl<'a> DustRenderer<'a> { impl<'a> DustRenderer<'a> {
@ -39,9 +28,8 @@ impl<'a> DustRenderer<'a> {
} }
} }
pub fn load_source(&mut self, template: &'a CompiledTemplate) { pub fn load_source(&mut self, template: &'a Template, name: String) {
self.templates self.templates.insert(name, template);
.insert(template.name.clone(), &template.template);
} }
/// Returns a option of a tuple of (parent, new_node_elements) /// Returns a option of a tuple of (parent, new_node_elements)