Skip to main content

ansible_inventory_cloud/http/
host_handler.rs

1use core::{future::Future, pin::Pin};
2use std::sync::Arc;
3
4use ansible_inventory::script_output::Host;
5
6use crate::http::{
7    authentication::{AuthenticationQuery, AuthenticationType, AuthenticationVerifier},
8    hostname::{Hostname, HostnameAxumPath, HostnameQuery, HostnameType},
9};
10
11//
12#[non_exhaustive]
13pub struct HostHandler<AQ, AO, HAP, HQ, Ctx>
14where
15    AQ: AuthenticationQuery,
16    HAP: HostnameAxumPath,
17    HQ: HostnameQuery,
18    Ctx: Clone,
19{
20    pub authentication_types: Vec<AuthenticationType>,
21    pub authentication_verifier: AuthenticationVerifier<AQ, AO, Ctx>,
22    pub hostname_types: Vec<HostnameType>,
23    pub fetcher: Fetcher<AO, HAP, HQ, Ctx>,
24    pub ctx: Ctx,
25}
26
27impl<AQ, AO, HAP, HQ, Ctx> HostHandler<AQ, AO, HAP, HQ, Ctx>
28where
29    AQ: AuthenticationQuery,
30    HAP: HostnameAxumPath,
31    HQ: HostnameQuery,
32    Ctx: Clone,
33{
34    pub fn new(
35        authentication_types: Vec<AuthenticationType>,
36        authentication_verifier: AuthenticationVerifier<AQ, AO, Ctx>,
37        hostname_types: Vec<HostnameType>,
38        fetcher: Fetcher<AO, HAP, HQ, Ctx>,
39        ctx: Ctx,
40    ) -> Self {
41        Self {
42            authentication_types,
43            authentication_verifier,
44            hostname_types,
45            fetcher,
46            ctx,
47        }
48    }
49}
50
51impl<AQ, AO, HAP, HQ, Ctx> Clone for HostHandler<AQ, AO, HAP, HQ, Ctx>
52where
53    AQ: AuthenticationQuery,
54    HAP: HostnameAxumPath,
55    HQ: HostnameQuery,
56    Ctx: Clone,
57{
58    fn clone(&self) -> Self {
59        Self {
60            authentication_types: self.authentication_types.clone(),
61            authentication_verifier: self.authentication_verifier.clone(),
62            hostname_types: self.hostname_types.clone(),
63            fetcher: self.fetcher.clone(),
64            ctx: self.ctx.clone(),
65        }
66    }
67}
68
69//
70//
71//
72pub enum Fetcher<AO, HAP, HQ, Ctx>
73where
74    HAP: HostnameAxumPath,
75    HQ: HostnameQuery,
76{
77    Async(
78        #[allow(clippy::type_complexity)]
79        Arc<
80            dyn Fn(
81                    Hostname<HAP, HQ>,
82                    AO,
83                    Ctx,
84                ) -> Pin<
85                    Box<
86                        dyn Future<Output = Result<Host, Box<dyn std::error::Error>>>
87                            + Send
88                            + 'static,
89                    >,
90                > + Send
91                + Sync,
92        >,
93    ),
94}
95impl<AO, HAP, HQ, Ctx> Clone for Fetcher<AO, HAP, HQ, Ctx>
96where
97    HAP: HostnameAxumPath,
98    HQ: HostnameQuery,
99{
100    fn clone(&self) -> Self {
101        match self {
102            Self::Async(x) => Self::Async(x.clone()),
103        }
104    }
105}
106
107impl<AO, HAP, HQ, Ctx> Fetcher<AO, HAP, HQ, Ctx>
108where
109    HAP: HostnameAxumPath,
110    HQ: HostnameQuery,
111{
112    pub fn new<F>(f: F) -> Self
113    where
114        F: Fn(
115                Hostname<HAP, HQ>,
116                AO,
117                Ctx,
118            ) -> Pin<
119                Box<dyn Future<Output = Result<Host, Box<dyn std::error::Error>>> + Send + 'static>,
120            > + Send
121            + Sync
122            + 'static,
123    {
124        Self::Async(Arc::new(f))
125    }
126}