pub(crate) mod backend;

mod sender;
mod receiver;

pub use sender::*;
pub use receiver::*;

use crate::Result;
use crate::traits::*;

/// Stream-based WebSocket.
#[derive(Debug)]
pub struct WebSocketWithBackend<Backend: WebSocketTrait = backend::WebSocket> {
    backend: std::marker::PhantomData<Backend>,
}

impl<Backend: WebSocketTrait> WebSocketWithBackend<Backend> {
    /// Creates a new WebSocket and connects it to the specified `url`.
    pub async fn new(url: &str) -> Result<(WebSocketSender<Backend::Sender>, WebSocketReceiver<Backend::Receiver>)>
    {
        Backend::new(url).await.map(|(sender, receiver)| (sender.into(), receiver.into()))
    }
}

/// Type alias for WebSocket with the default backend.
pub type WebSocket = WebSocketWithBackend<backend::WebSocket>;