Files
morethantext/src/action/message.rs
Jeff Baskin 837cea4ce0
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Added session id to message.
2026-03-26 12:18:38 -04:00

173 lines
4.9 KiB
Rust

use super::{Addition, ClientAction, Delete, DocDef, Query, Records, Reply, Show, Update};
use crate::{
message::MessageAction, mtterror::MTTError, name::NameType, queue::data_director::Register,
};
#[derive(Clone, Debug)]
pub enum MsgAction {
Addition(Addition),
Create(DocDef),
Delete(Delete),
DocumentCreated,
Error(MTTError),
None,
OnAddition(Records),
OnDelete(Records),
OnQuery(Records),
OnUpdate(Records),
Query(Query),
Records(Records),
Register(Register),
Reply(Reply),
Show(Show),
Update(Update),
}
impl MessageAction for MsgAction {
fn doc_name(&self) -> &NameType {
match self {
Self::Addition(data) => data.doc_name(),
Self::Create(data) => data.doc_name(),
Self::Delete(data) => data.doc_name(),
Self::DocumentCreated => &NameType::None,
Self::Error(data) => data.doc_name(),
Self::None => &NameType::None,
Self::OnAddition(data) => data.doc_name(),
Self::OnDelete(data) => data.doc_name(),
Self::OnQuery(data) => data.doc_name(),
Self::OnUpdate(data) => data.doc_name(),
Self::Query(data) => data.doc_name(),
Self::Records(data) => data.doc_name(),
Self::Register(data) => data.doc_name(),
Self::Reply(data) => data.doc_name(),
Self::Show(data) => data.doc_name(),
Self::Update(data) => data.doc_name(),
}
}
}
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<ClientAction> for MsgAction {
fn from(value: ClientAction) -> Self {
match value {
ClientAction::Addition(data) => Self::Addition(data),
ClientAction::Delete(data) => Self::Delete(data),
ClientAction::Query(data) => Self::Query(data),
ClientAction::Update(data) => Self::Update(data),
}
}
}
#[cfg(test)]
mod msgactions {
use super::*;
use crate::{mtterror::ErrorID, 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 doc_name = Name::english(Uuid::new_v4().to_string().as_str());
let expected_name: NameType = doc_name.clone().into();
let error = ErrorID::Document(expected_name.clone());
let err = MTTError::new(error.clone());
let result: MsgAction = err.into();
match result {
MsgAction::Error(err) => {
assert_eq!(err.doc_name(), &expected_name);
let err_id = err.get_error_ids().back().unwrap();
match err_id {
ErrorID::Document(_) => {}
_ => unreachable!("got {:?}, expected document name exists", err_id),
}
}
_ => unreachable!("Got {:?}: dhould have been create", result),
}
}
#[test]
fn turn_query_into_action() {
let name = Name::english(Uuid::new_v4().to_string().as_str());
let expected_name: NameType = name.clone().into();
let value = Query::new(name.clone());
let result: MsgAction = value.into();
match result {
MsgAction::Query(data) => assert_eq!(data.doc_name(), &expected_name),
_ => unreachable!("Got {:?}: dhould have been query", result),
}
}
#[test]
fn turn_reply_into_action() {
let value = Reply::new(Name::english("something"));
let result: MsgAction = value.into();
match result {
MsgAction::Reply(_) => {}
_ => unreachable!("Got {:?}: dhould have been reply", result),
}
}
}