From aa93b2a986e1e9bbd29c4ef46b33784e126d4fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Fri, 5 Jul 2024 03:31:17 +0200 Subject: [PATCH] Implement Hash + PartialEq for more parser + derive types --- rinja_derive/src/config.rs | 27 +++++++++------------------ rinja_derive/src/generator.rs | 6 +++++- rinja_derive/src/input.rs | 27 ++++++++++++++------------- rinja_parser/src/lib.rs | 2 +- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/rinja_derive/src/config.rs b/rinja_derive/src/config.rs index 51b69666..58123ac0 100644 --- a/rinja_derive/src/config.rs +++ b/rinja_derive/src/config.rs @@ -1,4 +1,5 @@ -use std::collections::{BTreeMap, HashSet}; +use std::borrow::Cow; +use std::collections::BTreeMap; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::{env, fs}; @@ -10,12 +11,12 @@ use serde::Deserialize; use crate::{CompileError, FileInfo, CRATE}; -#[derive(Debug)] +#[derive(Debug, Hash, PartialEq)] pub(crate) struct Config<'a> { pub(crate) dirs: Vec, pub(crate) syntaxes: BTreeMap>, pub(crate) default_syntax: &'a str, - pub(crate) escapers: Vec<(HashSet, String)>, + pub(crate) escapers: Vec<(Vec>, Cow<'a, str>)>, pub(crate) whitespace: WhitespaceHandling, } @@ -96,18 +97,11 @@ impl<'a> Config<'a> { let mut escapers = Vec::new(); if let Some(configured) = raw.escaper { for escaper in configured { - escapers.push(( - escaper - .extensions - .iter() - .map(|ext| (*ext).to_string()) - .collect(), - escaper.path.to_string(), - )); + escapers.push((str_set(&escaper.extensions), escaper.path.into())); } } for (extensions, path) in DEFAULT_ESCAPERS { - escapers.push((str_set(extensions), format!("{CRATE}{path}"))); + escapers.push((str_set(extensions), format!("{CRATE}{path}").into())); } Ok(Config { @@ -217,7 +211,7 @@ impl RawConfig<'_> { } } -#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, Default, PartialEq, Eq, Debug, Hash)] #[cfg_attr(feature = "config", derive(Deserialize))] #[cfg_attr(feature = "config", serde(field_identifier, rename_all = "lowercase"))] pub(crate) enum WhitespaceHandling { @@ -291,11 +285,8 @@ pub(crate) fn read_config_file( } } -fn str_set(vals: &[T]) -> HashSet -where - T: ToString, -{ - vals.iter().map(|s| s.to_string()).collect() +fn str_set<'a>(vals: &[&'a str]) -> Vec> { + vals.iter().copied().map(Cow::from).collect() } pub(crate) fn get_template_source( diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index 7d7cde3d..4376590b 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -1425,7 +1425,11 @@ impl<'a> Generator<'a> { .config .escapers .iter() - .find_map(|(escapers, escaper)| escapers.contains(name).then_some(escaper)) + .find_map(|(extensions, path)| { + extensions + .contains(&Cow::Borrowed(name)) + .then_some(path.as_ref()) + }) .ok_or_else(|| ctx.generate_error("invalid escaper for escape filter", node))?, None => self.input.escaper, }; diff --git a/rinja_derive/src/input.rs b/rinja_derive/src/input.rs index 7909faa8..26b0a9a3 100644 --- a/rinja_derive/src/input.rs +++ b/rinja_derive/src/input.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::collections::hash_map::HashMap; use std::path::{Path, PathBuf}; use std::rc::Rc; @@ -77,17 +78,17 @@ impl TemplateInput<'_> { .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) { - escaper = Some(path); - break; - } - } - - let escaper = escaper.ok_or_else(|| { - CompileError::no_file_info(format!("no escaper defined for extension '{escaping}'")) - })?; + let escaper = config + .escapers + .iter() + .find_map(|(extensions, path)| { + extensions + .contains(&Cow::Borrowed(escaping)) + .then_some(path.as_ref()) + }) + .ok_or_else(|| { + CompileError::no_file_info(format!("no escaper defined for extension '{escaping}'")) + })?; let mime_type = extension_to_mime_type(ext_default_to_path(ext.as_deref(), &path).unwrap_or("txt")) @@ -414,13 +415,13 @@ fn extension(path: &Path) -> Option<&str> { } } -#[derive(Debug)] +#[derive(Debug, Hash, PartialEq)] pub(crate) enum Source { Path(String), Source(String), } -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, Hash)] pub(crate) enum Print { All, Ast, diff --git a/rinja_parser/src/lib.rs b/rinja_parser/src/lib.rs index b521dfb0..a097a5de 100644 --- a/rinja_parser/src/lib.rs +++ b/rinja_parser/src/lib.rs @@ -688,7 +688,7 @@ impl<'a> State<'a> { } } -#[derive(Debug)] +#[derive(Debug, Hash, PartialEq)] pub struct Syntax<'a> { pub block_start: &'a str, pub block_end: &'a str,