use std::path::Path;
use tokio::net::UnixStream;
use crate::transport::build_stream_session;
use crate::{Error, Protocol, Session, Version};
#[derive(Debug, Clone)]
pub struct Config {
inner: crate::Config,
}
impl Config {
pub fn new(version: Version) -> Self {
Self {
inner: crate::Config::new(version),
}
}
pub fn protocols<I, S>(mut self, protocols: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.inner.protocol = Protocol::Negotiate(protocols.into_iter().map(Into::into).collect());
self
}
pub fn handshake_timeout(mut self, timeout: std::time::Duration) -> Self {
self.inner.handshake_timeout = timeout;
self
}
pub async fn connect(self, path: impl AsRef<Path>) -> Result<Session, Error> {
let stream = UnixStream::connect(path).await?;
finish(stream, self.inner, false).await
}
pub async fn accept(self, stream: UnixStream) -> Result<Session, Error> {
finish(stream, self.inner, true).await
}
}
async fn finish(
stream: UnixStream,
config: crate::Config,
is_server: bool,
) -> Result<Session, Error> {
build_stream_session(stream, config, is_server).await
}