From f1ec0ffb9eea19df68e276f895e77c753c26f7bb Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 31 May 2020 19:01:51 -0400 Subject: [PATCH] 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. --- src/bin.rs | 42 ++++++++++++++++++++++++---------------- src/parser/parser.rs | 30 ++++++++++++++-------------- src/renderer/mod.rs | 2 +- src/renderer/renderer.rs | 26 +++++++------------------ 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/bin.rs b/src/bin.rs index 43690e1..845a3ae 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -3,8 +3,9 @@ extern crate nom; use crate::renderer::CompareContextElement; use parser::Filter; use parser::OwnedLiteral; +use parser::Template; use renderer::compile_template; -use renderer::CompiledTemplate; +use renderer::CompileError; use renderer::ContextElement; use renderer::DustRenderer; use renderer::IntoContextElement; @@ -38,18 +39,18 @@ fn main() { (p.to_string(), template_content) }) .collect(); - let compiled_templates: Vec = template_contents - .iter() - .map(|(p, contents)| template_from_file(p, contents)) - .collect(); - let mut dust_renderer = DustRenderer::new(); - compiled_templates.iter().for_each(|template| { - dust_renderer.load_source(template); - }); - let main_template_name = &compiled_templates - .first() - .expect("There should be more than 1 template") - .name; + // let compiled_templates: Vec = template_contents + // .iter() + // .map(|(p, contents)| template_from_file(p, contents)) + // .collect(); + // let mut dust_renderer = DustRenderer::new(); + // compiled_templates.iter().for_each(|template| { + // dust_renderer.load_source(template); + // }); + // let main_template_name = &compiled_templates + // .first() + // .expect("There should be more than 1 template") + // .name; // let breadcrumbs = vec![&context as &dyn IntoContextElement]; // 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 name = path.file_stem().unwrap(); - compile_template(file_contents, name.to_string_lossy().to_string()) - .expect("Failed to compile template") + let name = path.file_stem().ok_or(CompileError { + message: format!("Failed to get file stem on {}", file_path), + })?; + Ok(( + name.to_string_lossy().to_string(), + compile_template(file_contents)?, + )) } fn read_context_from_stdin() -> serde_json::Value { diff --git a/src/parser/parser.rs b/src/parser/parser.rs index fa42cb7..8598f9f 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -23,7 +23,7 @@ use nom::sequence::terminated; use nom::sequence::tuple; use nom::IResult; -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub enum DustTag<'a> { DTSpecial(Special), DTComment(Comment<'a>), @@ -52,12 +52,12 @@ pub enum Special { RightCurlyBrace, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub enum IgnoredWhitespace<'a> { StartOfLine(&'a str), } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Comment<'a> { value: &'a str, } @@ -65,12 +65,12 @@ pub struct Comment<'a> { /// 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 -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Path<'a> { pub keys: Vec<&'a str>, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Reference<'a> { pub path: Path<'a>, pub filters: Vec, @@ -87,12 +87,12 @@ pub enum Filter { JsonParse, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Span<'a> { pub contents: &'a str, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct ParameterizedBlock<'a> { pub path: Path<'a>, pub explicit_context: Option>, @@ -101,33 +101,33 @@ pub struct ParameterizedBlock<'a> { pub else_contents: Option>, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Partial<'a> { pub name: Vec, pub explicit_context: Option>, pub params: Vec>, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub enum OwnedLiteral { LString(String), LPositiveInteger(u64), } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub enum RValue<'a> { RVPath(Path<'a>), RVTemplate(Vec), RVLiteral(OwnedLiteral), } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct KVPair<'a> { pub key: &'a str, pub value: RValue<'a>, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub enum PartialNameElement { PNSpan { contents: String, @@ -138,17 +138,17 @@ pub enum PartialNameElement { }, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Body<'a> { pub elements: Vec>, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct Template<'a> { pub contents: Body<'a>, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub enum TemplateElement<'a> { TESpan(Span<'a>), TETag(DustTag<'a>), diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 799cba4..2ebbc51 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -22,5 +22,5 @@ pub use errors::CompileError; pub use errors::RenderError; pub use errors::WalkError; pub use renderer::compile_template; -pub use renderer::CompiledTemplate; +// pub use renderer::CompiledTemplate; pub use renderer::DustRenderer; diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 75d45b2..c8e1d67 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -9,27 +9,16 @@ use crate::renderer::errors::CompileError; use crate::renderer::tree_walking::walk_path; use std::collections::HashMap; -#[derive(Clone, Debug)] -pub struct CompiledTemplate<'a> { - template: Template<'a>, - pub name: String, -} - #[derive(Clone, Debug)] pub struct DustRenderer<'a> { templates: HashMap>, } -pub fn compile_template<'a>( - source: &'a str, - name: String, -) -> Result, CompileError> { - // TODO: This could use better error management - let (_remaining, parsed_template) = template(source).expect("Failed to compile template"); - Ok(CompiledTemplate { - template: parsed_template, - name: name, - }) +pub fn compile_template<'a>(source: &'a str) -> Result, CompileError> { + let (_remaining, parsed_template) = template(source).map_err(|err| CompileError { + message: "Failed to compile template".to_owned(), + })?; + Ok(parsed_template) } impl<'a> DustRenderer<'a> { @@ -39,9 +28,8 @@ impl<'a> DustRenderer<'a> { } } - pub fn load_source(&mut self, template: &'a CompiledTemplate) { - self.templates - .insert(template.name.clone(), &template.template); + pub fn load_source(&mut self, template: &'a Template, name: String) { + self.templates.insert(name, template); } /// Returns a option of a tuple of (parent, new_node_elements)