From 0aab78c6775493fb13fabce2b5eaef7ac83e6665 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Thu, 3 Feb 2022 19:20:53 +0100 Subject: Enable tracking of the offending span of an error --- askama_shared/src/lib.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'askama_shared/src/lib.rs') diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index b2bebd0..994662c 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -9,6 +9,7 @@ use std::convert::TryFrom; use std::path::{Path, PathBuf}; use std::{env, fmt, fs}; +use proc_macro2::{Span, TokenStream}; #[cfg(feature = "serde")] use serde::Deserialize; @@ -292,27 +293,45 @@ static DEFAULT_ESCAPERS: &[(&[&str], &str)] = &[ (&["j2", "jinja", "jinja2"], "::askama::Html"), ]; -#[derive(Debug)] -pub struct CompileError(Cow<'static, str>); +#[derive(Debug, Clone)] +pub struct CompileError { + msg: Cow<'static, str>, + span: Span, +} + +impl CompileError { + pub fn new>>(s: S, span: Span) -> Self { + Self { + msg: s.into(), + span, + } + } + + pub fn to_compile_error(self) -> TokenStream { + syn::Error::new(self.span, self.msg).to_compile_error() + } +} + +impl std::error::Error for CompileError {} impl fmt::Display for CompileError { #[inline] fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.write_str(&self.0) + fmt.write_str(&self.msg) } } impl From<&'static str> for CompileError { #[inline] fn from(s: &'static str) -> Self { - Self(s.into()) + Self::new(s, Span::call_site()) } } impl From for CompileError { #[inline] fn from(s: String) -> Self { - Self(s.into()) + Self::new(s, Span::call_site()) } } -- cgit