aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--askama_derive/src/lib.rs5
-rw-r--r--askama_shared/src/lib.rs29
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())
}
}