//! Traits module.

use std::future::Future;

use crate::{Message, Result};

/// WebSocket trait.
pub trait WebSocketTrait: Sized {
    /// Sender type.
    type Sender: WebSocketSenderTrait;
    /// Receiver type.
    type Receiver: WebSocketReceiverTrait;

    /// Creates a new WebSocket and connects it to the specified `url`.

    fn new(url: &str) -> impl Future<Output = Result<(Self::Sender, Self::Receiver)>>;
}

/// WebSocket sender trait.
pub trait WebSocketSenderTrait {
    /// Sends a message.
    fn send(&mut self, message: Message) -> impl Future<Output = Result<()>>;
    /// Closes the WebSocket.
    fn close(&mut self, message: Option<(u16, String)>) -> impl Future<Output = Result<()>>;
}

/// WebSocket receiver trait.
pub trait WebSocketReceiverTrait {
    /// Receives a message.
    fn next(&mut self) -> impl Future<Output = Result<Option<Message>>>;
}
