use agent_client_protocol::{ByteStreams, ConnectTo, Error, Role};
use std::io;
use std::os::fd::AsFd;
use tokio::net::unix::pipe::{Receiver, Sender};
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
pub struct Stdio;
impl Stdio {
pub fn new() -> Self {
Self
}
}
impl<T: Role> ConnectTo<T> for Stdio {
async fn connect_to(self, client: impl ConnectTo<T::Counterpart>) -> Result<(), Error> {
let stdin = io::stdin()
.as_fd()
.try_clone_to_owned()
.and_then(Receiver::from_owned_fd)
.map_err(Error::into_internal_error)?;
let stdout = io::stdout()
.as_fd()
.try_clone_to_owned()
.and_then(Sender::from_owned_fd)
.map_err(Error::into_internal_error)?;
let streams = ByteStreams::new(stdout.compat_write(), stdin.compat());
ConnectTo::<T>::connect_to(streams, client).await
}
}
impl Default for Stdio {
fn default() -> Self {
Self::new()
}
}