Skip to content

Instantly share code, notes, and snippets.

@etamponi
Created November 3, 2019 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etamponi/9c9b6161e6f49daa4fe39d73a622bf6a to your computer and use it in GitHub Desktop.
Save etamponi/9c9b6161e6f49daa4fe39d73a622bf6a to your computer and use it in GitHub Desktop.
Specific Message types + generic Message type
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
pub struct AgentState {
agent_id: String,
agent_name: Option<String>,
messages: Vec<Message>,
position: Option<Vec<f64>>,
direction: Option<Vec<f64>>,
custom: HashMap<String, serde_json::Value>,
}
impl AgentState {
pub fn empty() -> AgentState {
AgentState {
agent_id: uuid::Uuid::new_v4().to_string(),
agent_name: None,
messages: vec![],
position: None,
direction: None,
custom: HashMap::new(),
}
}
}
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum Message {
CreateUser(CreateUserMessage),
RemoveUser(RemoveUserMessage),
Generic(GenericMessage),
}
#[derive(Serialize, Deserialize)]
pub enum CreateUser {
#[serde(rename = "create_user")]
Type,
}
#[derive(Serialize, Deserialize)]
pub enum RemoveUser {
#[serde(rename = "remove_user")]
Type,
}
#[derive(Serialize, Deserialize)]
pub struct RemoveUserPayload {
agent_id: String,
}
#[derive(Serialize, Deserialize)]
pub enum Hash {
#[serde(rename = "hash")]
Id,
}
#[derive(Serialize, Deserialize)]
pub struct CreateUserMessage {
r#type: CreateUser,
to: Hash,
from: String,
data: AgentState,
}
#[derive(Serialize, Deserialize)]
pub struct RemoveUserMessage {
r#type: RemoveUser,
to: Hash,
from: String,
data: RemoveUserPayload,
}
#[derive(Serialize, Deserialize)]
pub struct GenericMessage {
r#type: String,
to: String,
from: String,
data: serde_json::Value,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_user_message() {
let msg = Message::CreateUser(CreateUserMessage {
r#type: CreateUser::Type,
to: Hash::Id,
from: "some_agent".to_string(),
data: AgentState::empty(),
});
let json = serde_json::to_string(&msg).unwrap();
let msg_from_json: Message = serde_json::from_str(&json).unwrap();
match msg_from_json {
Message::CreateUser(_) => (),
_ => panic!("Expected CreateUser message")
};
}
#[test]
fn test_remove_user_message() {
let msg = Message::RemoveUser(RemoveUserMessage {
r#type: RemoveUser::Type,
to: Hash::Id,
from: "some_agent".to_string(),
data: RemoveUserPayload { agent_id: "old_agent".to_string() },
});
let json = serde_json::to_string_pretty(&msg).unwrap();
let msg_from_json: Message = serde_json::from_str(&json).unwrap();
match msg_from_json {
Message::RemoveUser(_) => (),
_ => panic!("Expected RemoveUser message")
};
}
#[test]
fn test_generic_message() {
let msg = Message::Generic(GenericMessage {
r#type: "custom_message".to_string(),
to: "some_other_agent".to_string(),
from: "some_agent".to_string(),
data: json!({
"foo": "bar",
}),
});
let json = serde_json::to_string(&msg).unwrap();
let msg_from_json: Message = serde_json::from_str(&json).unwrap();
match msg_from_json {
Message::Generic(msg) => {
assert_eq!(msg.to, "some_other_agent");
}
_ => panic!("Expected Generic message")
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment