From 1a9f5a16fb54d6c75a8e36aafa9703cc29a0a76a Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 1 Jul 2023 16:12:54 +0200 Subject: derive: move Parsed into parser module --- askama_derive/src/generator.rs | 37 +------------------------------------ askama_derive/src/parser/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index e2d657f..b6c6151 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1,7 +1,7 @@ use crate::config::{get_template_source, read_config_file, Config, WhitespaceHandling}; use crate::heritage::{Context, Heritage}; use crate::input::{Print, Source, TemplateInput}; -use crate::parser::{Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws}; +use crate::parser::{Cond, CondTest, Expr, Loop, Node, Parsed, Target, When, Whitespace, Ws}; use crate::CompileError; use proc_macro::TokenStream; @@ -241,41 +241,6 @@ fn find_used_templates( Ok(()) } -mod _parsed { - use std::mem; - - use crate::parser::{parse, Node, Syntax}; - use crate::CompileError; - - pub(super) struct Parsed { - #[allow(dead_code)] - source: String, - nodes: Vec>, - } - - impl Parsed { - pub(super) fn new(source: String, syntax: &Syntax<'_>) -> Result { - // 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.to_string().into()), - }; - - Ok(Self { source, nodes }) - } - - // The return value's lifetime must be limited to `self` to uphold the unsafe invariant. - pub(super) fn nodes(&self) -> &[Node<'_>] { - &self.nodes - } - } -} - -use _parsed::Parsed; - struct Generator<'a> { // The template input state: original struct AST and attributes input: &'a TemplateInput<'a>, 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 for Whitespace { } } +mod _parsed { + use std::mem; + + use super::{parse, Node, ParseError, Syntax}; + + pub(crate) struct Parsed { + #[allow(dead_code)] + source: String, + nodes: Vec>, + } + + impl Parsed { + pub(crate) fn new(source: String, syntax: &Syntax<'_>) -> Result { + // 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>, ParseError> { match Node::parse(src, &State::new(syntax)) { Ok((left, res)) => { -- cgit