diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2022-02-03 19:20:53 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2022-02-07 09:33:22 +0100 |
commit | 0aab78c6775493fb13fabce2b5eaef7ac83e6665 (patch) | |
tree | 13d55da7969824ad2b5295f18ebd8068aebdcaed | |
parent | e808b2a43ece9892314f68028679ec68566b21b3 (diff) | |
download | askama-0aab78c6775493fb13fabce2b5eaef7ac83e6665.tar.gz askama-0aab78c6775493fb13fabce2b5eaef7ac83e6665.tar.bz2 askama-0aab78c6775493fb13fabce2b5eaef7ac83e6665.zip |
Enable tracking of the offending span of an error
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/lib.rs | 5 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 29 |
2 files changed, 25 insertions, 9 deletions
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 4d81433..2409ba4 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -9,7 +9,6 @@ use askama_shared::{ generator, get_template_source, read_config_file, CompileError, Config, Integrations, }; use proc_macro::TokenStream; -use proc_macro2::Span; use std::collections::HashMap; use std::path::PathBuf; @@ -19,9 +18,7 @@ pub fn derive_template(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); match build_template(&ast) { Ok(source) => source.parse().unwrap(), - Err(e) => syn::Error::new(Span::call_site(), e) - .to_compile_error() - .into(), + Err(e) => e.to_compile_error().into(), } } 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: Into<Cow<'static, str>>>(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<String> for CompileError { #[inline] fn from(s: String) -> Self { - Self(s.into()) + Self::new(s, Span::call_site()) } } |