use crate::{TransportError, TransportResult};
use serde::Serialize;
use serde_json::value::{to_raw_value, RawValue};
use std::future::Future;
use url::Url;
pub fn to_json_raw_value<S>(s: &S) -> TransportResult<Box<RawValue>>
where
S: Serialize,
{
to_raw_value(s).map_err(TransportError::ser_err)
}
pub fn guess_local_url(s: impl AsRef<str>) -> bool {
fn _guess_local_url(url: &str) -> bool {
url.parse::<Url>().map_or(false, |url| {
url.host_str().map_or(true, |host| host == "localhost" || host == "127.0.0.1")
})
}
_guess_local_url(s.as_ref())
}
#[doc(hidden)]
pub trait Spawnable {
fn spawn_task(self);
}
#[cfg(not(target_arch = "wasm32"))]
impl<T> Spawnable for T
where
T: Future<Output = ()> + Send + 'static,
{
fn spawn_task(self) {
tokio::spawn(self);
}
}
#[cfg(target_arch = "wasm32")]
impl<T> Spawnable for T
where
T: Future<Output = ()> + 'static,
{
fn spawn_task(self) {
#[cfg(not(feature = "wasm-bindgen"))]
panic!("The 'wasm-bindgen' feature must be enabled");
#[cfg(feature = "wasm-bindgen")]
wasm_bindgen_futures::spawn_local(self);
}
}