diff --git a/src/message.rs b/src/message.rs index a066888..e449256 100644 --- a/src/message.rs +++ b/src/message.rs @@ -959,6 +959,15 @@ enum FieldType { Uuid, } +impl FieldType { + fn get_default(&self) -> Field { + match self { + FieldType::StaticString => "".into(), + FieldType::Uuid => Uuid::new_v4().into(), + } + } +} + impl From<&Field> for FieldType { fn from(value: &Field) -> Self { match value { @@ -968,6 +977,37 @@ impl From<&Field> for FieldType { } } +#[cfg(test)] +mod fieldtypes { + use super::*; + + #[test] + fn can_get_defaults_for_uuid() { + let ftype = FieldType::Uuid; + let mut ids: Vec = Vec::new(); + for _ in 0..5 { + let result = ftype.get_default(); + match result { + Field::Uuid(data) => { + assert!(!ids.contains(&data), "found duplicate id {:?} in {:?}", data, ids); + ids.push(data.clone()); + }, + _ => unreachable!("got {:?}: should have been uuid", result), + } + } + } + + #[test] + fn can_get_defaults_for_static_string() { + let ftype = FieldType::StaticString; + let result = ftype.get_default(); + match result { + Field::StaticString(data) => assert_eq!(data, ""), + _ => unreachable!("got {:?}: should have been static string", result), + } + } +} + #[derive(Clone, Debug, PartialEq)] enum Field { StaticString(String),