1use mpd_client::client::Connection;
2use mpd_client::protocol::MpdProtocolError;
3use mpd_client::Client;
4use std::os::unix::fs::FileTypeExt;
5use std::path::PathBuf;
6use tokio::net::{TcpStream, UnixStream};
7
8pub(crate) async fn try_get_connection(host: &str) -> Result<Connection, MpdProtocolError> {
12 if is_unix_socket(host) {
13 connect_unix(host).await
14 } else {
15 connect_tcp(host).await
16 }
17}
18
19fn is_unix_socket(host: &str) -> bool {
20 let path = PathBuf::from(host);
21 path.exists()
22 && path
23 .metadata()
24 .map_or(false, |metadata| metadata.file_type().is_socket())
25}
26
27async fn connect_unix(host: &str) -> Result<Connection, MpdProtocolError> {
28 let connection = UnixStream::connect(host).await?;
29 Client::connect(connection).await
30}
31
32async fn connect_tcp(host: &str) -> Result<Connection, MpdProtocolError> {
33 let connection = TcpStream::connect(host).await?;
34 Client::connect(connection).await
35}