From 62651ea80b9642127caf77a6414bd675c4f62d9a Mon Sep 17 00:00:00 2001 From: Sijmen Date: Tue, 16 May 2023 02:01:26 +0200 Subject: [PATCH] Make Mpd::connect use Mpd::command --- src/mpd.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/mpd.rs b/src/mpd.rs index 74c6520..29b3ef7 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -98,28 +98,24 @@ impl Mpd { } pub async fn connect() -> anyhow::Result { - let mut stream = TcpStream::connect(host()).await?; - let mut reader = BufReader::new(stream.clone()); + let stream = TcpStream::connect(host()).await?; + let reader = BufReader::new(stream.clone()); + let mut this = Self { stream, reader }; // skip OK MPD line // TODO check if it is indeed OK let mut buffer = String::new(); - reader.read_line(&mut buffer).await?; + this.reader.read_line(&mut buffer).await?; let password = std::env::var("MPD_PASSWORD").unwrap_or(String::new()); if !password.is_empty() { let password = Self::escape_str(&password); - let command = format!("password \"{password}\"\n"); - stream.write_all(command.as_bytes()).await?; - - reader.read_line(&mut buffer).await?; + this.command(&format!(r#"password "{password}""#)).await?; } - stream.write_all(b"binarylimit 1048576\n").await?; - buffer.clear(); - reader.read_line(&mut buffer).await?; + this.command("binarylimit 1048576").await?; - Ok(Self { stream, reader }) + Ok(this) } async fn read_binary_data(&mut self, size: usize) -> anyhow::Result> {