diff --git a/src/message.rs b/src/message.rs index 883e609..3051900 100644 --- a/src/message.rs +++ b/src/message.rs @@ -2118,7 +2118,10 @@ impl FieldSetting { } } - fn set_default(&mut self, holder: CV) -> Result<(), MTTError> where CV: Into { + fn set_default(&mut self, holder: CV) -> Result<(), MTTError> + where + CV: Into, + { let value = holder.into(); match &value { CalcValue::Calculate(calc) => { @@ -2136,28 +2139,28 @@ impl FieldSetting { return Err(MTTError::FieldInvalidType); } } - CalcValue::None => {}, + CalcValue::None => {} } self.default_value = value; Ok(()) } - fn validate(&self, value: Option) -> Result { + fn validate(&self, value: &Field) -> Result { match value { - Some(data) => { - let vft: FieldType = (&data).into(); + Field::None => match &self.default_value { + CalcValue::None => Err(MTTError::InvalidNone), + _ => Ok(self.default_value.get(&Field::None)), + }, + _ => { + let vft: FieldType = value.into(); if vft != self.fieldtype { return Err(MTTError::DocumentFieldWrongDataType( self.fieldtype.clone(), vft, )); } - Ok(data.clone()) + Ok(value.clone()) } - None => match &self.default_value { - CalcValue::None => Err(MTTError::InvalidNone), - _ => Ok(self.default_value.get(&Field::None)), - }, } } } @@ -2170,7 +2173,7 @@ mod fieldsettings { fn validates_field_type() { let fset = FieldSetting::new(FieldType::Uuid); let value: Field = Uuid::new_v4().into(); - match fset.validate(Some(value.clone())) { + match fset.validate(&value) { Ok(data) => assert_eq!(data, value), Err(err) => unreachable!("got {:?}: should have gotten a value", err), } @@ -2180,7 +2183,7 @@ mod fieldsettings { fn validates_for_bad_field_type() { let fset = FieldSetting::new(FieldType::Uuid); let value: Field = "text".into(); - match fset.validate(Some(value)) { + match fset.validate(&value) { Ok(data) => unreachable!("got {:?}: should have gotten an error", data), Err(err) => match err { MTTError::DocumentFieldWrongDataType(expected, got) => { @@ -2195,7 +2198,7 @@ mod fieldsettings { #[test] fn no_default_returns_error() { let fset = FieldSetting::new(FieldType::Uuid); - match fset.validate(None) { + match fset.validate(&Field::None) { Ok(data) => unreachable!("got {:?}: should have gotten an error", data), Err(err) => match err { MTTError::InvalidNone => {} @@ -2208,7 +2211,7 @@ mod fieldsettings { fn returns_value_if_default_is_set() { let mut fset = FieldSetting::new(FieldType::StaticString); fset.set_default(FieldType::StaticString); - match fset.validate(None) { + match fset.validate(&Field::None) { Ok(data) => assert_eq!(data, "".into()), Err(err) => unreachable!("got {:?}: should have gotten a value", err), } @@ -2219,7 +2222,7 @@ mod fieldsettings { let mut fset = FieldSetting::new(FieldType::StaticString); let input = "fred"; fset.set_default(input); - match fset.validate(None) { + match fset.validate(&Field::None) { Ok(data) => assert_eq!(data, input.into()), Err(err) => unreachable!("got {:?}: should have gotten a value", err), } @@ -2234,7 +2237,7 @@ mod fieldsettings { calc.add_value(duration); fset.set_default(calc); let start = Utc::now() + duration; - let result = match fset.validate(None).unwrap() { + let result = match fset.validate(&Field::None).unwrap() { Field::DateTime(data) => data, _ => unreachable!("should return datetime"), }; @@ -2274,7 +2277,7 @@ impl Addition { self.data.add_field(name, field); } - fn get_field(&self, name: NT) -> Option + fn get_field(&self, name: NT) -> &CalcValue where NT: Into, { @@ -2296,34 +2299,46 @@ mod additions { let name = Name::english(Uuid::new_v4().to_string().as_str()); let data = Uuid::new_v4().to_string(); add.add_field(name.clone(), data.clone()); - let result = add.get_field(&name).unwrap(); + let result = add.get_field(&name); match result { - Field::StaticString(result) => assert_eq!(result, data), - _ => unreachable!("got {:?}: should have received static string", result), + CalcValue::Value(result) => match result { + Field::StaticString(output) => assert_eq!(output, &data), + _ => unreachable!("got {:?}, should have been a string", result), + }, + _ => unreachable!("got {:?}: should have received value", result), } } + #[test] fn can_add_uuid() { let mut add = Addition::new(); let name = Name::english(Uuid::new_v4().to_string().as_str()); 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), + let output = add.get_field(&name); + match output { + CalcValue::Value(result) => match result { + Field::Uuid(result) => assert_eq!(result, &data), + _ => unreachable!("got {:?}: should have received uuid", result), + }, + _ => unreachable!("got {:?}: should have received value", output), } } + #[test] fn can_get_document() { let mut add = Addition::new(); let name = Name::english(Uuid::new_v4().to_string().as_str()); let data = Uuid::new_v4(); add.add_field(name.clone(), data.clone()); - let doc: Document = add.get_document(); - match doc.get_field(&name).unwrap() { - Field::Uuid(result) => assert_eq!(result, data), - _ => unreachable!("should have received uuid"), + let doc = add.get_document(); + let output = doc.get_field(&name); + match output { + CalcValue::Value(holder) => match holder { + Field::Uuid(result) => assert_eq!(result, &data), + _ => unreachable!("should have received uuid"), + }, + _ => unreachable!("got {:?}: should have received value", output), } } } @@ -2414,7 +2429,7 @@ impl DocDef { self.fields.keys().cloned().collect() } - fn validate(&self, field_name: NT, value: Option) -> Result + fn validate(&self, field_name: NT, value: &Field) -> Result where NT: Into, { @@ -2425,7 +2440,10 @@ impl DocDef { self.fields.get(&id).unwrap().validate(value) } - fn set_default(&mut self, field_name: &Name, value: CV) -> Result<(), MTTError> where CV: Into { + fn set_default(&mut self, field_name: &Name, value: CV) -> Result<(), MTTError> + where + CV: Into, + { let id = match self.field_names.get_id(field_name) { Ok(data) => data, Err(err) => return Err(err), @@ -2466,7 +2484,7 @@ mod docdefs { let field_type = FieldType::Uuid; docdef.add_field(name.clone(), field_type.clone()); let result = docdef.get_field(name).unwrap(); - match result.validate(Some(Uuid::new_v4().into())) { + match result.validate(&Uuid::new_v4().into()) { Ok(_) => {} Err(err) => unreachable!("got {:?}: should have been a value", err), } @@ -2497,7 +2515,7 @@ mod docdefs { } for name in names.iter() { let result = docdef.get_field(Name::english(name.clone())).unwrap(); - match result.validate(Some("".into())) { + match result.validate(&"".into()) { Ok(_) => {} Err(err) => unreachable!("got {:?}: should have been a value", err), } @@ -2511,7 +2529,7 @@ mod docdefs { let name = Name::english("defaultfunction"); docdef.add_field(name.clone(), FieldType::StaticString); docdef.set_default(&name, FieldType::StaticString); - match docdef.get_field(name).unwrap().validate(None) { + match docdef.get_field(name).unwrap().validate(&Field::None) { Ok(data) => match data { Field::StaticString(result) => assert_eq!(result, ""), _ => unreachable!("got {:?}: should return a static string", data), @@ -2543,7 +2561,7 @@ mod docdefs { match docdef.set_default(&name, "fred") { Ok(data) => unreachable!("got {:?}, should be an error", data), Err(err) => match err { - MTTError::FieldInvalidType => {}, + MTTError::FieldInvalidType => {} _ => unreachable!("got {:?}: should have been field not found", err), }, } @@ -2915,7 +2933,6 @@ impl Calculation { } } } - // Operand::Assign => result = self.values[0].get(existing), Operand::Equal => { if self.values.len() >= 2 { result = self.values[0] @@ -3224,14 +3241,20 @@ mod replies { reply.add(doc2); let mut reply_iter = reply.iter(); let mut result1 = reply_iter.next().unwrap(); - match result1.get_field(&fieldname).unwrap() { - Field::StaticString(output) => assert_eq!(output, "one"), - _ => unreachable!("got {:?}: should have been static string", result1), + match result1.get_field(&fieldname) { + CalcValue::Value(data) => match data { + Field::StaticString(output) => assert_eq!(output, "one"), + _ => unreachable!("got {:?}: should have been static string", result1), + }, + _ => unreachable!("got {:?}, should have been value", result1), } let result2 = reply_iter.next().unwrap(); - match result2.get_field(&fieldname).unwrap() { - Field::StaticString(output) => assert_eq!(output, "two"), - _ => unreachable!("got {:?}: should have been static string", result2), + match result2.get_field(&fieldname) { + CalcValue::Value(data) => match data { + Field::StaticString(output) => assert_eq!(output, "two"), + _ => unreachable!("got {:?}: should have been static string", result2), + }, + _ => unreachable!("got {:?}, should have been value", result2), } match reply_iter.next() { None => {} @@ -3252,11 +3275,14 @@ impl InternalRecord { } } - fn insert(&mut self, id: Uuid, data: F) -> Option + fn insert(&mut self, id: Uuid, data: F) -> Field where F: Into, { - self.data.insert(id, data.into()) + match self.data.insert(id, data.into()) { + Some(data) => data.clone(), + None => Field::None, + } } fn get(&self, id: &Uuid) -> Option<&Field> { @@ -3562,13 +3588,13 @@ impl Document { self.data.insert(name.into(), field.into()); } - fn get_field(&self, name: NT) -> Option + fn get_field(&self, name: NT) -> &CalcValue where NT: Into, { match self.data.get(&name.into()) { - Some(data) => Some(data.get(&Field::None)), - None => None, + Some(data) => data, + None => &CalcValue::None, } } @@ -3619,10 +3645,13 @@ mod documents { let name = Name::english(Uuid::new_v4().to_string().as_str()); let data = Uuid::new_v4().to_string(); add.add_field(name.clone(), data.clone()); - let result = add.get_field(&name).unwrap(); + let result = add.get_field(&name); match result { - Field::StaticString(result) => assert_eq!(result, data), - _ => unreachable!("got {:?}: should have received static string", result), + CalcValue::Value(holder) => match holder { + Field::StaticString(result) => assert_eq!(result, &data), + _ => unreachable!("got {:?}: should have received static string", holder), + }, + _ => unreachable!("got {:?}, should have been value", result), } } @@ -3631,10 +3660,13 @@ mod documents { let name = Name::english(Uuid::new_v4().to_string().as_str()); let data = Uuid::new_v4(); add.add_field(name.clone(), data.clone()); - let result = add.get_field(&name).unwrap(); + let result = add.get_field(&name); match result { - Field::Uuid(result) => assert_eq!(result, data), - _ => unreachable!("got {:?}: should have received uuid", result), + CalcValue::Value(holder) => match holder { + Field::Uuid(result) => assert_eq!(result, &data), + _ => unreachable!("got {:?}: should have received static string", holder), + }, + _ => unreachable!("got {:?}, should have been value", result), } } } @@ -4113,7 +4145,7 @@ impl DocumentFile { &self.docdef } - fn validate(&self, field_name: NT, value: Option) -> Result<(Uuid, Field), MTTError> + fn validate(&self, field_name: NT, value: &Field) -> Result<(Uuid, Field), MTTError> where NT: Into, { @@ -4159,7 +4191,7 @@ impl DocumentFile { let mut holder = InternalRecord::new(); let doc = addition.get_document(); for (field, value) in doc.iter() { - match self.validate(field, Some(value)) { + match self.validate(field, &value) { Ok((id, data)) => { holder.insert(id, data); } @@ -4169,7 +4201,7 @@ impl DocumentFile { let requested: HashSet = holder.keys().cloned().collect(); let all_fields = self.docdef.field_ids(); for field in all_fields.difference(&requested).cloned() { - match self.validate(field, None) { + match self.validate(field, &Field::None) { Ok((id, data)) => { holder.insert(id, data); } @@ -4265,7 +4297,7 @@ impl DocumentFile { fn update(&mut self, update: &Update) -> MsgAction { let mut changes: HashMap = HashMap::new(); for (key, value) in update.get_values().iter() { - let (id, field) = match self.validate(key, Some(value.clone())) { + let (id, field) = match self.validate(key, &value) { Ok(data) => data, Err(err) => return err.into(), };