use crate::queue::Message; use std::{fmt, sync::mpsc::Sender}; use uuid::Uuid; #[derive(Clone, Debug)] pub enum Field { Static(String), Uuid(Uuid), } impl From for Field { fn from(value: String) -> Self { match Uuid::try_from(value.as_str()) { Ok(data) => return Field::Uuid(data), Err(_) => {} } Field::Static(value) } } impl From<&str> for Field { fn from(value: &str) -> Self { match Uuid::try_from(value) { Ok(data) => return Field::Uuid(data), Err(_) => {} } Field::Static(value.into()) } } impl From for Field { fn from(value: Uuid) -> Self { Field::Uuid(value) } } impl fmt::Display for Field { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Field::Uuid(data) => write!(f, "{}", data), Field::Static(data) => write!(f, "{}", data), } } } #[cfg(test)] mod fields { use super::*; use std::sync::mpsc::channel; #[test] fn string_to_field() { let entries = ["test1".to_string(), "test2".to_string()]; for data in entries { match data.clone().into() { Field::Static(result) => assert_eq!(result, data), _ => unreachable!("shouuld have been a static field"), } } } #[test] fn str_to_field() { let entries = ["test1", "test2"]; for data in entries { match data.into() { Field::Static(result) => assert_eq!(result, data), _ => unreachable!("shouuld have been a static field"), } } } #[test] fn uuid_to_field() { let id = Uuid::new_v4(); match id.into() { Field::Uuid(result) => assert_eq!(result, id), _ => unreachable!("should have been a uuid field"), } } #[test] fn uuid_string_to_field() { let id = Uuid::new_v4(); let id_string = id.to_string(); match id_string.into() { Field::Uuid(result) => assert_eq!(result, id), _ => unreachable!("should have been a uuid field"), } } #[test] fn uuid_str_to_field() { let id = Uuid::new_v4(); let id_string = id.to_string(); let id_str = id_string.as_str(); match id_str.into() { Field::Uuid(result) => assert_eq!(result, id), _ => unreachable!("should have been a uuid field"), } } #[test] fn uuid_field_to_string() { let id = Uuid::new_v4(); let result = id.to_string(); let input = Field::Uuid(id); assert_eq!(input.to_string(), result); } #[test] fn str_field_to_string() { let result = "Something goes here"; let input: Field = result.into(); assert_eq!(input.to_string(), result); } #[test] fn string_field_to_string() { let result = "Another string".to_string(); let input: Field = result.clone().into(); assert_eq!(input.to_string(), result); } }