Skip to content

Commit 9536f13

Browse files
committed
Simplify agent::bind
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent 577d5fb commit 9536f13

File tree

1 file changed

+12
-51
lines changed

1 file changed

+12
-51
lines changed

src/agent.rs

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ where
448448
}
449449
}
450450

451+
#[cfg(unix)]
452+
type PlatformSpecificListener = tokio::net::UnixListener;
453+
454+
#[cfg(windows)]
455+
type PlatformSpecificListener = NamedPipeListener;
456+
451457
/// Bind to a service binding listener.
452458
///
453459
/// # Examples
@@ -482,10 +488,9 @@ where
482488
/// Ok(())
483489
/// }
484490
/// ```
485-
#[cfg(unix)]
486491
pub async fn bind<A>(listener: service_binding::Listener, agent: A) -> Result<(), AgentError>
487492
where
488-
A: Agent<tokio::net::UnixListener> + Agent<tokio::net::TcpListener>,
493+
A: Agent<PlatformSpecificListener> + Agent<tokio::net::TcpListener>,
489494
{
490495
match listener {
491496
#[cfg(unix)]
@@ -495,57 +500,13 @@ where
495500
service_binding::Listener::Tcp(listener) => {
496501
listen(TcpListener::from_std(listener)?, agent).await
497502
}
498-
_ => Err(AgentError::IO(std::io::Error::other(
499-
"Unsupported type of a listener.",
500-
))),
501-
}
502-
}
503-
504-
/// Bind to a service binding listener.
505-
///
506-
/// # Examples
507-
///
508-
/// The following example uses `clap` to parse the host socket data
509-
/// thus allowing the user to choose at runtime whether they want to
510-
/// use TCP sockets, Unix domain sockets (including systemd socket
511-
/// activation) or Named Pipes (under Windows).
512-
///
513-
/// ```no_run
514-
/// use clap::Parser;
515-
/// use service_binding::Binding;
516-
/// use ssh_agent_lib::agent::{bind, Session};
517-
///
518-
/// #[derive(Debug, Parser)]
519-
/// struct Args {
520-
/// #[clap(long, short = 'H', default_value = "unix:///tmp/ssh.sock")]
521-
/// host: Binding,
522-
/// }
523-
///
524-
/// #[derive(Default, Clone)]
525-
/// struct MyAgent;
526-
///
527-
/// impl Session for MyAgent {}
528-
///
529-
/// #[tokio::main]
530-
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
531-
/// let args = Args::parse();
532-
///
533-
/// bind(args.host.try_into()?, MyAgent::default()).await?;
534-
///
535-
/// Ok(())
536-
/// }
537-
/// ```
538-
#[cfg(windows)]
539-
pub async fn bind<A>(listener: service_binding::Listener, agent: A) -> Result<(), AgentError>
540-
where
541-
A: Agent<NamedPipeListener> + Agent<tokio::net::TcpListener>,
542-
{
543-
match listener {
544-
service_binding::Listener::Tcp(listener) => {
545-
listen(TcpListener::from_std(listener)?, agent).await
546-
}
503+
#[cfg(windows)]
547504
service_binding::Listener::NamedPipe(pipe) => {
548505
listen(NamedPipeListener::bind(pipe)?, agent).await
549506
}
507+
#[allow(unreachable_patterns)]
508+
_ => Err(AgentError::IO(std::io::Error::other(
509+
"Unsupported type of a listener.",
510+
))),
550511
}
551512
}

0 commit comments

Comments
 (0)