surrealdb/api/opt/endpoint/
ws.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::api::engine::remote::ws::Client;
use crate::api::engine::remote::ws::Ws;
use crate::api::engine::remote::ws::Wss;
use crate::api::err::Error;
use crate::api::opt::IntoEndpoint;
use crate::api::Endpoint;
use crate::api::Result;
use crate::opt::Config;
use std::net::SocketAddr;
use url::Url;

macro_rules! endpoints {
	($($name:ty),*) => {
		$(
			impl IntoEndpoint<Ws> for $name {
				type Client = Client;

				fn into_endpoint(self) -> Result<Endpoint> {
					let url = format!("ws://{self}");
					Ok(Endpoint {
						url: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?,
						path: String::new(),
						config: Default::default(),
					})
				}
			}

			impl IntoEndpoint<Ws> for ($name, Config) {
				type Client = Client;

				fn into_endpoint(self) -> Result<Endpoint> {
					let mut endpoint = IntoEndpoint::<Ws>::into_endpoint(self.0)?;
					endpoint.config = self.1;
					Ok(endpoint)
				}
			}

			impl IntoEndpoint<Wss> for $name {
				type Client = Client;

				fn into_endpoint(self) -> Result<Endpoint> {
					let url = format!("wss://{self}");
					Ok(Endpoint {
						url: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?,
						path: String::new(),
						config: Default::default(),
					})
				}
			}

			impl IntoEndpoint<Wss> for ($name, Config) {
				type Client = Client;

				fn into_endpoint(self) -> Result<Endpoint> {
					let mut endpoint = IntoEndpoint::<Wss>::into_endpoint(self.0)?;
					endpoint.config = self.1;
					Ok(endpoint)
				}
			}
		)*
	}
}

endpoints!(&str, &String, String, SocketAddr);