use crate::{ action::{CalcValue, Field}, name::NameType, }; use std::collections::HashMap; #[derive(Clone, Debug)] pub struct RequestData { data: HashMap, } impl RequestData { pub fn new() -> Self { Self { data: HashMap::new(), } } pub fn add_field(&mut self, name: NT, field: CV) where CV: Into, NT: Into, { self.data.insert(name.into(), field.into()); } fn get_field(&self, name: NT) -> &CalcValue where NT: Into, { match self.data.get(&name.into()) { Some(data) => data, None => &CalcValue::None, } } pub fn iter(&self) -> impl Iterator { self.data.iter() } } #[cfg(test)] mod request_datum { use super::*; use crate::name::Name; use uuid::Uuid; #[test] fn can_add_static_string() { let mut add = RequestData::new(); let name = Name::english(Uuid::new_v4().to_string().as_str()); let data = Uuid::new_v4().to_string(); add.add_field(name.clone(), data.clone()); let result = add.get_field(&name); match result { CalcValue::Value(holder) => match holder { Field::StaticString(result) => assert_eq!(result, &data), _ => unreachable!("got {:?}: should have received static string", holder), }, _ => unreachable!("got {:?}, should have been value", result), } } #[test] fn can_add_uuid() { let mut add = RequestData::new(); let name = Name::english(Uuid::new_v4().to_string().as_str()); let data = Uuid::new_v4(); add.add_field(name.clone(), data.clone()); let result = add.get_field(&name); match result { CalcValue::Value(holder) => match holder { Field::Uuid(result) => assert_eq!(result, &data), _ => unreachable!("got {:?}: should have received static string", holder), }, _ => unreachable!("got {:?}, should have been value", result), } } }