Added basic field definitions.

This commit is contained in:
Jeff Baskin 2025-08-04 23:51:57 -04:00
parent a71881e59d
commit ad094b23f3

View File

@ -906,30 +906,61 @@ impl CreateDoc {
} }
} }
#[derive(Clone, Debug, PartialEq)]
enum FieldType { enum FieldType {
StaticString, StaticString,
UUID, UUID,
} }
#[derive(Clone, Debug)]
struct FieldSetting {
fieldtype: FieldType,
}
impl FieldSetting {
fn new(ftype: FieldType) -> Self {
Self {
fieldtype: ftype,
}
}
fn get_type(&self) -> &FieldType {
&self.fieldtype
}
}
#[cfg(test)]
mod fieldsettings {
use super::*;
#[test]
fn can_field_type_be_assigned() {
let ftypes = [FieldType::StaticString, FieldType::UUID];
for ftype in ftypes.into_iter() {
let fieldinfo = FieldSetting::new(ftype.clone());
assert_eq!(fieldinfo.get_type(), &ftype);
}
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct DocDef { struct DocDef {
fields: Vec<String>, fields: HashMap<String, FieldSetting>,
} }
impl DocDef { impl DocDef {
fn new() -> Self { fn new() -> Self {
Self { fields: Vec::new() } Self { fields: HashMap::new() }
} }
fn add_field(&mut self, name: String) { fn add_field(&mut self, name: String, ftype: FieldType) {
self.fields.push(name); self.fields.insert(name, FieldSetting::new(ftype));
} }
fn get_field(&self, name: &str) -> Result<String, MTTError> { fn get_field(&self, name: &str) -> Result<&FieldSetting, MTTError> {
if self.fields.contains(&name.to_string()) { match self.fields.get(name) {
Ok(name.to_string()) Some(data) => Ok(data),
} else { None => Err(MTTError::DocumentFieldNotFound(name.to_string()))
Err(MTTError::DocumentFieldNotFound(name.to_string()))
} }
} }
} }
@ -942,9 +973,10 @@ mod docdefs {
fn can_field_be_added() { fn can_field_be_added() {
let mut docdef = DocDef::new(); let mut docdef = DocDef::new();
let name = Uuid::new_v4().to_string(); let name = Uuid::new_v4().to_string();
docdef.add_field(name.clone()); let field_type = FieldType::UUID;
docdef.add_field(name.clone(), field_type.clone());
let result = docdef.get_field(name.as_str()).unwrap(); let result = docdef.get_field(name.as_str()).unwrap();
assert_eq!(result, name); assert_eq!(result.get_type(), &field_type);
} }
#[test] #[test]
@ -964,12 +996,13 @@ mod docdefs {
fn can_multiple_fields_be_added() { fn can_multiple_fields_be_added() {
let mut docdef = DocDef::new(); let mut docdef = DocDef::new();
let names = ["one", "two", "three"]; let names = ["one", "two", "three"];
let field_type = FieldType::StaticString;
for name in names.iter() { for name in names.iter() {
docdef.add_field(name.to_string()); docdef.add_field(name.to_string(), field_type.clone());
} }
for name in names.iter() { for name in names.iter() {
let result = docdef.get_field(name).unwrap(); let result = docdef.get_field(name).unwrap();
assert_eq!(result, name.to_string()); assert_eq!(result.get_type(), &field_type);
} }
} }
} }
@ -1085,6 +1118,7 @@ mod documents {
let show = rx.recv_timeout(TIMEOUT).unwrap(); let show = rx.recv_timeout(TIMEOUT).unwrap();
} }
#[test]
fn only_responses_to_its_show_request() { fn only_responses_to_its_show_request() {
let docdef = DocDef::new(); let docdef = DocDef::new();
let name = "quiet"; let name = "quiet";