167 lines
4.1 KiB
Rust
167 lines
4.1 KiB
Rust
use crate::queue::Message;
|
|
use std::{
|
|
fmt,
|
|
sync::mpsc::Sender,
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub enum Field {
|
|
Static(String),
|
|
Uuid(Uuid),
|
|
Tx(Sender<Message>),
|
|
}
|
|
|
|
impl Field {
|
|
pub fn to_sender(&self) -> Result<Sender<Message>, String> {
|
|
match self {
|
|
Field::Tx(sender) => Ok(sender.clone()),
|
|
_ => Err("not a sender field".to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<String> 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<Uuid> for Field {
|
|
fn from(value: Uuid) -> Self {
|
|
Field::Uuid(value)
|
|
}
|
|
}
|
|
|
|
impl From<Sender<Message>> for Field {
|
|
fn from(value: Sender<Message>) -> Self {
|
|
Field::Tx(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),
|
|
Field::Tx(_) => write!(f, "{}", "message sender"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod fields {
|
|
use std::sync::mpsc::channel;
|
|
use super::*;
|
|
|
|
#[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 sender_to_field() {
|
|
let (tx, rx) = channel::<Message>();
|
|
match tx.into() {
|
|
Field::Tx(sender) => {
|
|
let msg = Message::new();
|
|
sender.send(msg).unwrap();
|
|
rx.recv().unwrap();
|
|
},
|
|
_ => unreachable!("should have been a sender"),
|
|
}
|
|
}
|
|
|
|
#[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);
|
|
}
|
|
|
|
#[test]
|
|
fn get_sender() {
|
|
let (tx, rx) = channel::<Message>();
|
|
let field: Field = tx.into();
|
|
let sender = field.to_sender().unwrap();
|
|
sender.send(Message::new()).unwrap();
|
|
rx.recv().unwrap();
|
|
}
|
|
}
|