use std::sync::Arc;
use async_trait::async_trait;
use http::HeaderMap;
use crate::error::Error;
#[async_trait]
pub trait Transport: Send + Sync {
async fn dial(&self, url: &str, options: DialOptions) -> Result<Box<dyn Conn>, Error>;
}
#[async_trait]
impl<T: Transport + ?Sized> Transport for Arc<T> {
async fn dial(&self, url: &str, options: DialOptions) -> Result<Box<dyn Conn>, Error> {
(**self).dial(url, options).await
}
}
#[derive(Debug, Clone, Default)]
pub struct DialOptions {
pub subprotocols: Vec<String>,
pub headers: HeaderMap,
}
#[async_trait]
pub trait Conn: Send + Sync {
fn subprotocol(&self) -> Option<&str>;
async fn read(&self) -> Result<Vec<u8>, Error>;
async fn write(&self, payload: &[u8]) -> Result<(), Error>;
async fn close(&self);
async fn close_with_status(&self, code: u16, reason: &str) {
let _ = (code, reason);
self.close().await;
}
}