diff --git a/src/message.rs b/src/message.rs index 0ed9ad5..c490337 100644 --- a/src/message.rs +++ b/src/message.rs @@ -80,9 +80,95 @@ impl From<&NameID> for NameID { #[derive(Clone, Debug)] enum MsgAction { Create(DocDef), + // Alter + // Remove Error(MTTError), Query(Access), Reply(Response), + // Add + // Delete +} + +impl From for MsgAction { + fn from(value: DocDef) -> Self { + MsgAction::Create(value) + } +} + +impl From for MsgAction { + fn from(value: MTTError) -> Self { + MsgAction::Error(value) + } +} + +impl From for MsgAction { + fn from(value: Access) -> Self { + MsgAction::Query(value) + } +} + +impl From for MsgAction { + fn from(value: Response) -> Self { + MsgAction::Reply(value) + } +} + +#[cfg(test)] +mod msgactions { + use super::*; + + #[test] + fn turn_document_definition_into_action() { + let value = DocDef::new(); + let result: MsgAction = value.into(); + match result { + MsgAction::Create(_) => {}, + _ => unreachable!("Got {:?}: dhould have been create", result), + } + } + + #[test] + fn turn_error_into_action() { + let data = "data".to_string(); + let value = MTTError::DocumentAlreadyExists(data.clone()); + let result: MsgAction = value.into(); + match result { + MsgAction::Error(result) => match result { + MTTError::DocumentAlreadyExists(output) => assert_eq!(output, data), + _ => unreachable!("Got {:?}: dhould have been create", result), + }, + _ => unreachable!("Got {:?}: dhould have been create", result), + } + let value = MTTError::DocumentNotFound(data.clone()); + let result: MsgAction = value.into(); + match result { + MsgAction::Error(result) => match result { + MTTError::DocumentNotFound(output) => assert_eq!(output, data), + _ => unreachable!("Got {:?}: dhould have been create", result), + }, + _ => unreachable!("Got {:?}: dhould have been create", result), + } + } + + #[test] + fn turn_query_into_action() { + let value = Access::new(); + let result: MsgAction = value.into(); + match result { + MsgAction::Query(_) => {}, + _ => unreachable!("Got {:?}: dhould have been query", result), + } + } + + #[test] + fn turn_reply_into_action() { + let value = Response::new(); + let result: MsgAction = value.into(); + match result { + MsgAction::Reply(_) => {}, + _ => unreachable!("Got {:?}: dhould have been reply", result), + } + } } #[derive(Clone)] @@ -93,14 +179,15 @@ struct Message { } impl Message { - fn new(doc_id: D, action: MsgAction) -> Self + fn new(doc_id: D, action: A) -> Self where D: Into, + A: Into, { Self { msg_id: Uuid::new_v4(), document_id: doc_id.into(), - action: action, + action: action.into(), } } @@ -892,6 +979,16 @@ impl Document { } } +#[cfg(test)] +mod documents { + use super::support_test::TIMEOUT; + use super::*; + + #[test] + fn has_data_fields() { + } +} + #[cfg(test)] mod createdocs { use super::support_test::TIMEOUT;