diff --git a/src/agent.rs b/src/agent.rs index 3a47a95..e9553ef 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -448,6 +448,12 @@ where } } +#[cfg(unix)] +type PlatformSpecificListener = tokio::net::UnixListener; + +#[cfg(windows)] +type PlatformSpecificListener = NamedPipeListener; + /// Bind to a service binding listener. /// /// # Examples @@ -482,10 +488,9 @@ where /// Ok(()) /// } /// ``` -#[cfg(unix)] pub async fn bind(listener: service_binding::Listener, agent: A) -> Result<(), AgentError> where - A: Agent + Agent, + A: Agent + Agent, { match listener { #[cfg(unix)] @@ -495,57 +500,12 @@ where service_binding::Listener::Tcp(listener) => { listen(TcpListener::from_std(listener)?, agent).await } - _ => Err(AgentError::IO(std::io::Error::other( - "Unsupported type of a listener.", - ))), - } -} - -/// Bind to a service binding listener. -/// -/// # Examples -/// -/// The following example uses `clap` to parse the host socket data -/// thus allowing the user to choose at runtime whether they want to -/// use TCP sockets, Unix domain sockets (including systemd socket -/// activation) or Named Pipes (under Windows). -/// -/// ```no_run -/// use clap::Parser; -/// use service_binding::Binding; -/// use ssh_agent_lib::agent::{bind, Session}; -/// -/// #[derive(Debug, Parser)] -/// struct Args { -/// #[clap(long, short = 'H', default_value = "unix:///tmp/ssh.sock")] -/// host: Binding, -/// } -/// -/// #[derive(Default, Clone)] -/// struct MyAgent; -/// -/// impl Session for MyAgent {} -/// -/// #[tokio::main] -/// async fn main() -> Result<(), Box> { -/// let args = Args::parse(); -/// -/// bind(args.host.try_into()?, MyAgent::default()).await?; -/// -/// Ok(()) -/// } -/// ``` -#[cfg(windows)] -pub async fn bind(listener: service_binding::Listener, agent: A) -> Result<(), AgentError> -where - A: Agent + Agent, -{ - match listener { - service_binding::Listener::Tcp(listener) => { - listen(TcpListener::from_std(listener)?, agent).await - } + #[cfg(windows)] service_binding::Listener::NamedPipe(pipe) => { listen(NamedPipeListener::bind(pipe)?, agent).await } + _ => Err(AgentError::IO(std::io::Error::other( + "Unsupported type of a listener.", + ))), } }