1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use chrono::prelude::*;
use log::Level;
use rs_data_formats_derive::Name;
use serde::{Deserialize, Serialize};
use serde_json::json;
pub const TOPIC_PREFIX: &str = concat!("tg/", env!("CARGO_PKG_VERSION_MAJOR"), "/");
pub trait Name {
fn name(&self) -> &'static str;
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Action {
Insert,
Update,
Delete,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Common<T> {
pub sender: String,
pub application: String,
pub time: DateTime<Utc>,
pub identifier: Option<String>,
pub payload: T,
pub action: Action,
}
#[derive(Serialize, Deserialize, Debug, Name)]
pub struct Log {
pub severity: Level,
pub msg: String,
}
#[derive(Serialize, Deserialize, Debug, Name)]
pub struct Kovaak {
pub score: f32,
pub scenario: String,
pub game_version: String,
}
#[derive(Serialize, Deserialize, Debug, Name)]
pub struct Homecounter {
pub name: String,
pub tab: String,
pub quantity: i32,
}
fn topic_name<T: Name>(prefix: &str, sym: &T) -> String {
format!("{}{}", prefix, sym.name().to_lowercase())
}
pub fn build_msg<T: Name + Serialize>(
sender: &str,
data: T,
action: Action,
time: Option<DateTime<Utc>>,
identifier: Option<String>,
) -> (String, String) {
(
topic_name(TOPIC_PREFIX, &data),
json!(Common {
sender: sender.to_string(),
time: time.unwrap_or_else(Utc::now),
identifier,
application: data.name().to_lowercase(),
payload: data,
action
})
.to_string(),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_topic_name() {
let k = Kovaak {
score: 0.0,
scenario: format!(""),
game_version: format!(""),
};
assert_eq!("Kovaak", topic_name(TOPIC_PREFIX, &k));
}
}