2026-02-09 23:32:14 -05:00
|
|
|
use super::{CalcValue, Field, RequestData};
|
2026-02-12 10:18:53 -05:00
|
|
|
use crate::{message::MessageAction, name::NameType};
|
2026-02-09 23:32:14 -05:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Addition {
|
|
|
|
|
data: RequestData,
|
2026-02-12 00:05:33 -05:00
|
|
|
doc_name: NameType,
|
2026-02-09 23:32:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Addition {
|
2026-02-12 00:05:33 -05:00
|
|
|
pub fn new<NT>(name: NT) -> Self
|
|
|
|
|
where
|
|
|
|
|
NT: Into<NameType>,
|
|
|
|
|
{
|
2026-02-09 23:32:14 -05:00
|
|
|
Self {
|
|
|
|
|
data: RequestData::new(),
|
2026-02-12 00:05:33 -05:00
|
|
|
doc_name: name.into(),
|
2026-02-09 23:32:14 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_field<NT, CV>(&mut self, name: NT, field: CV)
|
|
|
|
|
where
|
|
|
|
|
CV: Into<CalcValue>,
|
|
|
|
|
NT: Into<NameType>,
|
|
|
|
|
{
|
|
|
|
|
self.data.add_field(name, field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (&NameType, &CalcValue)> {
|
|
|
|
|
self.data.iter()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 10:18:53 -05:00
|
|
|
impl MessageAction for Addition {
|
2026-02-12 12:24:20 -05:00
|
|
|
fn doc_name(&self) -> &NameType {
|
|
|
|
|
&self.doc_name
|
2026-02-12 10:18:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-09 23:32:14 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod additions {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::name::Name;
|
|
|
|
|
|
2026-02-12 00:05:33 -05:00
|
|
|
#[test]
|
|
|
|
|
fn is_default_addition_empty() {
|
|
|
|
|
let doc_name = Name::english(Uuid::new_v4().to_string().as_str());
|
2026-02-12 12:24:20 -05:00
|
|
|
let expected_name: NameType = doc_name.clone().into();
|
2026-02-12 00:05:33 -05:00
|
|
|
let add = Addition::new(doc_name.clone());
|
2026-02-12 12:24:20 -05:00
|
|
|
assert_eq!(add.doc_name(), &expected_name);
|
2026-02-12 00:05:33 -05:00
|
|
|
assert_eq!(add.iter().count(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-09 23:32:14 -05:00
|
|
|
#[test]
|
|
|
|
|
fn can_add_static_string() {
|
2026-02-12 00:05:33 -05:00
|
|
|
let doc_name = Name::english(Uuid::new_v4().to_string().as_str());
|
|
|
|
|
let mut add = Addition::new(doc_name);
|
2026-02-09 23:32:14 -05:00
|
|
|
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
|
|
|
|
let ntype: NameType = name.clone().into();
|
|
|
|
|
let data = Uuid::new_v4().to_string();
|
|
|
|
|
add.add_field(name.clone(), data.clone());
|
|
|
|
|
assert_eq!(add.iter().count(), 1);
|
|
|
|
|
for (field_name, value) in add.iter() {
|
|
|
|
|
assert_eq!(field_name, &ntype);
|
|
|
|
|
match value {
|
|
|
|
|
CalcValue::Value(result) => match result {
|
|
|
|
|
Field::StaticString(output) => assert_eq!(output, &data),
|
|
|
|
|
_ => unreachable!("got {:?}, should have been a string", result),
|
|
|
|
|
},
|
|
|
|
|
_ => unreachable!("got {:?}: should have received value", value),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn can_add_uuid() {
|
2026-02-12 00:05:33 -05:00
|
|
|
let doc_name = Name::english(Uuid::new_v4().to_string().as_str());
|
|
|
|
|
let mut add = Addition::new(doc_name);
|
2026-02-09 23:32:14 -05:00
|
|
|
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
|
|
|
|
let ntype: NameType = name.clone().into();
|
|
|
|
|
let data = Uuid::new_v4();
|
|
|
|
|
add.add_field(name.clone(), data.clone());
|
|
|
|
|
assert_eq!(add.iter().count(), 1);
|
|
|
|
|
for (field_name, value) in add.iter() {
|
|
|
|
|
assert_eq!(field_name, &ntype);
|
|
|
|
|
match value {
|
|
|
|
|
CalcValue::Value(result) => match result {
|
|
|
|
|
Field::Uuid(output) => assert_eq!(output, &data),
|
|
|
|
|
_ => unreachable!("got {:?}, should have been a string", result),
|
|
|
|
|
},
|
|
|
|
|
_ => unreachable!("got {:?}: should have received value", value),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#[test]
|
|
|
|
|
fn can_get_document() {
|
|
|
|
|
let mut add = Addition::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 doc = add.get_document();
|
|
|
|
|
let output = doc.get_field(&name);
|
|
|
|
|
match output {
|
|
|
|
|
CalcValue::Value(holder) => match holder {
|
|
|
|
|
Field::Uuid(result) => assert_eq!(result, &data),
|
|
|
|
|
_ => unreachable!("should have received uuid"),
|
|
|
|
|
},
|
|
|
|
|
_ => unreachable!("got {:?}: should have received value", output),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|