From 07584691a8b2ff44776d22c9f9b2c4ec1b303948 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Thu, 10 Mar 2022 09:02:10 +0100 Subject: Un-"pub" most of askama_shared's internals Previously askama_shared exported most of it's internals, so askama_derive could use them. This is not needed anymore. --- askama_shared/src/derive.rs | 1 + askama_shared/src/generator.rs | 2 +- askama_shared/src/heritage.rs | 22 ++++++++--------- askama_shared/src/input.rs | 32 ++++++++++++------------ askama_shared/src/lib.rs | 50 ++++++++++++++++++-------------------- askama_shared/src/parser.rs | 55 ++++++++++++++++++++++-------------------- 6 files changed, 81 insertions(+), 81 deletions(-) (limited to 'askama_shared/src') diff --git a/askama_shared/src/derive.rs b/askama_shared/src/derive.rs index 04b3ba0..b8f0a2f 100644 --- a/askama_shared/src/derive.rs +++ b/askama_shared/src/derive.rs @@ -9,6 +9,7 @@ use crate::parser::{parse, Expr, Node}; use crate::{generator, get_template_source, read_config_file, CompileError, Config}; /// The actual implementation for askama_derive::Template +#[doc(hidden)] pub fn derive_template(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse2(input).unwrap(); match build_template(&ast) { diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index def8c08..f7783af 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; use std::path::Path; use std::{cmp, hash, mem, str}; -pub fn generate( +pub(crate) fn generate( input: &TemplateInput<'_>, contexts: &HashMap<&Path, Context<'_>, S>, heritage: Option<&Heritage<'_>>, diff --git a/askama_shared/src/heritage.rs b/askama_shared/src/heritage.rs index 8dd97e2..49599af 100644 --- a/askama_shared/src/heritage.rs +++ b/askama_shared/src/heritage.rs @@ -4,13 +4,13 @@ use std::path::{Path, PathBuf}; use crate::parser::{Expr, Loop, Macro, Node}; use crate::{CompileError, Config}; -pub struct Heritage<'a> { - pub root: &'a Context<'a>, - pub blocks: BlockAncestry<'a>, +pub(crate) struct Heritage<'a> { + pub(crate) root: &'a Context<'a>, + pub(crate) blocks: BlockAncestry<'a>, } impl Heritage<'_> { - pub fn new<'n, S: std::hash::BuildHasher>( + pub(crate) fn new<'n, S: std::hash::BuildHasher>( mut ctx: &'n Context<'n>, contexts: &'n HashMap<&'n Path, Context<'n>, S>, ) -> Heritage<'n> { @@ -33,16 +33,16 @@ impl Heritage<'_> { type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a Node<'a>)>>; -pub struct Context<'a> { - pub nodes: &'a [Node<'a>], - pub extends: Option, - pub blocks: HashMap<&'a str, &'a Node<'a>>, - pub macros: HashMap<&'a str, &'a Macro<'a>>, - pub imports: HashMap<&'a str, PathBuf>, +pub(crate) struct Context<'a> { + pub(crate) nodes: &'a [Node<'a>], + pub(crate) extends: Option, + pub(crate) blocks: HashMap<&'a str, &'a Node<'a>>, + pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>, + pub(crate) imports: HashMap<&'a str, PathBuf>, } impl Context<'_> { - pub fn new<'n>( + pub(crate) fn new<'n>( config: &Config<'_>, path: &Path, nodes: &'n [Node<'n>], diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs index f7eac23..c70b250 100644 --- a/askama_shared/src/input.rs +++ b/askama_shared/src/input.rs @@ -6,17 +6,17 @@ use std::str::FromStr; use mime::Mime; use quote::ToTokens; -pub struct TemplateInput<'a> { - pub ast: &'a syn::DeriveInput, - pub config: &'a Config<'a>, - pub syntax: &'a Syntax<'a>, - pub source: Source, - pub print: Print, - pub escaper: &'a str, - pub ext: Option, - pub mime_type: String, - pub parent: Option<&'a syn::Type>, - pub path: PathBuf, +pub(crate) struct TemplateInput<'a> { + pub(crate) ast: &'a syn::DeriveInput, + pub(crate) config: &'a Config<'a>, + pub(crate) syntax: &'a Syntax<'a>, + pub(crate) source: Source, + pub(crate) print: Print, + pub(crate) escaper: &'a str, + pub(crate) ext: Option, + pub(crate) mime_type: String, + pub(crate) parent: Option<&'a syn::Type>, + pub(crate) path: PathBuf, } impl TemplateInput<'_> { @@ -24,7 +24,7 @@ impl TemplateInput<'_> { /// mostly recovers the data for the `TemplateInput` fields from the /// `template()` attribute list fields; it also finds the of the `_parent` /// field, if any. - pub fn new<'n>( + pub(crate) fn new<'n>( ast: &'n syn::DeriveInput, config: &'n Config<'_>, ) -> Result, CompileError> { @@ -209,13 +209,13 @@ impl TemplateInput<'_> { } #[inline] - pub fn extension(&self) -> Option<&str> { + pub(crate) fn extension(&self) -> Option<&str> { ext_default_to_path(self.ext.as_deref(), &self.path) } } #[inline] -pub fn ext_default_to_path<'a>(ext: Option<&'a str>, path: &'a Path) -> Option<&'a str> { +fn ext_default_to_path<'a>(ext: Option<&'a str>, path: &'a Path) -> Option<&'a str> { ext.or_else(|| extension(path)) } @@ -233,13 +233,13 @@ fn extension(path: &Path) -> Option<&str> { } } -pub enum Source { +pub(crate) enum Source { Path(String), Source(String), } #[derive(PartialEq)] -pub enum Print { +pub(crate) enum Print { All, Ast, Code, diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 5a8facd..61ddb00 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -21,26 +21,22 @@ mod derive; mod error; pub use crate::error::{Error, Result}; pub mod filters; -#[doc(hidden)] -pub mod generator; +mod generator; pub mod helpers; -#[doc(hidden)] -pub mod heritage; -#[doc(hidden)] -pub mod input; -#[doc(hidden)] -pub mod parser; +mod heritage; +mod input; +mod parser; #[derive(Debug)] -pub struct Config<'a> { - pub dirs: Vec, - pub syntaxes: BTreeMap>, - pub default_syntax: &'a str, - pub escapers: Vec<(HashSet, String)>, +struct Config<'a> { + dirs: Vec, + syntaxes: BTreeMap>, + default_syntax: &'a str, + escapers: Vec<(HashSet, String)>, } impl Config<'_> { - pub fn new(s: &str) -> std::result::Result, CompileError> { + fn new(s: &str) -> std::result::Result, CompileError> { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let default_dirs = vec![root.join("templates")]; @@ -108,7 +104,7 @@ impl Config<'_> { }) } - pub fn find_template( + fn find_template( &self, path: &str, start_at: Option<&Path>, @@ -136,13 +132,13 @@ impl Config<'_> { } #[derive(Debug)] -pub struct Syntax<'a> { - pub block_start: &'a str, - pub block_end: &'a str, - pub expr_start: &'a str, - pub expr_end: &'a str, - pub comment_start: &'a str, - pub comment_end: &'a str, +struct Syntax<'a> { + block_start: &'a str, + block_end: &'a str, + expr_start: &'a str, + expr_end: &'a str, + comment_start: &'a str, + comment_end: &'a str, } impl Default for Syntax<'_> { @@ -241,7 +237,7 @@ struct RawEscaper<'a> { extensions: Vec<&'a str>, } -pub fn read_config_file() -> std::result::Result { +fn read_config_file() -> std::result::Result { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let filename = root.join(CONFIG_FILE_NAME); if filename.exists() { @@ -260,7 +256,7 @@ where } #[allow(clippy::match_wild_err_arm)] -pub fn get_template_source(tpl_path: &Path) -> std::result::Result { +fn get_template_source(tpl_path: &Path) -> std::result::Result { match fs::read_to_string(tpl_path) { Err(_) => Err(format!( "unable to open template file '{}'", @@ -285,20 +281,20 @@ static DEFAULT_ESCAPERS: &[(&[&str], &str)] = &[ ]; #[derive(Debug, Clone)] -pub struct CompileError { +struct CompileError { msg: Cow<'static, str>, span: Span, } impl CompileError { - pub fn new>>(s: S, span: Span) -> Self { + fn new>>(s: S, span: Span) -> Self { Self { msg: s.into(), span, } } - pub fn into_compile_error(self) -> TokenStream { + fn into_compile_error(self) -> TokenStream { syn::Error::new(self.span, self.msg).to_compile_error() } } diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 900e71a..2a7f49f 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -13,7 +13,7 @@ use nom::{self, error_position, AsChar, IResult, InputTakeAtPosition}; use crate::{CompileError, Syntax}; #[derive(Debug, PartialEq)] -pub enum Node<'a> { +pub(crate) enum Node<'a> { Lit(&'a str, &'a str, &'a str), Comment(Ws), Expr(Ws, Expr<'a>), @@ -34,19 +34,19 @@ pub enum Node<'a> { } #[derive(Debug, PartialEq)] -pub struct Loop<'a> { - pub ws1: Ws, - pub var: Target<'a>, - pub iter: Expr<'a>, - pub cond: Option>, - pub body: Vec>, - pub ws2: Ws, - pub else_block: Vec>, - pub ws3: Ws, +pub(crate) struct Loop<'a> { + pub(crate) ws1: Ws, + pub(crate) var: Target<'a>, + pub(crate) iter: Expr<'a>, + pub(crate) cond: Option>, + pub(crate) body: Vec>, + pub(crate) ws2: Ws, + pub(crate) else_block: Vec>, + pub(crate) ws3: Ws, } #[derive(Debug, PartialEq)] -pub enum Expr<'a> { +pub(crate) enum Expr<'a> { BoolLit(&'a str), NumLit(&'a str), StrLit(&'a str), @@ -70,7 +70,7 @@ pub enum Expr<'a> { impl Expr<'_> { /// Returns `true` if enough assumptions can be made, /// to determine that `self` is copyable. - pub fn is_copyable(&self) -> bool { + pub(crate) fn is_copyable(&self) -> bool { self.is_copyable_within_op(false) } @@ -98,7 +98,7 @@ impl Expr<'_> { } /// Returns `true` if this is an `Attr` where the `obj` is `"self"`. - pub fn is_attr_self(&self) -> bool { + pub(crate) fn is_attr_self(&self) -> bool { match self { Expr::Attr(obj, _) if matches!(obj.as_ref(), Expr::Var("self")) => true, Expr::Attr(obj, _) if matches!(obj.as_ref(), Expr::Attr(..)) => obj.is_attr_self(), @@ -107,18 +107,18 @@ impl Expr<'_> { } } -pub type When<'a> = (Ws, Target<'a>, Vec>); +pub(crate) type When<'a> = (Ws, Target<'a>, Vec>); #[derive(Debug, PartialEq)] -pub struct Macro<'a> { - pub ws1: Ws, - pub args: Vec<&'a str>, - pub nodes: Vec>, - pub ws2: Ws, +pub(crate) struct Macro<'a> { + pub(crate) ws1: Ws, + pub(crate) args: Vec<&'a str>, + pub(crate) nodes: Vec>, + pub(crate) ws2: Ws, } #[derive(Debug, PartialEq)] -pub enum Target<'a> { +pub(crate) enum Target<'a> { Name(&'a str), Tuple(Vec<&'a str>, Vec>), Struct(Vec<&'a str>, Vec<(&'a str, Target<'a>)>), @@ -130,14 +130,14 @@ pub enum Target<'a> { } #[derive(Clone, Copy, Debug, PartialEq)] -pub struct Ws(pub bool, pub bool); +pub(crate) struct Ws(pub(crate) bool, pub(crate) bool); -pub type Cond<'a> = (Ws, Option>, Vec>); +pub(crate) type Cond<'a> = (Ws, Option>, Vec>); #[derive(Debug, PartialEq)] -pub struct CondTest<'a> { - pub target: Option>, - pub expr: Expr<'a>, +pub(crate) struct CondTest<'a> { + pub(crate) target: Option>, + pub(crate) expr: Expr<'a>, } fn is_ws(c: char) -> bool { @@ -1157,7 +1157,10 @@ fn tag_expr_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> { tag(s.syntax.expr_end)(i) } -pub fn parse<'a>(src: &'a str, syntax: &'a Syntax<'a>) -> Result>, CompileError> { +pub(crate) fn parse<'a>( + src: &'a str, + syntax: &'a Syntax<'a>, +) -> Result>, CompileError> { let state = State { syntax, loop_depth: Cell::new(0), -- cgit