diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2022-03-10 09:02:10 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2022-03-23 19:37:10 +0100 |
commit | 07584691a8b2ff44776d22c9f9b2c4ec1b303948 (patch) | |
tree | 43549d7d414aad8c728b7f6b6e3c67c0b6004898 /askama_shared | |
parent | 4c0388d11e1bafb85250b649ff610d3f9c7f9452 (diff) | |
download | askama-07584691a8b2ff44776d22c9f9b2c4ec1b303948.tar.gz askama-07584691a8b2ff44776d22c9f9b2c4ec1b303948.tar.bz2 askama-07584691a8b2ff44776d22c9f9b2c4ec1b303948.zip |
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.
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/derive.rs | 1 | ||||
-rw-r--r-- | askama_shared/src/generator.rs | 2 | ||||
-rw-r--r-- | askama_shared/src/heritage.rs | 22 | ||||
-rw-r--r-- | askama_shared/src/input.rs | 32 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 50 | ||||
-rw-r--r-- | askama_shared/src/parser.rs | 55 |
6 files changed, 81 insertions, 81 deletions
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<S: std::hash::BuildHasher>( +pub(crate) fn generate<S: std::hash::BuildHasher>( 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<PathBuf>, - 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<PathBuf>, + 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<String>, - 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<String>, + 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<TemplateInput<'n>, 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<PathBuf>, - pub syntaxes: BTreeMap<String, Syntax<'a>>, - pub default_syntax: &'a str, - pub escapers: Vec<(HashSet<String>, String)>, +struct Config<'a> { + dirs: Vec<PathBuf>, + syntaxes: BTreeMap<String, Syntax<'a>>, + default_syntax: &'a str, + escapers: Vec<(HashSet<String>, String)>, } impl Config<'_> { - pub fn new(s: &str) -> std::result::Result<Config<'_>, CompileError> { + fn new(s: &str) -> std::result::Result<Config<'_>, 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<String, CompileError> { +fn read_config_file() -> std::result::Result<String, CompileError> { 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<String, CompileError> { +fn get_template_source(tpl_path: &Path) -> std::result::Result<String, CompileError> { 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: Into<Cow<'static, str>>>(s: S, span: Span) -> Self { + fn new<S: Into<Cow<'static, str>>>(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<Expr<'a>>, - pub body: Vec<Node<'a>>, - pub ws2: Ws, - pub else_block: Vec<Node<'a>>, - 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<Expr<'a>>, + pub(crate) body: Vec<Node<'a>>, + pub(crate) ws2: Ws, + pub(crate) else_block: Vec<Node<'a>>, + 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<Node<'a>>); +pub(crate) type When<'a> = (Ws, Target<'a>, Vec<Node<'a>>); #[derive(Debug, PartialEq)] -pub struct Macro<'a> { - pub ws1: Ws, - pub args: Vec<&'a str>, - pub nodes: Vec<Node<'a>>, - pub ws2: Ws, +pub(crate) struct Macro<'a> { + pub(crate) ws1: Ws, + pub(crate) args: Vec<&'a str>, + pub(crate) nodes: Vec<Node<'a>>, + pub(crate) ws2: Ws, } #[derive(Debug, PartialEq)] -pub enum Target<'a> { +pub(crate) enum Target<'a> { Name(&'a str), Tuple(Vec<&'a str>, Vec<Target<'a>>), 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<CondTest<'a>>, Vec<Node<'a>>); +pub(crate) type Cond<'a> = (Ws, Option<CondTest<'a>>, Vec<Node<'a>>); #[derive(Debug, PartialEq)] -pub struct CondTest<'a> { - pub target: Option<Target<'a>>, - pub expr: Expr<'a>, +pub(crate) struct CondTest<'a> { + pub(crate) target: Option<Target<'a>>, + 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<Vec<Node<'a>>, CompileError> { +pub(crate) fn parse<'a>( + src: &'a str, + syntax: &'a Syntax<'a>, +) -> Result<Vec<Node<'a>>, CompileError> { let state = State { syntax, loop_depth: Cell::new(0), |