use std::collections::HashMap;
use ahash::RandomState;
use cheetah_string::CheetahString;
pub trait EvaluationContext {
fn get(&self, name: &str) -> Option<&CheetahString>;
fn key_values(&self) -> Option<HashMap<CheetahString, CheetahString>>;
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MessageEvaluationContext {
#[serde(with = "hashmap_serde")]
properties: HashMap<CheetahString, CheetahString, RandomState>,
}
mod hashmap_serde {
use super::*;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
pub fn serialize<S>(
map: &HashMap<CheetahString, CheetahString, RandomState>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let standard_map: HashMap<CheetahString, CheetahString> =
map.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
standard_map.serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<HashMap<CheetahString, CheetahString, RandomState>, D::Error>
where
D: Deserializer<'de>,
{
let standard_map: HashMap<CheetahString, CheetahString> = HashMap::deserialize(deserializer)?;
let mut ahash_map = HashMap::with_hasher(RandomState::default());
ahash_map.extend(standard_map);
Ok(ahash_map)
}
}
impl Default for MessageEvaluationContext {
fn default() -> Self {
Self {
properties: HashMap::with_hasher(RandomState::default()),
}
}
}
impl MessageEvaluationContext {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self {
properties: HashMap::with_capacity_and_hasher(capacity, RandomState::default()),
}
}
#[inline]
pub fn from_properties(properties: HashMap<CheetahString, CheetahString, RandomState>) -> Self {
Self { properties }
}
#[inline]
pub fn put(&mut self, key: impl Into<CheetahString>, value: impl Into<CheetahString>) -> Option<CheetahString> {
self.properties.insert(key.into(), value.into())
}
#[inline]
pub fn remove(&mut self, key: &str) -> Option<CheetahString> {
self.properties.remove(key)
}
#[inline]
pub fn contains_key(&self, key: &str) -> bool {
self.properties.contains_key(key)
}
#[inline]
pub fn len(&self) -> usize {
self.properties.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.properties.is_empty()
}
#[inline]
pub fn clear(&mut self) {
self.properties.clear();
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (&CheetahString, &CheetahString)> {
self.properties.iter()
}
#[inline]
pub fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = (CheetahString, CheetahString)>,
{
self.properties.extend(iter);
}
}
impl EvaluationContext for MessageEvaluationContext {
#[inline]
fn get(&self, name: &str) -> Option<&CheetahString> {
self.properties.get(name)
}
fn key_values(&self) -> Option<HashMap<CheetahString, CheetahString>> {
Some(self.properties.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_context_is_empty() {
let context = MessageEvaluationContext::new();
assert!(context.is_empty());
assert_eq!(context.len(), 0);
}
#[test]
fn test_default_context_is_empty() {
let context = MessageEvaluationContext::default();
assert!(context.is_empty());
assert_eq!(context.len(), 0);
}
#[test]
fn test_put_and_get() {
let mut context = MessageEvaluationContext::new();
context.put("key1", "value1");
context.put("key2", "value2");
assert_eq!(context.get("key1").map(|s| s.as_str()), Some("value1"));
assert_eq!(context.get("key2").map(|s| s.as_str()), Some("value2"));
assert_eq!(context.get("nonexistent"), None);
assert_eq!(context.len(), 2);
}
#[test]
fn test_put_overwrites_existing() {
let mut context = MessageEvaluationContext::new();
context.put("key", "value1");
let old = context.put("key", "value2");
assert_eq!(old.as_ref().map(|s| s.as_str()), Some("value1"));
assert_eq!(context.get("key").map(|s| s.as_str()), Some("value2"));
assert_eq!(context.len(), 1);
}
#[test]
fn test_remove() {
let mut context = MessageEvaluationContext::new();
context.put("key", "value");
let removed = context.remove("key");
assert_eq!(removed.as_ref().map(|s| s.as_str()), Some("value"));
assert!(context.get("key").is_none());
assert!(context.is_empty());
}
#[test]
fn test_contains_key() {
let mut context = MessageEvaluationContext::new();
context.put("key", "value");
assert!(context.contains_key("key"));
assert!(!context.contains_key("nonexistent"));
}
#[test]
fn test_clear() {
let mut context = MessageEvaluationContext::new();
context.put("key1", "value1");
context.put("key2", "value2");
context.clear();
assert!(context.is_empty());
assert_eq!(context.len(), 0);
}
#[test]
fn test_with_capacity() {
let context = MessageEvaluationContext::with_capacity(10);
assert!(context.is_empty());
}
#[test]
fn test_from_properties() {
let mut props = HashMap::with_hasher(RandomState::default());
props.insert(CheetahString::from("key1"), CheetahString::from("value1"));
props.insert(CheetahString::from("key2"), CheetahString::from("value2"));
let context = MessageEvaluationContext::from_properties(props);
assert_eq!(context.len(), 2);
assert_eq!(context.get("key1").map(|s| s.as_str()), Some("value1"));
}
#[test]
fn test_iter() {
let mut context = MessageEvaluationContext::new();
context.put("key1", "value1");
context.put("key2", "value2");
let count = context.iter().count();
assert_eq!(count, 2);
}
#[test]
fn test_extend() {
let mut context = MessageEvaluationContext::new();
let mut props = HashMap::with_hasher(RandomState::default());
props.insert(CheetahString::from("key1"), CheetahString::from("value1"));
props.insert(CheetahString::from("key2"), CheetahString::from("value2"));
context.extend(props);
assert_eq!(context.len(), 2);
}
#[test]
fn test_key_values() {
let mut context = MessageEvaluationContext::new();
context.put("key1", "value1");
context.put("key2", "value2");
let key_values = context.key_values();
assert!(key_values.is_some());
let map = key_values.unwrap();
assert_eq!(map.len(), 2);
assert_eq!(
map.get(&CheetahString::from("key1")).map(|s| s.as_str()),
Some("value1")
);
}
#[test]
fn test_evaluation_context_trait() {
let mut context = MessageEvaluationContext::new();
context.put("property", "value");
let ctx: &dyn EvaluationContext = &context;
assert_eq!(ctx.get("property").map(|s| s.as_str()), Some("value"));
assert!(ctx.get("nonexistent").is_none());
let key_values = ctx.key_values();
assert!(key_values.is_some());
assert_eq!(key_values.unwrap().len(), 1);
}
#[test]
fn test_clone() {
let mut context = MessageEvaluationContext::new();
context.put("key", "value");
let cloned = context.clone();
assert_eq!(cloned.get("key").map(|s| s.as_str()), Some("value"));
assert_eq!(context, cloned);
}
#[test]
fn test_debug_format() {
let mut context = MessageEvaluationContext::new();
context.put("key", "value");
let debug_str = format!("{:?}", context);
assert!(debug_str.contains("MessageEvaluationContext"));
}
#[test]
fn test_serde_serialize_deserialize() {
let mut context = MessageEvaluationContext::new();
context.put("key1", "value1");
context.put("key2", "value2");
let json = serde_json::to_string(&context).unwrap();
assert!(json.contains("key1"));
assert!(json.contains("value1"));
let deserialized: MessageEvaluationContext = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.len(), 2);
assert_eq!(deserialized.get("key1").map(|s| s.as_str()), Some("value1"));
assert_eq!(deserialized.get("key2").map(|s| s.as_str()), Some("value2"));
}
}