morethantext/src/action/request_data.rs

80 lines
2.2 KiB
Rust
Raw Normal View History

2026-02-07 23:13:24 -05:00
use crate::{
2026-02-08 11:59:54 -05:00
action::{CalcValue, Field},
2026-02-07 23:13:24 -05:00
name::NameType,
};
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct RequestData {
data: HashMap<NameType, CalcValue>,
}
impl RequestData {
2026-02-09 23:32:14 -05:00
pub fn new() -> Self {
2026-02-07 23:13:24 -05:00
Self {
data: HashMap::new(),
}
}
pub fn add_field<NT, CV>(&mut self, name: NT, field: CV)
where
CV: Into<CalcValue>,
NT: Into<NameType>,
{
self.data.insert(name.into(), field.into());
}
fn get_field<NT>(&self, name: NT) -> &CalcValue
where
NT: Into<NameType>,
{
match self.data.get(&name.into()) {
Some(data) => data,
None => &CalcValue::None,
}
}
pub fn iter(&self) -> impl Iterator<Item = (&NameType, &CalcValue)> {
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),
}
}
}