Implement Hash + PartialEq for more parser + derive types

This commit is contained in:
René Kijewski 2024-07-05 03:31:17 +02:00
parent e3c1fe3548
commit aa93b2a986
4 changed files with 29 additions and 33 deletions

View File

@ -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<PathBuf>,
pub(crate) syntaxes: BTreeMap<String, Syntax<'a>>,
pub(crate) default_syntax: &'a str,
pub(crate) escapers: Vec<(HashSet<String>, String)>,
pub(crate) escapers: Vec<(Vec<Cow<'a, str>>, 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<T>(vals: &[T]) -> HashSet<String>
where
T: ToString,
{
vals.iter().map(|s| s.to_string()).collect()
fn str_set<'a>(vals: &[&'a str]) -> Vec<Cow<'a, str>> {
vals.iter().copied().map(Cow::from).collect()
}
pub(crate) fn get_template_source(

View File

@ -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,
};

View File

@ -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,

View File

@ -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,