diff --git a/src/main.rs b/src/main.rs index 2817621..dd53c26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -176,7 +176,7 @@ fn run_command(args: Args) -> Result<()> { false => EvalMode::Local, }; let mut eval_ctx = yolk.prepare_eval_ctx_for_templates(mode)?; - let result = yolk.eval_template(&mut eval_ctx, &text)?; + let result = yolk.eval_template(&mut eval_ctx, "inline", &text)?; println!("{}", result); } } diff --git a/src/templating/document.rs b/src/templating/document.rs index b5c0ca7..aa6c385 100644 --- a/src/templating/document.rs +++ b/src/templating/document.rs @@ -6,7 +6,7 @@ use super::{ parser::{comment_style::CommentStyle, document_parser::DocumentParser, Rule, YolkParser}, }; -use miette::{Diagnostic, LabeledSpan, Result, SourceCode}; +use miette::{Diagnostic, LabeledSpan, Result}; use pest::Parser as _; #[derive(Debug)] @@ -33,12 +33,12 @@ pub enum ParseError { } impl Diagnostic for ParseError { - fn source_code(&self) -> Option<&dyn SourceCode> { - match self { - ParseError::Pest(text, _) => Some(text), - ParseError::DocumentParser(text, _) => Some(text), - } - } + // fn source_code(&self) -> Option<&dyn SourceCode> { + // match self { + // ParseError::Pest(text, _) => Some(text), + // ParseError::DocumentParser(text, _) => Some(text), + // } + // } fn labels(&self) -> Option + '_>> { match self { ParseError::Pest(_, e) => Some(Box::new(std::iter::once(LabeledSpan::at( diff --git a/src/templating/parser/linewise.rs b/src/templating/parser/linewise.rs index ed7a2f4..6e1188b 100644 --- a/src/templating/parser/linewise.rs +++ b/src/templating/parser/linewise.rs @@ -82,9 +82,8 @@ impl<'a> ParsedLine<'a> { } #[allow(unused)] pub fn try_from_str(s: &'a str) -> Result { - let mut result = YolkParser::parse(Rule::Line, s) - .into_diagnostic() - .map_err(|e| e.with_source_code(s.to_string()))?; + let mut result = YolkParser::parse(Rule::Line, s).into_diagnostic()?; + // .map_err(|e| e.with_source_code(s.to_string()))?; Ok(Self::from_pair(result.next().unwrap())) } diff --git a/src/test.rs b/src/test.rs index 81da9f4..1922a77 100644 --- a/src/test.rs +++ b/src/test.rs @@ -14,7 +14,7 @@ pub fn test_custom_functions_in_text_transformer_tag() -> TestResult { let mut eval_ctx = yolk.prepare_eval_ctx_for_templates(crate::yolk::EvalMode::Local)?; assert_eq!( "TEST{< scream() >}", - yolk.eval_template(&mut eval_ctx, "test{< scream() >}")? + yolk.eval_template(&mut eval_ctx, "", "test{< scream() >}")? ); Ok(()) diff --git a/src/yolk.rs b/src/yolk.rs index 106510b..fa67bdf 100644 --- a/src/yolk.rs +++ b/src/yolk.rs @@ -1,7 +1,7 @@ use std::path::{Path, PathBuf}; use fs_err::PathExt as _; -use miette::{Context, IntoDiagnostic, Result}; +use miette::{Context, IntoDiagnostic, NamedSource, Result}; use mlua::Value; use crate::{ @@ -164,20 +164,30 @@ impl Yolk { } /// Evaluate a templated file and return the rendered content. - pub fn eval_template(&self, eval_ctx: &mut EvalCtx, content: &str) -> Result { - let doc = Document::parse_string(content).context("Failed to parse document")?; - doc.render(eval_ctx) - .map_err(|e| e.with_source_code(content.to_string())) - .context("Failed to render document") + /// + /// The `file_path` is just used for error reporting. + pub fn eval_template( + &self, + eval_ctx: &mut EvalCtx, + file_path: &str, + content: &str, + ) -> Result { + let mut eval = move || { + let doc = Document::parse_string(content).context("Failed to parse document")?; + doc.render(eval_ctx).context("Failed to render document") + }; + eval().map_err(|e| e.with_source_code(NamedSource::new(file_path, content.to_string()))) } /// Sync a single template file in place on the filesystem. pub fn sync_template_file(&self, eval_ctx: &mut EvalCtx, path: impl AsRef) -> Result<()> { tracing::info!("Syncing file {}", path.as_ref().display()); let content = fs_err::read_to_string(&path).into_diagnostic()?; - let rendered = self.eval_template(eval_ctx, &content).with_context(|| { - format!("Failed to eval template file: {}", path.as_ref().display()) - })?; + let rendered = self + .eval_template(eval_ctx, &path.as_ref().to_string_lossy(), &content) + .with_context(|| { + format!("Failed to eval template file: {}", path.as_ref().display()) + })?; fs_err::write(&path, rendered).into_diagnostic()?; Ok(()) }