surrealdb/api/opt/endpoint/
ws.rs

1use crate::api::engine::remote::ws::Client;
2use crate::api::engine::remote::ws::Ws;
3use crate::api::engine::remote::ws::Wss;
4use crate::api::err::Error;
5use crate::api::opt::IntoEndpoint;
6use crate::api::Endpoint;
7use crate::api::Result;
8use crate::opt::Config;
9use std::net::SocketAddr;
10use url::Url;
11
12macro_rules! endpoints {
13	($($name:ty),*) => {
14		$(
15			impl IntoEndpoint<Ws> for $name {
16				type Client = Client;
17
18				fn into_endpoint(self) -> Result<Endpoint> {
19					let url = format!("ws://{self}");
20					Ok(Endpoint {
21						url: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?,
22						path: String::new(),
23						config: Default::default(),
24					})
25				}
26			}
27
28			impl IntoEndpoint<Ws> for ($name, Config) {
29				type Client = Client;
30
31				fn into_endpoint(self) -> Result<Endpoint> {
32					let mut endpoint = IntoEndpoint::<Ws>::into_endpoint(self.0)?;
33					endpoint.config = self.1;
34					Ok(endpoint)
35				}
36			}
37
38			impl IntoEndpoint<Wss> for $name {
39				type Client = Client;
40
41				fn into_endpoint(self) -> Result<Endpoint> {
42					let url = format!("wss://{self}");
43					Ok(Endpoint {
44						url: Url::parse(&url).map_err(|_| Error::InvalidUrl(url))?,
45						path: String::new(),
46						config: Default::default(),
47					})
48				}
49			}
50
51			impl IntoEndpoint<Wss> for ($name, Config) {
52				type Client = Client;
53
54				fn into_endpoint(self) -> Result<Endpoint> {
55					let mut endpoint = IntoEndpoint::<Wss>::into_endpoint(self.0)?;
56					endpoint.config = self.1;
57					Ok(endpoint)
58				}
59			}
60		)*
61	}
62}
63
64endpoints!(&str, &String, String, SocketAddr);