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 {
StaticString,
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)]
struct DocDef {
fields: Vec<String>,
fields: HashMap<String, FieldSetting>,
}
impl DocDef {
fn new() -> Self {
Self { fields: Vec::new() }
Self { fields: HashMap::new() }
}
fn add_field(&mut self, name: String) {
self.fields.push(name);
fn add_field(&mut self, name: String, ftype: FieldType) {
self.fields.insert(name, FieldSetting::new(ftype));
}
fn get_field(&self, name: &str) -> Result<String, MTTError> {
if self.fields.contains(&name.to_string()) {
Ok(name.to_string())
} else {
Err(MTTError::DocumentFieldNotFound(name.to_string()))
fn get_field(&self, name: &str) -> Result<&FieldSetting, MTTError> {
match self.fields.get(name) {
Some(data) => Ok(data),
None => Err(MTTError::DocumentFieldNotFound(name.to_string()))
}
}
}
@ -942,9 +973,10 @@ mod docdefs {
fn can_field_be_added() {
let mut docdef = DocDef::new();
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();
assert_eq!(result, name);
assert_eq!(result.get_type(), &field_type);
}
#[test]
@ -964,12 +996,13 @@ mod docdefs {
fn can_multiple_fields_be_added() {
let mut docdef = DocDef::new();
let names = ["one", "two", "three"];
let field_type = FieldType::StaticString;
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() {
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();
}
#[test]
fn only_responses_to_its_show_request() {
let docdef = DocDef::new();
let name = "quiet";