Files
morethantext/src/action/addition.rs

113 lines
3.5 KiB
Rust
Raw Normal View History

2026-02-09 23:32:14 -05:00
use super::{CalcValue, Field, RequestData};
use crate::name::NameType;
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
}
}
2026-02-12 00:05:33 -05:00
pub fn doc_name(&self) -> NameType {
self.doc_name.clone()
}
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()
}
}
#[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());
let add = Addition::new(doc_name.clone());
assert_eq!(add.doc_name(), doc_name.into());
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),
}
}
*/
}