ansible_inventory_cloud/http/
list_handler.rs1use core::{future::Future, pin::Pin};
2use std::sync::Arc;
3
4use ansible_inventory::script_output::List;
5
6use crate::http::authentication::{
7 AuthenticationQuery, AuthenticationType, AuthenticationVerifier,
8};
9
10#[non_exhaustive]
14pub struct ListHandler<AQ, AO, Ctx>
15where
16 AQ: AuthenticationQuery,
17 Ctx: Clone,
18{
19 pub authentication_types: Vec<AuthenticationType>,
20 pub authentication_verifier: AuthenticationVerifier<AQ, AO, Ctx>,
21 pub fetcher: Fetcher<AO, Ctx>,
22 pub ctx: Ctx,
23}
24
25impl<AQ, AO, Ctx> ListHandler<AQ, AO, Ctx>
26where
27 AQ: AuthenticationQuery,
28 Ctx: Clone,
29{
30 pub fn new(
31 authentication_types: Vec<AuthenticationType>,
32 authentication_verifier: AuthenticationVerifier<AQ, AO, Ctx>,
33 fetcher: Fetcher<AO, Ctx>,
34 ctx: Ctx,
35 ) -> Self {
36 Self {
37 authentication_types,
38 authentication_verifier,
39 fetcher,
40 ctx,
41 }
42 }
43}
44
45impl<AQ, AO, Ctx> Clone for ListHandler<AQ, AO, Ctx>
46where
47 AQ: AuthenticationQuery,
48 Ctx: Clone,
49{
50 fn clone(&self) -> Self {
51 Self {
52 authentication_types: self.authentication_types.clone(),
53 authentication_verifier: self.authentication_verifier.clone(),
54 fetcher: self.fetcher.clone(),
55 ctx: self.ctx.clone(),
56 }
57 }
58}
59
60pub enum Fetcher<AO, Ctx> {
64 Async(
65 #[allow(clippy::type_complexity)]
66 Arc<
67 dyn Fn(
68 AO,
69 Ctx,
70 ) -> Pin<
71 Box<
72 dyn Future<Output = Result<List, Box<dyn std::error::Error>>>
73 + Send
74 + 'static,
75 >,
76 > + Send
77 + Sync,
78 >,
79 ),
80}
81impl<AO, Ctx> Clone for Fetcher<AO, Ctx> {
82 fn clone(&self) -> Self {
83 match self {
84 Self::Async(x) => Self::Async(x.clone()),
85 }
86 }
87}
88
89impl<AO, Ctx> Fetcher<AO, Ctx> {
90 pub fn new<F>(f: F) -> Self
91 where
92 F: Fn(
93 AO,
94 Ctx,
95 ) -> Pin<
96 Box<dyn Future<Output = Result<List, Box<dyn std::error::Error>>> + Send + 'static>,
97 > + Send
98 + Sync
99 + 'static,
100 {
101 Self::Async(Arc::new(f))
102 }
103}