Skip to main content

rship_entities/
emitter.rs

1use myko_macros::Eventable;
2use partially::Partial;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Partial, PartialEq, Eventable, Clone, Serialize, Deserialize, Debug)]
7#[serde(rename_all = "camelCase")]
8#[partially(derive(Clone, Serialize, Deserialize, Default))]
9pub struct Emitter {
10    pub id: String,
11    pub hash: String,
12    pub name: String,
13    pub target_id: String,
14    pub service_id: String,
15    schema: Value,
16}
17
18impl Emitter {
19    pub fn new(
20        id: String,
21        name: String,
22        target_id: String,
23        service_id: String,
24        schema: Value,
25    ) -> Self {
26        Self {
27            id,
28            hash: uuid::Uuid::new_v4().to_string(),
29            name,
30            target_id,
31            service_id,
32            schema,
33        }
34    }
35
36    pub fn from_item(item: &Emitter) -> Self {
37        Self {
38            id: item.id.clone(),
39            hash: item.hash.clone(),
40            name: item.name.clone(),
41            target_id: item.target_id.clone(),
42            service_id: item.service_id.clone(),
43            schema: item.schema.clone(),
44        }
45    }
46
47    pub fn from_value(value: serde_json::Value) -> Self {
48        serde_json::from_value(value).unwrap()
49    }
50
51    pub fn to_value(&self) -> serde_json::Value {
52        serde_json::to_value(self).unwrap()
53    }
54
55    pub fn to_bytes(&self) -> Vec<u8> {
56        serde_json::to_vec(self).unwrap()
57    }
58
59    pub fn from_bytes(bytes: Vec<u8>) -> Self {
60        serde_json::from_slice(&bytes).unwrap()
61    }
62
63    pub fn get_schema(&self) -> serde_json::Value {
64        self.schema.clone()
65    }
66
67    pub fn set_schema(&mut self, schema: serde_json::Value) {
68        self.schema = schema;
69    }
70}