Added the basics of a document definition.
This commit is contained in:
parent
e2b9045e3c
commit
a71881e59d
@ -18,6 +18,7 @@ mod support_test {
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
enum MTTError {
|
enum MTTError {
|
||||||
DocumentAlreadyExists(String),
|
DocumentAlreadyExists(String),
|
||||||
|
DocumentFieldNotFound(String),
|
||||||
DocumentNotFound(String),
|
DocumentNotFound(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -905,12 +906,71 @@ impl CreateDoc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum FieldType {
|
||||||
|
StaticString,
|
||||||
|
UUID,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
struct DocDef;
|
struct DocDef {
|
||||||
|
fields: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
impl DocDef {
|
impl DocDef {
|
||||||
fn new() -> Self {
|
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<String, MTTError> {
|
||||||
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user