diff --git a/src/message.rs b/src/message.rs index a22dbf8..90651b1 100644 --- a/src/message.rs +++ b/src/message.rs @@ -18,6 +18,7 @@ mod support_test { #[derive(Clone, Debug)] enum MTTError { DocumentAlreadyExists(String), + DocumentFieldNotFound(String), DocumentNotFound(String), } @@ -905,12 +906,71 @@ impl CreateDoc { } } +enum FieldType { + StaticString, + UUID, +} + #[derive(Clone, Debug)] -struct DocDef; +struct DocDef { + fields: Vec, +} impl DocDef { fn new() -> Self { - Self {} + Self { fields: Vec::new() } + } + + fn add_field(&mut self, name: String) { + self.fields.push(name); + } + + fn get_field(&self, name: &str) -> Result { + if self.fields.contains(&name.to_string()) { + Ok(name.to_string()) + } else { + Err(MTTError::DocumentFieldNotFound(name.to_string())) + } + } +} + +#[cfg(test)] +mod docdefs { + use super::*; + + #[test] + fn can_field_be_added() { + let mut docdef = DocDef::new(); + let name = Uuid::new_v4().to_string(); + docdef.add_field(name.clone()); + let result = docdef.get_field(name.as_str()).unwrap(); + assert_eq!(result, name); + } + + #[test] + fn produces_error_for_bad_fields() { + let docdef = DocDef::new(); + let name = Uuid::new_v4().to_string(); + match docdef.get_field(name.as_str()) { + Ok(_) => unreachable!("should return non existant field error"), + Err(err) => match err { + MTTError::DocumentFieldNotFound(data) => assert_eq!(data, name), + _ => unreachable!("got {:?}: should have been document field not found", err), + }, + } + } + + #[test] + fn can_multiple_fields_be_added() { + let mut docdef = DocDef::new(); + let names = ["one", "two", "three"]; + for name in names.iter() { + docdef.add_field(name.to_string()); + } + for name in names.iter() { + let result = docdef.get_field(name).unwrap(); + assert_eq!(result, name.to_string()); + } } }