1use std::collections::VecDeque;
2use std::fmt::Display;
3
4use clap::{CommandFactory, FromArgMatches};
5use futures::Future;
6use imbl_value::imbl::OrdMap;
7use imbl_value::Value;
8use serde::de::DeserializeOwned;
9use serde::Serialize;
10
11use crate::util::PhantomData;
12use crate::{
13 CliBindings, Empty, HandlerArgs, HandlerArgsFor, HandlerFor, HandlerTypes, PrintCliResult,
14};
15
16pub struct FromFn<F, T, E, Args> {
17 _phantom: PhantomData<(T, E, Args)>,
18 function: F,
19 blocking: bool,
20 metadata: OrdMap<&'static str, Value>,
21}
22impl<F, T, E, Args> FromFn<F, T, E, Args> {
23 pub fn with_metadata(mut self, key: &'static str, value: Value) -> Self {
24 self.metadata.insert(key, value);
25 self
26 }
27}
28impl<F: Clone, T, E, Args> Clone for FromFn<F, T, E, Args> {
29 fn clone(&self) -> Self {
30 Self {
31 _phantom: PhantomData::new(),
32 function: self.function.clone(),
33 blocking: self.blocking,
34 metadata: self.metadata.clone(),
35 }
36 }
37}
38impl<F, T, E, Args> std::fmt::Debug for FromFn<F, T, E, Args> {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 f.debug_struct("FromFn")
41 .field("blocking", &self.blocking)
42 .finish()
43 }
44}
45impl<Context, F, T, E, Args> PrintCliResult<Context> for FromFn<F, T, E, Args>
46where
47 Context: crate::Context,
48 Self: HandlerTypes,
49 <Self as HandlerTypes>::Ok: Display,
50{
51 fn print(&self, _: HandlerArgsFor<Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
52 Ok(println!("{result}"))
53 }
54}
55impl<Context, F, T, E, Args> CliBindings<Context> for FromFn<F, T, E, Args>
56where
57 Context: crate::Context,
58 Self: HandlerTypes,
59 Self::Params: CommandFactory + FromArgMatches + Serialize,
60 Self: PrintCliResult<Context>,
61{
62 fn cli_command(&self) -> clap::Command {
63 Self::Params::command()
64 }
65 fn cli_parse(
66 &self,
67 matches: &clap::ArgMatches,
68 ) -> Result<(VecDeque<&'static str>, Value), clap::Error> {
69 Self::Params::from_arg_matches(matches).and_then(|a| {
70 Ok((
71 VecDeque::new(),
72 imbl_value::to_value(&a)
73 .map_err(|e| clap::Error::raw(clap::error::ErrorKind::ValueValidation, e))?,
74 ))
75 })
76 }
77 fn cli_display(
78 &self,
79 HandlerArgs {
80 context,
81 parent_method,
82 method,
83 params,
84 inherited_params,
85 raw_params,
86 }: HandlerArgsFor<Context, Self>,
87 result: Self::Ok,
88 ) -> Result<(), Self::Err> {
89 self.print(
90 HandlerArgs {
91 context,
92 parent_method,
93 method,
94 params,
95 inherited_params,
96 raw_params,
97 },
98 result,
99 )
100 }
101}
102
103pub fn from_fn<F, T, E, Args>(function: F) -> FromFn<F, T, E, Args>
104where
105 FromFn<F, T, E, Args>: HandlerTypes,
106{
107 FromFn {
108 function,
109 _phantom: PhantomData::new(),
110 blocking: false,
111 metadata: OrdMap::new(),
112 }
113}
114
115pub fn from_fn_blocking<F, T, E, Args>(function: F) -> FromFn<F, T, E, Args>
116where
117 FromFn<F, T, E, Args>: HandlerTypes,
118{
119 FromFn {
120 function,
121 _phantom: PhantomData::new(),
122 blocking: true,
123 metadata: OrdMap::new(),
124 }
125}
126
127pub struct FromFnAsync<F, Fut, T, E, Args> {
128 _phantom: PhantomData<(Fut, T, E, Args)>,
129 function: F,
130 metadata: OrdMap<&'static str, Value>,
131}
132impl<F, Fut, T, E, Args> FromFnAsync<F, Fut, T, E, Args> {
133 pub fn with_metadata(mut self, key: &'static str, value: Value) -> Self {
134 self.metadata.insert(key, value);
135 self
136 }
137}
138impl<F: Clone, Fut, T, E, Args> Clone for FromFnAsync<F, Fut, T, E, Args> {
139 fn clone(&self) -> Self {
140 Self {
141 _phantom: PhantomData::new(),
142 function: self.function.clone(),
143 metadata: self.metadata.clone(),
144 }
145 }
146}
147impl<F, Fut, T, E, Args> std::fmt::Debug for FromFnAsync<F, Fut, T, E, Args> {
148 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
149 f.debug_struct("FromFnAsync").finish()
150 }
151}
152impl<Context, F, Fut, T, E, Args> PrintCliResult<Context> for FromFnAsync<F, Fut, T, E, Args>
153where
154 Context: crate::Context,
155 Self: HandlerTypes,
156 <Self as HandlerTypes>::Ok: Display,
157{
158 fn print(&self, _: HandlerArgsFor<Context, Self>, result: Self::Ok) -> Result<(), Self::Err> {
159 Ok(println!("{result}"))
160 }
161}
162impl<Context, F, Fut, T, E, Args> CliBindings<Context> for FromFnAsync<F, Fut, T, E, Args>
163where
164 Context: crate::Context,
165 Self: HandlerTypes,
166 Self::Params: CommandFactory + FromArgMatches + Serialize,
167 Self: PrintCliResult<Context>,
168{
169 fn cli_command(&self) -> clap::Command {
170 Self::Params::command()
171 }
172 fn cli_parse(
173 &self,
174 matches: &clap::ArgMatches,
175 ) -> Result<(VecDeque<&'static str>, Value), clap::Error> {
176 Self::Params::from_arg_matches(matches).and_then(|a| {
177 Ok((
178 VecDeque::new(),
179 imbl_value::to_value(&a)
180 .map_err(|e| clap::Error::raw(clap::error::ErrorKind::ValueValidation, e))?,
181 ))
182 })
183 }
184 fn cli_display(
185 &self,
186 HandlerArgs {
187 context,
188 parent_method,
189 method,
190 params,
191 inherited_params,
192 raw_params,
193 }: HandlerArgsFor<Context, Self>,
194 result: Self::Ok,
195 ) -> Result<(), Self::Err> {
196 self.print(
197 HandlerArgs {
198 context,
199 parent_method,
200 method,
201 params,
202 inherited_params,
203 raw_params,
204 },
205 result,
206 )
207 }
208}
209
210pub fn from_fn_async<F, Fut, T, E, Args>(function: F) -> FromFnAsync<F, Fut, T, E, Args>
211where
212 FromFnAsync<F, Fut, T, E, Args>: HandlerTypes,
213{
214 FromFnAsync {
215 function,
216 _phantom: PhantomData::new(),
217 metadata: OrdMap::new(),
218 }
219}
220
221impl<F, T, E, Context, Params, InheritedParams> HandlerTypes
222 for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>>
223where
224 F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Result<T, E>
225 + Send
226 + Sync
227 + Clone
228 + 'static,
229 T: Send + Sync + 'static,
230 E: Send + Sync + 'static,
231 Context: crate::Context,
232 Params: Send + Sync,
233 InheritedParams: Send + Sync,
234{
235 type Params = Params;
236 type InheritedParams = InheritedParams;
237 type Ok = T;
238 type Err = E;
239}
240
241impl<F, T, E, Context, Params, InheritedParams> HandlerFor<Context>
242 for FromFn<F, T, E, HandlerArgs<Context, Params, InheritedParams>>
243where
244 F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Result<T, E>
245 + Send
246 + Sync
247 + Clone
248 + 'static,
249 T: Send + Sync + 'static,
250 E: Send + Sync + 'static,
251 Context: crate::Context,
252 Params: Send + Sync + 'static,
253 InheritedParams: Send + Sync + 'static,
254{
255 fn handle_sync(
256 &self,
257 handle_args: HandlerArgsFor<Context, Self>,
258 ) -> Result<Self::Ok, Self::Err> {
259 (self.function)(handle_args)
260 }
261 async fn handle_async(
262 &self,
263 handle_args: HandlerArgsFor<Context, Self>,
264 ) -> Result<Self::Ok, Self::Err> {
265 if self.blocking {
266 self.handle_async_with_sync_blocking(handle_args).await
267 } else {
268 self.handle_async_with_sync(handle_args).await
269 }
270 }
271 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
272 self.metadata.clone()
273 }
274}
275impl<F, Fut, T, E, Context, Params, InheritedParams> HandlerTypes
276 for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>>
277where
278 F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Fut + Send + Sync + Clone + 'static,
279 Fut: Future<Output = Result<T, E>> + Send + 'static,
280 T: Send + Sync + 'static,
281 E: Send + Sync + 'static,
282 Context: crate::Context,
283 Params: Send + Sync,
284 InheritedParams: Send + Sync,
285{
286 type Params = Params;
287 type InheritedParams = InheritedParams;
288 type Ok = T;
289 type Err = E;
290}
291
292impl<F, Fut, T, E, Context, Params, InheritedParams> HandlerFor<Context>
293 for FromFnAsync<F, Fut, T, E, HandlerArgs<Context, Params, InheritedParams>>
294where
295 F: Fn(HandlerArgs<Context, Params, InheritedParams>) -> Fut + Send + Sync + Clone + 'static,
296 Fut: Future<Output = Result<T, E>> + Send + 'static,
297 T: Send + Sync + 'static,
298 E: Send + Sync + 'static,
299 Context: crate::Context,
300 Params: Send + Sync + 'static,
301 InheritedParams: Send + Sync + 'static,
302{
303 async fn handle_async(
304 &self,
305 handle_args: HandlerArgsFor<Context, Self>,
306 ) -> Result<Self::Ok, Self::Err> {
307 (self.function)(handle_args).await
308 }
309 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
310 self.metadata.clone()
311 }
312}
313
314impl<F, T, E> HandlerTypes for FromFn<F, T, E, ()>
315where
316 F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static,
317 T: Send + Sync + 'static,
318 E: Send + Sync + 'static,
319{
320 type Params = Empty;
321 type InheritedParams = Empty;
322 type Ok = T;
323 type Err = E;
324}
325
326impl<Context, F, T, E> HandlerFor<Context> for FromFn<F, T, E, ()>
327where
328 Context: crate::Context,
329 F: Fn() -> Result<T, E> + Send + Sync + Clone + 'static,
330 T: Send + Sync + 'static,
331 E: Send + Sync + 'static,
332{
333 fn handle_sync(&self, _: HandlerArgsFor<Context, Self>) -> Result<Self::Ok, Self::Err> {
334 (self.function)()
335 }
336 async fn handle_async(
337 &self,
338 handle_args: HandlerArgsFor<Context, Self>,
339 ) -> Result<Self::Ok, Self::Err> {
340 if self.blocking {
341 self.handle_async_with_sync_blocking(handle_args).await
342 } else {
343 self.handle_async_with_sync(handle_args).await
344 }
345 }
346 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
347 self.metadata.clone()
348 }
349}
350impl<F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, ()>
351where
352 F: Fn() -> Fut + Send + Sync + Clone + 'static,
353 Fut: Future<Output = Result<T, E>> + Send + 'static,
354 T: Send + Sync + 'static,
355 E: Send + Sync + 'static,
356{
357 type Params = Empty;
358 type InheritedParams = Empty;
359 type Ok = T;
360 type Err = E;
361}
362
363impl<Context, F, Fut, T, E> HandlerFor<Context> for FromFnAsync<F, Fut, T, E, ()>
364where
365 Context: crate::Context,
366 F: Fn() -> Fut + Send + Sync + Clone + 'static,
367 Fut: Future<Output = Result<T, E>> + Send + 'static,
368 T: Send + Sync + 'static,
369 E: Send + Sync + 'static,
370{
371 async fn handle_async(&self, _: HandlerArgsFor<Context, Self>) -> Result<Self::Ok, Self::Err> {
372 (self.function)().await
373 }
374 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
375 self.metadata.clone()
376 }
377}
378
379impl<Context, F, T, E> HandlerTypes for FromFn<F, T, E, (Context,)>
380where
381 Context: crate::Context,
382 F: Fn(Context) -> Result<T, E> + Send + Sync + Clone + 'static,
383 T: Send + Sync + 'static,
384 E: Send + Sync + 'static,
385{
386 type Params = Empty;
387 type InheritedParams = Empty;
388 type Ok = T;
389 type Err = E;
390}
391
392impl<Context, F, T, E> HandlerFor<Context> for FromFn<F, T, E, (Context,)>
393where
394 Context: crate::Context,
395 F: Fn(Context) -> Result<T, E> + Send + Sync + Clone + 'static,
396 T: Send + Sync + 'static,
397 E: Send + Sync + 'static,
398{
399 fn handle_sync(
400 &self,
401 handle_args: HandlerArgsFor<Context, Self>,
402 ) -> Result<Self::Ok, Self::Err> {
403 (self.function)(handle_args.context)
404 }
405 async fn handle_async(
406 &self,
407 handle_args: HandlerArgsFor<Context, Self>,
408 ) -> Result<Self::Ok, Self::Err> {
409 if self.blocking {
410 self.handle_async_with_sync_blocking(handle_args).await
411 } else {
412 self.handle_async_with_sync(handle_args).await
413 }
414 }
415 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
416 self.metadata.clone()
417 }
418}
419impl<Context, F, Fut, T, E> HandlerTypes for FromFnAsync<F, Fut, T, E, (Context,)>
420where
421 Context: crate::Context,
422 F: Fn(Context) -> Fut + Send + Sync + Clone + 'static,
423 Fut: Future<Output = Result<T, E>> + Send + 'static,
424 T: Send + Sync + 'static,
425 E: Send + Sync + 'static,
426{
427 type Params = Empty;
428 type InheritedParams = Empty;
429 type Ok = T;
430 type Err = E;
431}
432
433impl<Context, F, Fut, T, E> HandlerFor<Context> for FromFnAsync<F, Fut, T, E, (Context,)>
434where
435 Context: crate::Context,
436 F: Fn(Context) -> Fut + Send + Sync + Clone + 'static,
437 Fut: Future<Output = Result<T, E>> + Send + 'static,
438 T: Send + Sync + 'static,
439 E: Send + Sync + 'static,
440{
441 async fn handle_async(
442 &self,
443 handle_args: HandlerArgsFor<Context, Self>,
444 ) -> Result<Self::Ok, Self::Err> {
445 (self.function)(handle_args.context).await
446 }
447 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
448 self.metadata.clone()
449 }
450}
451
452impl<Context, F, T, E, Params> HandlerTypes for FromFn<F, T, E, (Context, Params)>
453where
454 Context: crate::Context,
455 F: Fn(Context, Params) -> Result<T, E> + Send + Sync + Clone + 'static,
456 Params: DeserializeOwned + Send + Sync + 'static,
457 T: Send + Sync + 'static,
458 E: Send + Sync + 'static,
459{
460 type Params = Params;
461 type InheritedParams = Empty;
462 type Ok = T;
463 type Err = E;
464}
465
466impl<Context, F, T, E, Params> HandlerFor<Context> for FromFn<F, T, E, (Context, Params)>
467where
468 Context: crate::Context,
469 F: Fn(Context, Params) -> Result<T, E> + Send + Sync + Clone + 'static,
470 Params: DeserializeOwned + Send + Sync + 'static,
471 T: Send + Sync + 'static,
472 E: Send + Sync + 'static,
473{
474 fn handle_sync(
475 &self,
476 handle_args: HandlerArgsFor<Context, Self>,
477 ) -> Result<Self::Ok, Self::Err> {
478 let HandlerArgs {
479 context, params, ..
480 } = handle_args;
481 (self.function)(context, params)
482 }
483 async fn handle_async(
484 &self,
485 handle_args: HandlerArgsFor<Context, Self>,
486 ) -> Result<Self::Ok, Self::Err> {
487 if self.blocking {
488 self.handle_async_with_sync_blocking(handle_args).await
489 } else {
490 self.handle_async_with_sync(handle_args).await
491 }
492 }
493 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
494 self.metadata.clone()
495 }
496}
497impl<Context, F, Fut, T, E, Params> HandlerTypes for FromFnAsync<F, Fut, T, E, (Context, Params)>
498where
499 Context: crate::Context,
500 F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static,
501 Fut: Future<Output = Result<T, E>> + Send + 'static,
502 Params: DeserializeOwned + Send + Sync + 'static,
503 T: Send + Sync + 'static,
504 E: Send + Sync + 'static,
505{
506 type Params = Params;
507 type InheritedParams = Empty;
508 type Ok = T;
509 type Err = E;
510}
511
512impl<Context, F, Fut, T, E, Params> HandlerFor<Context>
513 for FromFnAsync<F, Fut, T, E, (Context, Params)>
514where
515 Context: crate::Context,
516 F: Fn(Context, Params) -> Fut + Send + Sync + Clone + 'static,
517 Fut: Future<Output = Result<T, E>> + Send + 'static,
518 Params: DeserializeOwned + Send + Sync + 'static,
519 T: Send + Sync + 'static,
520 E: Send + Sync + 'static,
521{
522 async fn handle_async(
523 &self,
524 handle_args: HandlerArgsFor<Context, Self>,
525 ) -> Result<Self::Ok, Self::Err> {
526 let HandlerArgs {
527 context, params, ..
528 } = handle_args;
529 (self.function)(context, params).await
530 }
531 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
532 self.metadata.clone()
533 }
534}
535
536impl<Context, F, T, E, Params, InheritedParams> HandlerTypes
537 for FromFn<F, T, E, (Context, Params, InheritedParams)>
538where
539 Context: crate::Context,
540 F: Fn(Context, Params, InheritedParams) -> Result<T, E> + Send + Sync + Clone + 'static,
541 Params: DeserializeOwned + Send + Sync + 'static,
542 InheritedParams: Send + Sync + 'static,
543 T: Send + Sync + 'static,
544 E: Send + Sync + 'static,
545{
546 type Params = Params;
547 type InheritedParams = InheritedParams;
548 type Ok = T;
549 type Err = E;
550}
551
552impl<Context, F, T, E, Params, InheritedParams> HandlerFor<Context>
553 for FromFn<F, T, E, (Context, Params, InheritedParams)>
554where
555 Context: crate::Context,
556 F: Fn(Context, Params, InheritedParams) -> Result<T, E> + Send + Sync + Clone + 'static,
557 Params: DeserializeOwned + Send + Sync + 'static,
558 InheritedParams: Send + Sync + 'static,
559 T: Send + Sync + 'static,
560 E: Send + Sync + 'static,
561{
562 fn handle_sync(
563 &self,
564 handle_args: HandlerArgsFor<Context, Self>,
565 ) -> Result<Self::Ok, Self::Err> {
566 let HandlerArgs {
567 context,
568 params,
569 inherited_params,
570 ..
571 } = handle_args;
572 (self.function)(context, params, inherited_params)
573 }
574 async fn handle_async(
575 &self,
576 handle_args: HandlerArgsFor<Context, Self>,
577 ) -> Result<Self::Ok, Self::Err> {
578 if self.blocking {
579 self.handle_async_with_sync_blocking(handle_args).await
580 } else {
581 self.handle_async_with_sync(handle_args).await
582 }
583 }
584 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
585 self.metadata.clone()
586 }
587}
588impl<Context, F, Fut, T, E, Params, InheritedParams> HandlerTypes
589 for FromFnAsync<F, Fut, T, E, (Context, Params, InheritedParams)>
590where
591 Context: crate::Context,
592 F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static,
593 Fut: Future<Output = Result<T, E>> + Send + 'static,
594 Params: DeserializeOwned + Send + Sync + 'static,
595 InheritedParams: Send + Sync + 'static,
596 T: Send + Sync + 'static,
597 E: Send + Sync + 'static,
598{
599 type Params = Params;
600 type InheritedParams = InheritedParams;
601 type Ok = T;
602 type Err = E;
603}
604
605impl<Context, F, Fut, T, E, Params, InheritedParams> HandlerFor<Context>
606 for FromFnAsync<F, Fut, T, E, (Context, Params, InheritedParams)>
607where
608 Context: crate::Context,
609 F: Fn(Context, Params, InheritedParams) -> Fut + Send + Sync + Clone + 'static,
610 Fut: Future<Output = Result<T, E>> + Send + 'static,
611 Params: DeserializeOwned + Send + Sync + 'static,
612 InheritedParams: Send + Sync + 'static,
613 T: Send + Sync + 'static,
614 E: Send + Sync + 'static,
615{
616 async fn handle_async(
617 &self,
618 handle_args: HandlerArgsFor<Context, Self>,
619 ) -> Result<Self::Ok, Self::Err> {
620 let HandlerArgs {
621 context,
622 params,
623 inherited_params,
624 ..
625 } = handle_args;
626 (self.function)(context, params, inherited_params).await
627 }
628 fn metadata(&self, _: VecDeque<&'static str>) -> OrdMap<&'static str, Value> {
629 self.metadata.clone()
630 }
631}