aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-01 15:44:04 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 10:27:15 +0200
commit91e3c88d8f6428d41326869bbd8f6ece99145627 (patch)
tree76308a10c6de2e7fd4c1ba63924a261197a74370 /askama_derive
parent8b184511edff62bd3786be3f5d62c1965430278b (diff)
downloadaskama-91e3c88d8f6428d41326869bbd8f6ece99145627.tar.gz
askama-91e3c88d8f6428d41326869bbd8f6ece99145627.tar.bz2
askama-91e3c88d8f6428d41326869bbd8f6ece99145627.zip
derive: define separate ParseError type
Diffstat (limited to 'askama_derive')
-rw-r--r--askama_derive/src/generator.rs6
-rw-r--r--askama_derive/src/lib.rs8
-rw-r--r--askama_derive/src/parser/mod.rs23
-rw-r--r--askama_derive/src/parser/tests.rs4
4 files changed, 32 insertions, 9 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index f8b9ecb..f17b6f6 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -259,7 +259,11 @@ mod _parsed {
// internally we will transmute it to `&'static str` to satisfy the compiler.
// However, we only expose the nodes with a lifetime limited to `self`.
let src = unsafe { mem::transmute::<&str, &'static str>(source.as_str()) };
- let nodes = parse(src, syntax)?;
+ let nodes = match parse(src, syntax) {
+ Ok(nodes) => nodes,
+ Err(e) => return Err(e.to_string().into()),
+ };
+
Ok(Self { source, nodes })
}
diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs
index 0683e71..1483438 100644
--- a/askama_derive/src/lib.rs
+++ b/askama_derive/src/lib.rs
@@ -12,6 +12,7 @@ mod generator;
mod heritage;
mod input;
mod parser;
+use parser::ParseError;
#[proc_macro_derive(Template, attributes(template))]
pub fn derive_template(input: TokenStream) -> TokenStream {
@@ -48,6 +49,13 @@ impl fmt::Display for CompileError {
}
}
+impl From<ParseError> for CompileError {
+ #[inline]
+ fn from(e: ParseError) -> Self {
+ Self::new(e.to_string(), Span::call_site())
+ }
+}
+
impl From<&'static str> for CompileError {
#[inline]
fn from(s: &'static str) -> Self {
diff --git a/askama_derive/src/parser/mod.rs b/askama_derive/src/parser/mod.rs
index f81bcc8..0bf68ca 100644
--- a/askama_derive/src/parser/mod.rs
+++ b/askama_derive/src/parser/mod.rs
@@ -1,5 +1,5 @@
use std::cell::Cell;
-use std::str;
+use std::{fmt, str};
use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, tag, take_till};
@@ -13,7 +13,6 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition};
pub(crate) use self::expr::Expr;
pub(crate) use self::node::{Cond, CondTest, Loop, Macro, Node, Target, When, Whitespace, Ws};
-use crate::CompileError;
mod expr;
mod node;
@@ -57,11 +56,11 @@ impl From<char> for Whitespace {
}
}
-pub(crate) fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a>>, CompileError> {
+pub(crate) fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a>>, ParseError> {
match Node::parse(src, &State::new(syntax)) {
Ok((left, res)) => {
if !left.is_empty() {
- Err(format!("unable to parse template:\n\n{left:?}").into())
+ Err(ParseError(format!("unable to parse template:\n\n{left:?}")))
} else {
Ok(res)
}
@@ -86,10 +85,22 @@ pub(crate) fn parse<'a>(src: &'a str, syntax: &Syntax<'_>) -> Result<Vec<Node<'a
column,
source_after,
);
- Err(msg.into())
+
+ Err(ParseError(msg))
}
- Err(nom::Err::Incomplete(_)) => Err("parsing incomplete".into()),
+ Err(nom::Err::Incomplete(_)) => Err(ParseError("parsing incomplete".into())),
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(crate) struct ParseError(String);
+
+impl std::error::Error for ParseError {}
+
+impl fmt::Display for ParseError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ self.0.fmt(f)
}
}
diff --git a/askama_derive/src/parser/tests.rs b/askama_derive/src/parser/tests.rs
index 68cf14b..0e785eb 100644
--- a/askama_derive/src/parser/tests.rs
+++ b/askama_derive/src/parser/tests.rs
@@ -260,7 +260,7 @@ fn test_rust_macro() {
assert_eq!(
&*super::parse("{{a.b.c!( hello )}}", &syntax)
.unwrap_err()
- .msg,
+ .to_string(),
"problems parsing template source at row 1, column 7 near:\n\"!( hello )}}\"",
);
}
@@ -706,7 +706,7 @@ fn test_missing_space_after_kw() {
let syntax = Syntax::default();
let err = super::parse("{%leta=b%}", &syntax).unwrap_err();
assert!(matches!(
- &*err.msg,
+ &*err.to_string(),
"unable to parse template:\n\n\"{%leta=b%}\""
));
}