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 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<CompiledTemplate> = 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<CompiledTemplate> = 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 {

View File

@ -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<Filter>,
@ -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<Path<'a>>,
@ -101,33 +101,33 @@ pub struct ParameterizedBlock<'a> {
pub else_contents: Option<Body<'a>>,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, PartialEq)]
pub struct Partial<'a> {
pub name: Vec<PartialNameElement>,
pub explicit_context: Option<Path<'a>>,
pub params: Vec<KVPair<'a>>,
}
#[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<PartialNameElement>),
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<TemplateElement<'a>>,
}
#[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>),

View File

@ -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;

View File

@ -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<String, &'a Template<'a>>,
}
pub fn compile_template<'a>(
source: &'a str,
name: String,
) -> Result<CompiledTemplate<'a>, 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<Template<'a>, 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)