Skip to content

Commit

Permalink
Clean up error reporting structure
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Dec 5, 2024
1 parent 1d0a044 commit 47cf25b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/templating/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
match self {
ParseError::Pest(_, e) => Some(Box::new(std::iter::once(LabeledSpan::at(
Expand Down
5 changes: 2 additions & 3 deletions src/templating/parser/linewise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ impl<'a> ParsedLine<'a> {
}
#[allow(unused)]
pub fn try_from_str(s: &'a str) -> Result<Self> {
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()))
}

Expand Down
2 changes: 1 addition & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
28 changes: 19 additions & 9 deletions src/yolk.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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<String> {
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<String> {
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<Path>) -> 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(())
}
Expand Down

0 comments on commit 47cf25b

Please sign in to comment.