Skip to content

Commit

Permalink
Simplify agent::bind
Browse files Browse the repository at this point in the history
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
  • Loading branch information
wiktor-k committed Jun 28, 2024
1 parent 577d5fb commit c3dfb4f
Showing 1 changed file with 11 additions and 50 deletions.
61 changes: 11 additions & 50 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ where
}
}

#[cfg(unix)]
type PlatformSpecificListener = tokio::net::UnixListener;

#[cfg(windows)]
type PlatformSpecificListener = NamedPipeListener;

/// Bind to a service binding listener.
///
/// # Examples
Expand Down Expand Up @@ -485,7 +491,7 @@ where
#[cfg(unix)]
pub async fn bind<A>(listener: service_binding::Listener, agent: A) -> Result<(), AgentError>
where
A: Agent<tokio::net::UnixListener> + Agent<tokio::net::TcpListener>,
A: Agent<PlatformSpecificListener> + Agent<tokio::net::TcpListener>,
{
match listener {
#[cfg(unix)]
Expand All @@ -495,57 +501,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<dyn std::error::Error>> {
/// let args = Args::parse();
///
/// bind(args.host.try_into()?, MyAgent::default()).await?;
///
/// Ok(())
/// }
/// ```
#[cfg(windows)]
pub async fn bind<A>(listener: service_binding::Listener, agent: A) -> Result<(), AgentError>
where
A: Agent<NamedPipeListener> + Agent<tokio::net::TcpListener>,
{
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.",
))),
}
}

0 comments on commit c3dfb4f

Please sign in to comment.