1use std::marker::PhantomData;
10
11use crate::{Condition, Context, HandlerResult, Value};
12
13pub struct Channel<T> {
28 name: String,
29 marker: PhantomData<fn() -> T>,
30}
31
32impl<T> Channel<T> {
33 pub fn new(name: impl Into<String>) -> Self {
35 Self {
36 name: name.into(),
37 marker: PhantomData,
38 }
39 }
40
41 pub fn publish(&self, context: &mut Context, value: T) -> HandlerResult<()>
47 where
48 T: Value,
49 {
50 context.publish(self, value)
51 }
52
53 pub fn size(&self, context: &Context) -> HandlerResult<usize> {
55 context.channel_size(self)
56 }
57
58 pub fn condition_results(&self, context: &Context) -> HandlerResult<Vec<T>>
64 where
65 T: Value,
66 {
67 context.channel_results(self)
68 }
69
70 pub fn for_one(&self) -> Condition {
72 self.range(Some(1), Some(1))
73 }
74
75 pub fn for_n(&self, count: usize) -> Condition {
77 self.range(Some(count), Some(count))
78 }
79
80 pub fn at_least(&self, count: usize) -> Condition {
82 self.range(Some(count), None)
83 }
84
85 pub fn at_most(&self, count: usize) -> Condition {
87 self.range(None, Some(count))
88 }
89
90 pub fn range(&self, at_least: Option<usize>, at_most: Option<usize>) -> Condition {
92 Condition::channel(self.name.clone(), None, at_least, at_most)
93 }
94
95 pub fn when_empty(&self) -> ChannelGuard {
97 ChannelGuard {
98 name: self.name.clone(),
99 instance: None,
100 }
101 }
102
103 pub(crate) fn name(&self) -> &str {
104 &self.name
105 }
106}
107
108impl<T> Clone for Channel<T> {
109 fn clone(&self) -> Self {
110 Self {
111 name: self.name.clone(),
112 marker: PhantomData,
113 }
114 }
115}
116
117pub struct ChannelMap<T> {
122 name: String,
123 marker: PhantomData<fn() -> T>,
124}
125
126impl<T> ChannelMap<T> {
127 pub fn new(name: impl Into<String>) -> Self {
129 Self {
130 name: name.into(),
131 marker: PhantomData,
132 }
133 }
134
135 pub fn publish(&self, context: &mut Context, instance: &str, value: T) -> HandlerResult<()>
141 where
142 T: Value,
143 {
144 context.publish_map(self, instance, value)
145 }
146
147 pub fn size(&self, context: &Context, instance: &str) -> HandlerResult<usize> {
149 context.channel_map_size(self, instance)
150 }
151
152 pub fn map_size(&self, context: &Context) -> HandlerResult<usize> {
154 Ok(self.all_instance_keys(context)?.len())
155 }
156
157 pub fn all_instance_keys(&self, context: &Context) -> HandlerResult<Vec<String>> {
159 context.channel_map_keys(self)
160 }
161
162 pub fn condition_results(&self, context: &Context, instance: &str) -> HandlerResult<Vec<T>>
168 where
169 T: Value,
170 {
171 context.channel_map_results(self, instance)
172 }
173
174 pub fn for_one(&self, instance: &str) -> Condition {
176 self.range(instance, Some(1), Some(1))
177 }
178
179 pub fn for_n(&self, instance: &str, count: usize) -> Condition {
181 self.range(instance, Some(count), Some(count))
182 }
183
184 pub fn at_least(&self, instance: &str, count: usize) -> Condition {
186 self.range(instance, Some(count), None)
187 }
188
189 pub fn at_most(&self, instance: &str, count: usize) -> Condition {
191 self.range(instance, None, Some(count))
192 }
193
194 pub fn range(
196 &self,
197 instance: &str,
198 at_least: Option<usize>,
199 at_most: Option<usize>,
200 ) -> Condition {
201 Condition::channel(
202 self.name.clone(),
203 Some(instance.to_string()),
204 at_least,
205 at_most,
206 )
207 }
208
209 pub fn when_empty(&self, instance: &str) -> ChannelGuard {
211 ChannelGuard {
212 name: self.name.clone(),
213 instance: Some(instance.to_string()),
214 }
215 }
216
217 pub(crate) fn name(&self) -> &str {
218 &self.name
219 }
220}
221
222impl<T> Clone for ChannelMap<T> {
223 fn clone(&self) -> Self {
224 Self {
225 name: self.name.clone(),
226 marker: PhantomData,
227 }
228 }
229}
230
231#[derive(Clone, Debug, Eq, PartialEq)]
232pub struct ChannelGuard {
234 name: String,
235 instance: Option<String>,
236}
237
238impl ChannelGuard {
239 pub(crate) fn physical_name(&self) -> String {
240 match self.instance.as_deref() {
241 Some(instance) => crate::registry::physical_name(&self.name, instance),
242 None => self.name.clone(),
243 }
244 }
245}