149 lines
3.8 KiB
Rust
149 lines
3.8 KiB
Rust
|
|
use super::{DocDef, Query, UserAction};
|
||
|
|
use crate::{
|
||
|
|
message::wrapper::{Addition, Delete, Records, Reply, Update},
|
||
|
|
mtterror::MTTError,
|
||
|
|
queue::data_director::Register,
|
||
|
|
};
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub enum MsgAction {
|
||
|
|
Addition(Addition),
|
||
|
|
Create(DocDef),
|
||
|
|
Error(MTTError),
|
||
|
|
OnAddition(Records),
|
||
|
|
OnDelete(Records),
|
||
|
|
OnQuery(Records),
|
||
|
|
OnUpdate(Records),
|
||
|
|
Query(Query),
|
||
|
|
Records(Records),
|
||
|
|
Register(Register),
|
||
|
|
Reply(Reply),
|
||
|
|
Show,
|
||
|
|
Delete(Delete),
|
||
|
|
Update(Update),
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Addition> for MsgAction {
|
||
|
|
fn from(value: Addition) -> Self {
|
||
|
|
MsgAction::Addition(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Delete> for MsgAction {
|
||
|
|
fn from(value: Delete) -> Self {
|
||
|
|
MsgAction::Delete(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<DocDef> for MsgAction {
|
||
|
|
fn from(value: DocDef) -> Self {
|
||
|
|
MsgAction::Create(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<MTTError> for MsgAction {
|
||
|
|
fn from(value: MTTError) -> Self {
|
||
|
|
MsgAction::Error(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Query> for MsgAction {
|
||
|
|
fn from(value: Query) -> Self {
|
||
|
|
MsgAction::Query(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Records> for MsgAction {
|
||
|
|
fn from(value: Records) -> Self {
|
||
|
|
MsgAction::Records(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Register> for MsgAction {
|
||
|
|
fn from(value: Register) -> Self {
|
||
|
|
MsgAction::Register(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Reply> for MsgAction {
|
||
|
|
fn from(value: Reply) -> Self {
|
||
|
|
MsgAction::Reply(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Update> for MsgAction {
|
||
|
|
fn from(value: Update) -> Self {
|
||
|
|
MsgAction::Update(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<UserAction> for MsgAction {
|
||
|
|
fn from(value: UserAction) -> Self {
|
||
|
|
match value {
|
||
|
|
UserAction::CreateDocument(data) => Self::Create(data),
|
||
|
|
UserAction::Query(data) => Self::Query(data),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod msgactions {
|
||
|
|
use super::*;
|
||
|
|
use crate::name::Name;
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn turn_document_definition_into_action() {
|
||
|
|
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
||
|
|
let value = DocDef::new(name.clone());
|
||
|
|
let result: MsgAction = value.into();
|
||
|
|
match result {
|
||
|
|
MsgAction::Create(def) => assert_eq!(def.get_document_names(), &[name].to_vec()),
|
||
|
|
_ => 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 = Query::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 = Reply::new();
|
||
|
|
let result: MsgAction = value.into();
|
||
|
|
match result {
|
||
|
|
MsgAction::Reply(_) => {}
|
||
|
|
_ => unreachable!("Got {:?}: dhould have been reply", result),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|