Skip to content

Commit

Permalink
Reorganize TemplateArgs and TemplateInput structure
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Oct 6, 2023
1 parent 5fc350b commit be58d16
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
36 changes: 19 additions & 17 deletions askama_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use mime::Mime;
use quote::ToTokens;
use syn::punctuated::Punctuated;

use crate::config::{get_template_source, Config};
use crate::config::{get_template_source, read_config_file, Config};
use crate::CompileError;
use parser::{Node, Parsed, Syntax};

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) source: &'a Source,
pub(crate) print: Print,
pub(crate) escaper: &'a str,
pub(crate) ext: Option<String>,
pub(crate) ext: Option<&'a str>,
pub(crate) mime_type: String,
pub(crate) path: PathBuf,
}
Expand All @@ -29,7 +29,7 @@ impl TemplateInput<'_> {
pub(crate) fn new<'n>(
ast: &'n syn::DeriveInput,
config: &'n Config<'_>,
args: TemplateArgs,
args: &'n TemplateArgs,
) -> Result<TemplateInput<'n>, CompileError> {
let TemplateArgs {
source,
Expand All @@ -43,7 +43,9 @@ impl TemplateInput<'_> {
// Validate the `source` and `ext` value together, since they are
// related. In case `source` was used instead of `path`, the value
// of `ext` is merged into a synthetic `path` value here.
let source = source.expect("template path or source not found in attributes");
let source = source
.as_ref()
.expect("template path or source not found in attributes");
let path = match (&source, &ext) {
(Source::Path(path), _) => config.find_template(path, None)?,
(&Source::Source(_), Some(ext)) => PathBuf::from(format!("{}.{}", ast.ident, ext)),
Expand All @@ -53,28 +55,25 @@ impl TemplateInput<'_> {
};

// Validate syntax
let syntax = syntax.map_or_else(
let syntax = syntax.as_deref().map_or_else(
|| Ok(config.syntaxes.get(config.default_syntax).unwrap()),
|s| {
config
.syntaxes
.get(&s)
.get(s)
.ok_or_else(|| CompileError::from(format!("attribute syntax {s} not exist")))
},
)?;

// Match extension against defined output formats

let escaping = escaping.unwrap_or_else(|| {
path.extension()
.map(|s| s.to_str().unwrap())
.unwrap_or("")
.to_string()
});
let escaping = escaping
.as_deref()
.unwrap_or_else(|| path.extension().map(|s| s.to_str().unwrap()).unwrap_or(""));

let mut escaper = None;
for (extensions, path) in &config.escapers {
if extensions.contains(&escaping) {
if extensions.contains(escaping) {
escaper = Some(path);
break;
}
Expand All @@ -93,9 +92,9 @@ impl TemplateInput<'_> {
config,
syntax,
source,
print,
print: *print,
escaper,
ext,
ext: ext.as_deref(),
mime_type,
path,
})
Expand Down Expand Up @@ -160,6 +159,7 @@ pub(crate) struct TemplateArgs {
pub(crate) ext: Option<String>,
pub(crate) syntax: Option<String>,
pub(crate) config_path: Option<String>,
pub(crate) config: String,
pub(crate) whitespace: Option<String>,
}

Expand All @@ -184,6 +184,8 @@ impl TemplateArgs {
template_args.ok_or_else(|| CompileError::from("no attribute 'template' found"))?;

let mut args = Self::default();
args.config = read_config_file(args.config_path.as_deref())?;

// Loop over the meta attributes and find everything that we
// understand. Return a CompileError if something is not right.
// `source` contains an enum that can represent `path` or `source`.
Expand Down Expand Up @@ -302,7 +304,7 @@ pub(crate) enum Source {
Source(String),
}

#[derive(PartialEq)]
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Print {
All,
Ast,
Expand Down
7 changes: 3 additions & 4 deletions askama_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use proc_macro2::Span;
use parser::ParseError;

mod config;
use config::{read_config_file, Config};
use config::Config;
mod generator;
use generator::{Generator, MapChain};
mod heritage;
Expand All @@ -36,9 +36,8 @@ pub fn derive_template(input: TokenStream) -> TokenStream {
/// value as passed to the `template()` attribute.
pub(crate) fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
let template_args = TemplateArgs::new(ast)?;
let config_toml = read_config_file(template_args.config_path.as_deref())?;
let config = Config::new(&config_toml, template_args.whitespace.as_ref())?;
let input = TemplateInput::new(ast, &config, template_args)?;
let config = Config::new(&template_args.config, template_args.whitespace.as_ref())?;
let input = TemplateInput::new(ast, &config, &template_args)?;

let mut templates = HashMap::new();
input.find_used_templates(&mut templates)?;
Expand Down

0 comments on commit be58d16

Please sign in to comment.