Still setting up document additions.

This commit is contained in:
Jeff Baskin 2025-08-05 11:27:18 -04:00
parent 20f798043f
commit 11455ee118

View File

@ -920,6 +920,7 @@ enum FieldType {
Uuid,
}
#[derive(Clone, Debug)]
enum Field {
StaticString(String),
Uuid(Uuid),
@ -929,19 +930,47 @@ impl Field {
fn get_type(&self) -> FieldType {
match self {
Self::StaticString(_) => FieldType::StaticString,
Self::Uuid(_) => FieldType::Uuid,
}
}
}
impl From<String> for Field {
fn from(value: String) -> Self {
Self::StaticString(value)
}
}
impl From<Uuid> for Field {
fn from(value: Uuid) -> Self {
Self::Uuid(value)
}
}
#[cfg(test)]
mod fields {
use super::*;
#[test]
fn can_create_static_string() {
let data = Uuid::new_v4().to_string();
let result: Field = data.clone().into();
match result.clone() {
Field::StaticString(output) => assert_eq!(output, data),
_ => unreachable!("got {:?}: should have been static string", result),
}
assert_eq!(result.get_type(), FieldType::StaticString);
}
#[test]
fn create_uuid() {
let data = Uuid::new_v4();
let sstring = Field::StaticString(data.to_string());
assert_eq!(sstring.get_type(), FieldType::StaticString);
let result: Field = data.clone().into();
match result.clone() {
Field::Uuid(output) => assert_eq!(output, data),
_ => unreachable!("got {:?}: should have been uuid", result),
}
assert_eq!(result.get_type(), FieldType::Uuid);
}
}
@ -978,11 +1007,53 @@ mod fieldsettings {
}
#[derive(Clone, Debug)]
struct Addition;
struct Addition {
data: HashMap<String, Field>,
}
impl Addition {
fn new() -> Self {
Self {}
Self {
data: HashMap::new(),
}
}
fn add_field<F>(&mut self, name: String, field: F) where F: Into<Field> {
self.data.insert(name, field.into());
}
fn get_field(&self, name: &str) -> Option<&Field> {
self.data.get(name)
}
}
#[cfg(test)]
mod additions {
use super::*;
#[test]
fn can_add_static_string() {
let mut add = Addition::new();
let name = Uuid::new_v4().to_string();
let data = Uuid::new_v4().to_string();
add.add_field(name.clone(), data.clone());
let result = add.get_field(&name).unwrap();
match result {
Field::StaticString(result) => assert_eq!(result, &data),
_ => unreachable!("got {:?}: should have received static string", result),
}
}
fn can_add_uuid() {
let mut add = Addition::new();
let name = Uuid::new_v4().to_string();
let data = Uuid::new_v4();
add.add_field(name.clone(), data.clone());
let result = add.get_field(&name).unwrap();
match result {
Field::Uuid(result) => assert_eq!(result, &data),
_ => unreachable!("got {:?}: should have received uuid", result),
}
}
}