aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2022-02-03 19:20:53 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-02-07 09:33:22 +0100
commit0aab78c6775493fb13fabce2b5eaef7ac83e6665 (patch)
tree13d55da7969824ad2b5295f18ebd8068aebdcaed /askama_shared
parente808b2a43ece9892314f68028679ec68566b21b3 (diff)
downloadaskama-0aab78c6775493fb13fabce2b5eaef7ac83e6665.tar.gz
askama-0aab78c6775493fb13fabce2b5eaef7ac83e6665.tar.bz2
askama-0aab78c6775493fb13fabce2b5eaef7ac83e6665.zip
Enable tracking of the offending span of an error
Diffstat (limited to 'askama_shared')
-rw-r--r--askama_shared/src/lib.rs29
1 files changed, 24 insertions, 5 deletions
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())
}
}