aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/parser/mod.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-01 16:12:54 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 10:27:15 +0200
commit1a9f5a16fb54d6c75a8e36aafa9703cc29a0a76a (patch)
treed763bdefbcb5d53b5256762064e29b3be16af416 /askama_derive/src/parser/mod.rs
parent2912ec92f83357e6909f13674529546fa73878ac (diff)
downloadaskama-1a9f5a16fb54d6c75a8e36aafa9703cc29a0a76a.tar.gz
askama-1a9f5a16fb54d6c75a8e36aafa9703cc29a0a76a.tar.bz2
askama-1a9f5a16fb54d6c75a8e36aafa9703cc29a0a76a.zip
derive: move Parsed into parser module
Diffstat (limited to '')
-rw-r--r--askama_derive/src/parser/mod.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/askama_derive/src/parser/mod.rs b/askama_derive/src/parser/mod.rs
index 0bf68ca..8da96f5 100644
--- a/askama_derive/src/parser/mod.rs
+++ b/askama_derive/src/parser/mod.rs
@@ -56,6 +56,40 @@ impl From<char> for Whitespace {
}
}
+mod _parsed {
+ use std::mem;
+
+ use super::{parse, Node, ParseError, Syntax};
+
+ pub(crate) struct Parsed {
+ #[allow(dead_code)]
+ source: String,
+ nodes: Vec<Node<'static>>,
+ }
+
+ impl Parsed {
+ pub(crate) fn new(source: String, syntax: &Syntax<'_>) -> Result<Self, ParseError> {
+ // Self-referential borrowing: `self` will keep the source alive as `String`,
+ // 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 = match parse(src, syntax) {
+ Ok(nodes) => nodes,
+ Err(e) => return Err(e),
+ };
+
+ Ok(Self { source, nodes })
+ }
+
+ // The return value's lifetime must be limited to `self` to uphold the unsafe invariant.
+ pub(crate) fn nodes(&self) -> &[Node<'_>] {
+ &self.nodes
+ }
+ }
+}
+
+pub(crate) use _parsed::Parsed;
+
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)) => {