trillium_client/proxy/
router.rs1use super::Proxy;
2use crate::Url;
3use std::{fmt, sync::Arc};
4
5pub trait ProxyRouter: Send + Sync + 'static {
32 fn route(&self, url: &Url) -> Option<Proxy>;
34}
35
36impl ProxyRouter for Proxy {
37 fn route(&self, url: &Url) -> Option<Proxy> {
38 self.carries(url).then(|| self.clone())
39 }
40}
41
42impl<F> ProxyRouter for F
43where
44 F: Fn(&Url) -> Option<Proxy> + Send + Sync + 'static,
45{
46 fn route(&self, url: &Url) -> Option<Proxy> {
47 self(url).and_then(|proxy| proxy.route(url))
48 }
49}
50
51impl<R: ProxyRouter> ProxyRouter for Option<R> {
52 fn route(&self, url: &Url) -> Option<Proxy> {
53 self.as_ref()?.route(url)
54 }
55}
56
57impl<R: ProxyRouter> ProxyRouter for Vec<R> {
58 fn route(&self, url: &Url) -> Option<Proxy> {
59 self.iter().find_map(|router| router.route(url))
60 }
61}
62
63impl<R: ProxyRouter, const N: usize> ProxyRouter for [R; N] {
64 fn route(&self, url: &Url) -> Option<Proxy> {
65 self.iter().find_map(|router| router.route(url))
66 }
67}
68
69#[derive(Clone)]
70pub(crate) struct ArcedProxyRouter(Arc<dyn ProxyRouter>);
71
72impl ArcedProxyRouter {
73 pub(crate) fn new(router: impl ProxyRouter) -> Self {
74 Self(Arc::new(router))
75 }
76
77 pub(crate) fn route(&self, url: &Url) -> Option<Proxy> {
78 self.0.route(url)
79 }
80}
81
82impl fmt::Debug for ArcedProxyRouter {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 f.debug_struct("ProxyRouter").finish_non_exhaustive()
85 }
86}