89 lines
2.4 KiB
Rust
89 lines
2.4 KiB
Rust
|
|
use crate::{
|
||
|
|
document::field::Field,
|
||
|
|
message::wrapper::CalcValue,
|
||
|
|
name::NameType,
|
||
|
|
};
|
||
|
|
use std::collections::HashMap;
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub struct RequestData {
|
||
|
|
data: HashMap<NameType, CalcValue>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl RequestData {
|
||
|
|
fn new() -> Self {
|
||
|
|
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,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn get_all(&self) -> Vec<(NameType, Field)> {
|
||
|
|
let mut output = Vec::new();
|
||
|
|
for (key, value) in self.data.iter() {
|
||
|
|
output.push((key.clone(), value.get(&Field::None)));
|
||
|
|
}
|
||
|
|
output
|
||
|
|
}
|
||
|
|
|
||
|
|
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),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|