diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-07-01 16:12:54 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-07-31 10:27:15 +0200 |
commit | 1a9f5a16fb54d6c75a8e36aafa9703cc29a0a76a (patch) | |
tree | d763bdefbcb5d53b5256762064e29b3be16af416 | |
parent | 2912ec92f83357e6909f13674529546fa73878ac (diff) | |
download | askama-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/generator.rs | 37 | ||||
-rw-r--r-- | 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<Node<'static>>, - } - - impl Parsed { - pub(super) fn new(source: String, syntax: &Syntax<'_>) -> Result<Self, CompileError> { - // 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<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)) => { |