megaphone/dto/
message.rs

1use chrono::{DateTime, Utc};
2use rand::Rng;
3use rand::distributions::Alphanumeric;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Clone, Debug)]
7pub struct EventDto {
8    #[serde(rename = "sid")]
9    pub stream_id: String,
10    #[serde(rename = "eid")]
11    pub event_id: String,
12    #[serde(rename = "ts")]
13    pub timestamp: DateTime<Utc>,
14    pub body: serde_json::Value,
15}
16
17impl EventDto {
18    pub fn new(
19        stream_id: String,
20        body: serde_json::Value,
21    ) -> Self {
22        Self {
23            stream_id,
24            event_id: rand::thread_rng()
25                .sample_iter(&Alphanumeric)
26                .take(23)
27                .map(char::from)
28                .collect(),
29            timestamp: Utc::now(),
30            body,
31        }
32    }
33}