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
pub type Id = String;
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Request<T> {
pub id: Id,
pub payload: T,
}
impl<T> Request<T> {
pub fn new(payload: T) -> Self {
Self {
id: rand::random::<u64>().to_string(),
payload,
}
}
}
#[cfg(feature = "schemars")]
impl<T: schemars::JsonSchema> Request<T> {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(Request<T>)
}
}
impl<T> From<T> for Request<T> {
fn from(payload: T) -> Self {
Self::new(payload)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Response<T> {
pub id: Id,
pub origin_id: Id,
pub payload: T,
}
impl<T> Response<T> {
pub fn new(origin_id: Id, payload: T) -> Self {
Self {
id: rand::random::<u64>().to_string(),
origin_id,
payload,
}
}
}
#[cfg(feature = "schemars")]
impl<T: schemars::JsonSchema> Response<T> {
pub fn root_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(Response<T>)
}
}