use chrono::Utc; use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InfraQueueMessage {
pub id: String, pub timestamp: u64, pub sender: String, pub topic: String, pub payload: String, pub retry_count: u8, pub priority: Option<u8>, }
impl InfraQueueMessage {
pub fn new(
sender: impl Into<String>,
topic: impl Into<String>,
payload: impl Into<String>,
) -> Self {
let ts_ms_i64 = Utc::now().timestamp_millis();
let ts_ms_u64 = if ts_ms_i64 >= 0 { ts_ms_i64 as u64 } else { 0 };
let id = Uuid::new_v4().to_string();
Self {
id,
timestamp: ts_ms_u64,
sender: sender.into(),
topic: topic.into(),
payload: payload.into(),
retry_count: 0,
priority: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_new_basics() {
let m = InfraQueueMessage::new("s", "t", "p");
assert!(!m.id.is_empty());
assert!(m.timestamp > 0);
assert_eq!(m.sender, "s");
assert_eq!(m.topic, "t");
assert_eq!(m.payload, "p");
assert_eq!(m.retry_count, 0);
assert!(m.priority.is_none());
}
}