flippico_cache/types/
channels.rs1use redis::{FromRedisValue, RedisResult, ToRedisArgs, Value};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, PartialEq, Debug, Deserialize)]
5pub enum SubscriptionChannel {
6 Amadeus,
7 Notifica,
8 BajkomatApi,
9 Shopify,
10}
11
12impl SubscriptionChannel {
13 pub fn get_channel(&self) -> &'static str {
14 match self {
15 SubscriptionChannel::Amadeus => "AMADEUS_CHANNEL",
16 SubscriptionChannel::Notifica => "NOTIFICA_CHANNEL",
17 SubscriptionChannel::BajkomatApi => "BAJKOMAT_API_CHANNEL",
18 SubscriptionChannel::Shopify => "SHOPIFY_CHANNEL",
19 }
20 }
21
22 pub fn from_channel(value: &str) -> Option<Self> {
23 [
24 SubscriptionChannel::Amadeus,
25 SubscriptionChannel::BajkomatApi,
26 SubscriptionChannel::Notifica,
27 SubscriptionChannel::Shopify,
28 ]
29 .into_iter()
30 .find(|variant| variant.get_channel() == value)
31 }
32}
33
34#[derive(Serialize, PartialEq, Debug, Deserialize)]
35pub enum ListChannel {
36 Amadeus,
37 Notifica,
38 BajkomatApi,
39 KsefGpt,
40 N8n,
41 Shopify,
42}
43
44impl ListChannel {
45 pub fn get_channel(&self) -> &'static str {
46 match self {
47 ListChannel::Amadeus => "AMADEUS_LIST",
48 ListChannel::Notifica => "NOTIFICA_LIST",
49 ListChannel::BajkomatApi => "BAJKOMAT_API_LIST",
50 ListChannel::KsefGpt => "KSEF_GPT_LISt",
51 ListChannel::Shopify => "SHOPIFY_LIST",
52 ListChannel::N8n => "N_8_N",
53 }
54 }
55 pub fn from_channel(value: &str) -> Option<Self> {
56 [
57 ListChannel::Amadeus,
58 ListChannel::BajkomatApi,
59 ListChannel::KsefGpt,
60 ListChannel::Notifica,
61 ListChannel::Shopify,
62 ListChannel::N8n,
63 ]
64 .into_iter()
65 .find(|variant| variant.get_channel() == value)
66 }
67}
68
69pub enum CacheSpace {
70 Amadeus,
71 Notifica,
72 BajkomatApi,
73 Shopify,
74}
75
76impl CacheSpace {
77 pub fn get_space(&self) -> &'static str {
78 match self {
79 CacheSpace::Amadeus => "AMADEUS_CACHE",
80 CacheSpace::Notifica => "NOTIFICA_CACHE",
81 CacheSpace::BajkomatApi => "BAJKOMAT_API_CACHE",
82 CacheSpace::Shopify => "SHOPIFY_CACHE",
83 }
84 }
85}
86
87#[derive(Serialize, PartialEq, Debug, Deserialize)]
88pub struct ChannelMessage {
89 pub meta: Option<MessageMeta>,
90 pub channel: SubscriptionChannel,
91 pub body: Option<serde_json::Value>,
92}
93
94#[derive(Serialize, PartialEq, Debug, Deserialize)]
95pub struct MessageMeta {
96 pub app_name: String,
97}
98
99#[derive(Serialize, PartialEq, Debug, Deserialize)]
100pub struct ListMessage {
101 pub meta: Option<MessageMeta>,
102 pub body: Option<serde_json::Value>,
103}
104
105#[derive(Serialize, PartialEq, Debug, Deserialize)]
106pub struct CacheValue<T> {
107 pub meta: Option<MessageMeta>,
108 pub value: Option<T>,
109}
110
111impl From<String> for SubscriptionChannel {
112 fn from(value: String) -> Self {
113 SubscriptionChannel::from_channel(&value).expect("Invalid channel string")
114 }
115}
116
117impl FromRedisValue for ChannelMessage {
118 fn from_redis_value(v: &Value) -> RedisResult<Self> {
119 match v {
120 Value::Array(items) => {
121 if items.len() < 2 {
122 return Err((redis::ErrorKind::TypeError, "Not enough items").into());
123 }
124 let channel: String = redis::from_redis_value(&items[0])?;
125 let mut body: Option<String> = None;
126 for chunk in items[1..].chunks(2) {
127 if chunk.len() == 2 {
128 let val = redis::from_redis_value(&chunk[1])?;
130 body = Some(val)
131 }
132 }
133
134 Ok(ChannelMessage {
135 channel: SubscriptionChannel::from(channel),
136 meta: None,
138 body: serde_json::from_str(&body.unwrap()).unwrap(),
139 })
140 }
141 _ => Err((redis::ErrorKind::TypeError, "Unexpected Redis value").into()),
142 }
143 }
144}
145
146impl ToRedisArgs for ChannelMessage {
147 fn write_redis_args<W>(&self, out: &mut W)
148 where
149 W: ?Sized + redis::RedisWrite,
150 {
151 let msg = serde_json::to_vec(&self);
152 if let Ok(message) = msg {
153 out.write_arg(&message);
154 }
155 }
156}