diff --git a/src/message.rs b/src/message.rs index 85b94d9..5165ca6 100644 --- a/src/message.rs +++ b/src/message.rs @@ -920,6 +920,7 @@ enum FieldType { Uuid, } +#[derive(Clone, Debug)] enum Field { StaticString(String), Uuid(Uuid), @@ -929,19 +930,47 @@ impl Field { fn get_type(&self) -> FieldType { match self { Self::StaticString(_) => FieldType::StaticString, + Self::Uuid(_) => FieldType::Uuid, } } } +impl From for Field { + fn from(value: String) -> Self { + Self::StaticString(value) + } +} + +impl From for Field { + fn from(value: Uuid) -> Self { + Self::Uuid(value) + } +} + #[cfg(test)] mod fields { use super::*; #[test] fn can_create_static_string() { + let data = Uuid::new_v4().to_string(); + let result: Field = data.clone().into(); + match result.clone() { + Field::StaticString(output) => assert_eq!(output, data), + _ => unreachable!("got {:?}: should have been static string", result), + } + assert_eq!(result.get_type(), FieldType::StaticString); + } + + #[test] + fn create_uuid() { let data = Uuid::new_v4(); - let sstring = Field::StaticString(data.to_string()); - assert_eq!(sstring.get_type(), FieldType::StaticString); + let result: Field = data.clone().into(); + match result.clone() { + Field::Uuid(output) => assert_eq!(output, data), + _ => unreachable!("got {:?}: should have been uuid", result), + } + assert_eq!(result.get_type(), FieldType::Uuid); } } @@ -978,11 +1007,53 @@ mod fieldsettings { } #[derive(Clone, Debug)] -struct Addition; +struct Addition { + data: HashMap, +} impl Addition { fn new() -> Self { - Self {} + Self { + data: HashMap::new(), + } + } + + fn add_field(&mut self, name: String, field: F) where F: Into { + self.data.insert(name, field.into()); + } + + fn get_field(&self, name: &str) -> Option<&Field> { + self.data.get(name) + } +} + +#[cfg(test)] +mod additions { + use super::*; + + #[test] + fn can_add_static_string() { + let mut add = Addition::new(); + let name = Uuid::new_v4().to_string(); + let data = Uuid::new_v4().to_string(); + add.add_field(name.clone(), data.clone()); + let result = add.get_field(&name).unwrap(); + match result { + Field::StaticString(result) => assert_eq!(result, &data), + _ => unreachable!("got {:?}: should have received static string", result), + } + } + + fn can_add_uuid() { + let mut add = Addition::new(); + let name = Uuid::new_v4().to_string(); + 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), + } } }