use {
super::{escape, parse::Chunk},
crate::errors::Result,
serde::Serialize,
serde_json::Value,
std::{borrow::Cow, collections::HashMap, io::Write},
};
pub trait Params {
fn resolve(&self, key: &str) -> Option<Cow<'_, str>>;
fn prepare(&self) -> Option<Value> {
None
}
fn indexed(&self) -> bool {
false
}
fn fill<'a>(&'a self, keys: &[Box<str>], index: &HashMap<Box<str>, usize>, out: &mut [Option<Cow<'a, str>>]) {
let _ = index;
for (slot, key) in out.iter_mut().zip(keys) {
*slot = self.resolve(key);
}
}
}
impl Params for () {
#[inline(always)]
fn resolve(&self, _key: &str) -> Option<Cow<'_, str>> {
None
}
fn fill<'a>(&'a self, _keys: &[Box<str>], _index: &HashMap<Box<str>, usize>, _out: &mut [Option<Cow<'a, str>>]) {}
}
impl<T: Params + ?Sized> Params for &T {
#[inline(always)]
fn resolve(&self, key: &str) -> Option<Cow<'_, str>> {
(*self).resolve(key)
}
fn prepare(&self) -> Option<Value> {
(*self).prepare()
}
fn indexed(&self) -> bool {
(*self).indexed()
}
fn fill<'a>(&'a self, keys: &[Box<str>], index: &HashMap<Box<str>, usize>, out: &mut [Option<Cow<'a, str>>]) {
(*self).fill(keys, index, out)
}
}
fn fill_pairs<'a>(pairs: &'a [(&str, &str)], index: &HashMap<Box<str>, usize>, out: &mut [Option<Cow<'a, str>>]) {
for (k, v) in pairs {
if let Some(&i) = index.get(*k) {
if out[i].is_none() {
out[i] = Some(Cow::Borrowed(*v));
}
}
}
}
impl Params for [(&str, &str)] {
#[inline]
fn resolve(&self, key: &str) -> Option<Cow<'_, str>> {
self.iter().find(|(k, _)| *k == key).map(|(_, v)| Cow::Borrowed(*v))
}
fn indexed(&self) -> bool {
true
}
fn fill<'a>(&'a self, _keys: &[Box<str>], index: &HashMap<Box<str>, usize>, out: &mut [Option<Cow<'a, str>>]) {
fill_pairs(self, index, out)
}
}
impl<const N: usize> Params for [(&str, &str); N] {
#[inline]
fn resolve(&self, key: &str) -> Option<Cow<'_, str>> {
self.as_slice().resolve(key)
}
fn indexed(&self) -> bool {
true
}
fn fill<'a>(&'a self, _keys: &[Box<str>], index: &HashMap<Box<str>, usize>, out: &mut [Option<Cow<'a, str>>]) {
fill_pairs(self, index, out)
}
}
impl Params for HashMap<String, String> {
#[inline]
fn resolve(&self, key: &str) -> Option<Cow<'_, str>> {
self.get(key).map(|s| Cow::Borrowed(s.as_str()))
}
}
pub fn stringify(value: &Value) -> Option<Cow<'_, str>> {
match value {
Value::String(s) => Some(Cow::Borrowed(s.as_str())),
Value::Number(n) => Some(Cow::Owned(match n.as_f64() {
Some(f) if n.is_f64() && f.fract() == 0.0 && f.abs() < 9_007_199_254_740_992.0 => (f as i64).to_string(),
_ => n.to_string(),
})),
Value::Bool(b) => Some(Cow::Borrowed(if *b { "true" } else { "false" })),
Value::Null => None,
other => Some(Cow::Owned(other.to_string())),
}
}
impl Params for Value {
fn resolve(&self, key: &str) -> Option<Cow<'_, str>> {
match self {
Value::Object(map) => map.get(key).and_then(stringify),
_ => None,
}
}
}
pub struct Data<T: Serialize>(pub T);
impl<T: Serialize> Params for Data<T> {
fn resolve(&self, key: &str) -> Option<Cow<'_, str>> {
let val = serde_json::to_value(&self.0).ok()?;
val.resolve(key).map(|c| Cow::Owned(c.into_owned()))
}
fn prepare(&self) -> Option<Value> {
Some(serde_json::to_value(&self.0).unwrap_or(Value::Null))
}
}
#[derive(Debug, Clone)]
enum Part {
Static(Box<str>),
Slot { key: usize, escape: bool },
}
#[derive(Debug, Clone)]
pub struct Plan {
parts: Vec<Part>,
keys: Vec<Box<str>>,
index: HashMap<Box<str>, usize>,
static_len: usize,
slot_count: usize,
}
impl Plan {
pub fn new(chunks: &[Chunk], escape: bool) -> Self {
let mut plan = Plan {
parts: Vec::with_capacity(chunks.len()),
keys: Vec::new(),
index: HashMap::new(),
static_len: 0,
slot_count: 0,
};
for chunk in chunks {
match chunk {
Chunk::Static(bytes) => {
let text: Box<str> = String::from_utf8_lossy(bytes).into();
plan.static_len += text.len();
plan.parts.push(Part::Static(text));
}
Chunk::Slot(name) | Chunk::Raw(name) => {
let key = match plan.index.get(name.as_str()) {
Some(&i) => i,
None => {
let name: Box<str> = name.as_str().into();
plan.keys.push(name.clone());
plan.index.insert(name, plan.keys.len() - 1);
plan.keys.len() - 1
}
};
plan.slot_count += 1;
plan.parts.push(Part::Slot {
key,
escape: escape && matches!(chunk, Chunk::Slot(_)),
});
}
}
}
plan
}
pub fn keys(&self) -> &[Box<str>] {
&self.keys
}
pub fn bind<P: Params + ?Sized>(&self, params: &P) -> String {
match params.prepare() {
Some(value) => self.bind_resolved(&value),
None => self.bind_resolved(params),
}
}
fn bind_resolved<P: Params + ?Sized>(&self, params: &P) -> String {
if !params.indexed() {
let mut out = String::with_capacity(self.static_len + 16 * self.slot_count);
for part in &self.parts {
match part {
Part::Static(text) => out.push_str(text),
Part::Slot { key, escape } => {
if let Some(value) = params.resolve(&self.keys[*key]) {
if *escape {
escape::escape_push(&mut out, &value);
} else {
out.push_str(&value);
}
}
}
}
}
return out;
}
let mut values: Vec<Option<Cow<'_, str>>> = vec![None; self.keys.len()];
params.fill(&self.keys, &self.index, &mut values);
let len = self.static_len + values.iter().flatten().map(|v| v.len()).sum::<usize>();
let mut out = String::with_capacity(len);
for part in &self.parts {
match part {
Part::Static(text) => out.push_str(text),
Part::Slot { key, escape } => {
if let Some(value) = &values[*key] {
if *escape {
escape::escape_push(&mut out, value);
} else {
out.push_str(value);
}
}
}
}
}
out
}
}
pub fn inject<P: Params + ?Sized, W: Write>(chunks: &[Chunk], params: &P, out: &mut W) -> Result<()> {
inject_with(chunks, params, false, out)
}
pub fn inject_with<P: Params + ?Sized, W: Write>(chunks: &[Chunk], params: &P, escape: bool, out: &mut W) -> Result<()> {
out
.write_all(string_with(chunks, params, escape)?.as_bytes())
.map_err(|e| crate::errors::Error::internal(e.to_string()))
}
pub fn string<P: Params + ?Sized>(chunks: &[Chunk], params: &P) -> Result<String> {
string_with(chunks, params, false)
}
pub fn string_with<P: Params + ?Sized>(chunks: &[Chunk], params: &P, escape: bool) -> Result<String> {
Ok(Plan::new(chunks, escape).bind(params))
}