#![forbid(unsafe_code)]
#![warn(clippy::large_futures)]
#![cfg_attr(feature = "default", doc = include_str!("../README.md"))]
#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
use std::net::SocketAddr;
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
use std::path::{Path, PathBuf};
use std::time::Duration;
pub use futures_util;
pub use url::{self, Url};
pub mod message;
#[cfg(not(target_arch = "wasm32"))]
pub mod native;
pub mod prelude;
mod socket;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
pub use self::message::Message;
#[cfg(not(target_arch = "wasm32"))]
pub use self::native::Error;
pub use self::socket::WebSocket;
#[cfg(target_arch = "wasm32")]
pub use self::wasm::Error;
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ConnectionMode {
#[default]
Direct,
#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
Proxy(SocketAddr),
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
Tor {
custom_path: Option<PathBuf>,
},
}
impl ConnectionMode {
#[inline]
pub fn direct() -> Self {
Self::Direct
}
#[inline]
#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
pub fn proxy(addr: SocketAddr) -> Self {
Self::Proxy(addr)
}
#[inline]
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
pub fn tor() -> Self {
Self::Tor { custom_path: None }
}
#[inline]
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
pub fn tor_with_path<P>(data_path: P) -> Self
where
P: AsRef<Path>,
{
Self::Tor {
custom_path: Some(data_path.as_ref().to_path_buf()),
}
}
}
#[inline]
pub async fn connect(
url: &Url,
mode: &ConnectionMode,
timeout: Duration,
) -> Result<WebSocket, Error> {
WebSocket::connect(url, mode, timeout).await
}