use crate::{ message::{Action, MsgAction}, name::NameType, }; use uuid::Uuid; #[derive(Clone, Debug, Eq, Hash)] pub enum Include { All, Just(T), } impl PartialEq for Include { fn eq(&self, other: &Self) -> bool { match self { Include::All => true, Include::Just(data) => match other { Include::All => true, Include::Just(other_data) => data == other_data, }, } } } #[cfg(test)] mod includes { use super::*; #[test] fn does_all_equal_evberything() { let a: Include = Include::All; let b: Include = Include::Just(5); let c: Include = Include::Just(7); assert!(a == a, "all should equal all"); assert!(a == b, "all should equal just"); assert!(b == a, "just should equal all"); assert!(b == b, "same just should equal"); assert!(b != c, "different justs do not equal"); } } #[derive(Clone, Debug)] pub struct Path { pub msg_id: Include, pub doc: Include, pub action: Include, } impl Path { pub fn new(id: Include, doc: Include, action: Include) -> Self { Self { msg_id: id, doc: doc, action: action, } } pub fn for_message(name: NT, action: &MsgAction) -> Self where NT: Into, { Self { msg_id: Include::Just(Uuid::new_v4()), doc: Include::Just(name.into()), action: Include::Just(action.into()), } } } #[cfg(test)] mod paths { use super::*; use crate::{ message::{MsgAction, Records}, name::{Name, Names}, }; #[test] fn can_create_for_message() { let input = [ (Name::english("one"), MsgAction::Show), ( Name::english("two"), MsgAction::Records(Records::new(Names::new())), ), ]; for item in input.iter() { let path = Path::for_message(item.0.clone(), &item.1); match path.doc { Include::Just(name) => assert_eq!(name, item.0.clone().into()), _ => unreachable!("should have returned document name"), } match path.action { Include::Just(action) => assert_eq!(action, item.1.clone().into()), _ => unreachable!("should have returned action type"), } } } #[test] fn message_ids_are_unique_for_message_paths() { let count = 10; let mut ids: Vec = Vec::new(); for _ in 0..count { let path = Path::for_message(NameType::None, &MsgAction::Show); let id = match path.msg_id { Include::Just(data) => data.clone(), Include::All => unreachable!("should have been a message id"), }; assert!(!ids.contains(&id), "{:?} is duplicated in {:?}", id, ids); ids.push(id); } } }