Skip to main content

outbox_core/
object.rs

1use uuid::Uuid;
2
3#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
4#[cfg_attr(feature = "sqlx", sqlx(transparent))]
5#[derive(Debug)]
6pub struct EventId(Uuid);
7impl Default for EventId {
8    fn default() -> Self {
9        Self(Uuid::new_v4())
10    }
11}
12impl EventId {
13    pub fn load(id: Uuid) -> Self {
14        Self(id)
15    }
16    pub fn as_uuid(&self) -> Uuid {
17        self.0
18    }
19}
20
21#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
22#[cfg_attr(feature = "sqlx", sqlx(transparent))]
23#[derive(Debug, Clone)]
24pub struct IdempotencyToken(pub String);
25impl IdempotencyToken {
26    pub fn new(token: String) -> Self {
27        Self(token)
28    }
29    pub fn as_str(&self) -> &str {
30        &self.0
31    }
32    pub fn as_bytes(&self) -> &[u8] {
33        self.0.as_bytes()
34    }
35}
36
37#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
38#[cfg_attr(feature = "sqlx", sqlx(transparent))]
39#[derive(Debug)]
40pub struct EventType(String);
41impl EventType {
42    pub fn new(event_type: &str) -> Self {
43        Self(event_type.to_string())
44    }
45    pub fn load(value: &String) -> Self {
46        Self(value.clone())
47    }
48    pub fn as_str(&self) -> &str {
49        &self.0
50    }
51}
52
53#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
54#[cfg_attr(feature = "sqlx", sqlx(transparent))]
55#[derive(Debug)]
56pub struct Payload(serde_json::Value);
57impl Payload {
58    pub fn new(payload: serde_json::Value) -> Self {
59        Self(payload)
60    }
61    pub fn load(value: &serde_json::Value) -> Self {
62        Self(value.clone())
63    }
64    pub fn as_json(&self) -> &serde_json::Value {
65        &self.0
66    }
67    pub fn from_static_str(s: &'static str) -> Self {
68        let val = serde_json::from_str(s).expect("Invalid JSON in static provider");
69        Self(val)
70    }
71}