1use std::marker::PhantomData;
10
11use crate::{Condition, Context, HandlerResult, Value};
12
13#[derive(Clone, Debug, Eq, PartialEq)]
15pub struct ChannelMessage<T> {
16 pub message_id: String,
18 pub value: T,
20}
21
22pub struct Channel<T> {
37 name: String,
38 marker: PhantomData<fn() -> T>,
39}
40
41impl<T> Channel<T> {
42 pub fn new(name: impl Into<String>) -> Self {
44 Self {
45 name: name.into(),
46 marker: PhantomData,
47 }
48 }
49
50 pub fn publish(&self, context: &mut Context, value: T) -> HandlerResult<()>
56 where
57 T: Value,
58 {
59 context.publish(self, value)
60 }
61
62 pub fn delete(&self, context: &mut Context, message_id: &str) -> HandlerResult<()> {
64 context.delete_channel_message(self, message_id)
65 }
66
67 pub fn size(&self, context: &Context) -> HandlerResult<usize> {
69 context.channel_size(self)
70 }
71
72 pub fn condition_results(&self, context: &Context) -> HandlerResult<Vec<T>>
78 where
79 T: Value,
80 {
81 context.channel_results(self)
82 }
83
84 pub fn for_one(&self) -> Condition {
86 self.range(Some(1), Some(1))
87 }
88
89 pub fn for_n(&self, count: usize) -> Condition {
91 self.range(Some(count), Some(count))
92 }
93
94 pub fn at_least(&self, count: usize) -> Condition {
96 self.range(Some(count), None)
97 }
98
99 pub fn at_most(&self, count: usize) -> Condition {
101 self.range(None, Some(count))
102 }
103
104 pub fn range(&self, at_least: Option<usize>, at_most: Option<usize>) -> Condition {
106 Condition::channel(self.name.clone(), None, at_least, at_most)
107 }
108
109 pub fn when_empty(&self) -> ChannelGuard {
111 ChannelGuard {
112 name: self.name.clone(),
113 instance: None,
114 }
115 }
116
117 pub(crate) fn name(&self) -> &str {
118 &self.name
119 }
120}
121
122impl<T> Clone for Channel<T> {
123 fn clone(&self) -> Self {
124 Self {
125 name: self.name.clone(),
126 marker: PhantomData,
127 }
128 }
129}
130
131pub struct ChannelMap<T> {
136 name: String,
137 marker: PhantomData<fn() -> T>,
138}
139
140impl<T> ChannelMap<T> {
141 pub fn new(name: impl Into<String>) -> Self {
143 Self {
144 name: name.into(),
145 marker: PhantomData,
146 }
147 }
148
149 pub fn publish(&self, context: &mut Context, instance: &str, value: T) -> HandlerResult<()>
155 where
156 T: Value,
157 {
158 context.publish_map(self, instance, value)
159 }
160
161 pub fn delete(
163 &self,
164 context: &mut Context,
165 instance: &str,
166 message_id: &str,
167 ) -> HandlerResult<()> {
168 context.delete_channel_map_message(self, instance, message_id)
169 }
170
171 pub fn size(&self, context: &Context, instance: &str) -> HandlerResult<usize> {
173 context.channel_map_size(self, instance)
174 }
175
176 pub fn map_size(&self, context: &Context) -> HandlerResult<usize> {
178 Ok(self.all_instance_keys(context)?.len())
179 }
180
181 pub fn all_instance_keys(&self, context: &Context) -> HandlerResult<Vec<String>> {
183 context.channel_map_keys(self)
184 }
185
186 pub fn condition_results(&self, context: &Context, instance: &str) -> HandlerResult<Vec<T>>
192 where
193 T: Value,
194 {
195 context.channel_map_results(self, instance)
196 }
197
198 pub fn for_one(&self, instance: &str) -> Condition {
200 self.range(instance, Some(1), Some(1))
201 }
202
203 pub fn for_n(&self, instance: &str, count: usize) -> Condition {
205 self.range(instance, Some(count), Some(count))
206 }
207
208 pub fn at_least(&self, instance: &str, count: usize) -> Condition {
210 self.range(instance, Some(count), None)
211 }
212
213 pub fn at_most(&self, instance: &str, count: usize) -> Condition {
215 self.range(instance, None, Some(count))
216 }
217
218 pub fn range(
220 &self,
221 instance: &str,
222 at_least: Option<usize>,
223 at_most: Option<usize>,
224 ) -> Condition {
225 Condition::channel(
226 self.name.clone(),
227 Some(instance.to_string()),
228 at_least,
229 at_most,
230 )
231 }
232
233 pub fn when_empty(&self, instance: &str) -> ChannelGuard {
235 ChannelGuard {
236 name: self.name.clone(),
237 instance: Some(instance.to_string()),
238 }
239 }
240
241 pub(crate) fn name(&self) -> &str {
242 &self.name
243 }
244}
245
246impl<T> Clone for ChannelMap<T> {
247 fn clone(&self) -> Self {
248 Self {
249 name: self.name.clone(),
250 marker: PhantomData,
251 }
252 }
253}
254
255#[derive(Clone, Debug, Eq, PartialEq)]
256pub struct ChannelGuard {
258 name: String,
259 instance: Option<String>,
260}
261
262impl ChannelGuard {
263 pub(crate) fn physical_name(&self) -> String {
264 match self.instance.as_deref() {
265 Some(instance) => crate::registry::physical_name(&self.name, instance),
266 None => self.name.clone(),
267 }
268 }
269}