use super::collection::{Collection, HashMapCollection, MutableCollection};
use regex::Regex;
use std::borrow::Cow;
#[derive(Debug, Clone, Default)]
pub struct TxCollection {
data: HashMapCollection,
}
fn normalize(key: &str) -> Cow<'_, str> {
if key.bytes().any(|b| b.is_ascii_uppercase()) {
Cow::Owned(key.to_ascii_lowercase())
} else {
Cow::Borrowed(key)
}
}
impl TxCollection {
pub fn new() -> Self {
Self::default()
}
pub fn clear(&mut self) {
self.data.clear();
}
}
impl Collection for TxCollection {
fn all(&self) -> Vec<(&str, &str)> {
self.data.all()
}
fn get(&self, key: &str) -> Option<Vec<&str>> {
self.data.get(&normalize(key))
}
fn get_regex(&self, pattern: &Regex) -> Vec<(&str, &str)> {
self.data.get_regex(pattern)
}
fn count(&self) -> usize {
self.data.count()
}
fn count_key(&self, key: &str) -> usize {
self.data.count_key(&normalize(key))
}
}
impl MutableCollection for TxCollection {
fn set(&mut self, key: String, value: String) {
match normalize(&key) {
Cow::Borrowed(_) => self.data.set(key, value),
Cow::Owned(lower) => self.data.set(lower, value),
}
}
fn delete(&mut self, key: &str) {
self.data.delete(&normalize(key));
}
fn increment(&mut self, key: &str, amount: i64) {
self.data.increment(&normalize(key), amount);
}
fn decrement(&mut self, key: &str, amount: i64) {
self.data.decrement(&normalize(key), amount);
}
}