Skip to main content

trillium_testing/runtimeless/
client.rs

1use super::SERVERS;
2use crate::{RuntimelessRuntime, TestTransport};
3use std::{
4    io::{Error, ErrorKind, Result},
5    net::SocketAddr,
6};
7use trillium_server_common::Connector;
8use url::Url;
9
10/// An in-memory Connector to use with RuntimelessServer.
11#[derive(Default, Debug, Clone, Copy)]
12pub struct RuntimelessClientConfig(());
13
14impl RuntimelessClientConfig {
15    /// constructs a RuntimelessClientConfig
16    pub fn new() -> Self {
17        Self(())
18    }
19}
20
21impl Connector for RuntimelessClientConfig {
22    type Runtime = RuntimelessRuntime;
23    type Transport = TestTransport;
24    type Udp = ();
25
26    async fn connect(&self, url: &Url) -> Result<Self::Transport> {
27        let (tx, _) = &*SERVERS
28            .get(&(
29                url.host_str().unwrap().to_string(),
30                url.port_or_known_default().unwrap(),
31            ))
32            .ok_or(Error::new(ErrorKind::AddrNotAvailable, "not available"))?;
33        let (client_transport, server_transport) = TestTransport::new();
34        tx.send(server_transport).await.unwrap();
35        Ok(client_transport)
36    }
37
38    fn runtime(&self) -> Self::Runtime {
39        RuntimelessRuntime::default()
40    }
41
42    async fn resolve(&self, _host: &str, _port: u16) -> Result<Vec<SocketAddr>> {
43        Ok(vec![])
44    }
45}