zarumet 1.5.14

A terminal-based mpd client with album display
use mpd_client::Client;
use tokio::net::TcpStream;

#[cfg(unix)]
use tokio::net::UnixStream;

/// Connect to MPD via Unix socket or TCP based on address format
pub async fn connect_to_mpd(
    address: &str,
) -> color_eyre::Result<(Client, mpd_client::client::ConnectionEvents)> {
    let is_unix_socket = address.contains('/');

    if is_unix_socket {
        #[cfg(unix)]
        {
            let connection = UnixStream::connect(address).await?;
            Ok(Client::connect(connection).await?)
        }
        #[cfg(not(unix))]
        {
            Err(color_eyre::eyre::eyre!(
                "Unix sockets are not supported on this platform"
            ))
        }
    } else {
        let connection = TcpStream::connect(address).await?;
        Ok(Client::connect(connection).await?)
    }
}