1use crate::{TransportError, TransportResult};
2use serde::Serialize;
3use serde_json::value::{to_raw_value, RawValue};
4use std::future::Future;
5use url::Url;
6
7pub fn to_json_raw_value<S>(s: &S) -> TransportResult<Box<RawValue>>
10where
11 S: Serialize,
12{
13 to_raw_value(s).map_err(TransportError::ser_err)
14}
15
16pub fn guess_local_url(s: impl AsRef<str>) -> bool {
22 fn _guess_local_url(url: &str) -> bool {
23 url.parse::<Url>().is_ok_and(|url| {
24 url.host_str().is_none_or(|host| host == "localhost" || host == "127.0.0.1")
25 })
26 }
27 _guess_local_url(s.as_ref())
28}
29
30#[doc(hidden)]
31pub trait Spawnable {
32 fn spawn_task(self);
37}
38
39#[cfg(not(target_family = "wasm"))]
40impl<T> Spawnable for T
41where
42 T: Future<Output = ()> + Send + 'static,
43{
44 fn spawn_task(self) {
45 tokio::spawn(self);
46 }
47}
48
49#[cfg(target_family = "wasm")]
50impl<T> Spawnable for T
51where
52 T: Future<Output = ()> + 'static,
53{
54 fn spawn_task(self) {
55 #[cfg(not(feature = "wasm-bindgen"))]
56 panic!("The 'wasm-bindgen' feature must be enabled");
57
58 #[cfg(feature = "wasm-bindgen")]
59 wasm_bindgen_futures::spawn_local(self);
60 }
61}