Make Mpd::connect use Mpd::command

This commit is contained in:
Sijmen 2023-05-16 02:01:26 +02:00
parent 1cf1429753
commit 62651ea80b
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -98,28 +98,24 @@ impl Mpd {
} }
pub async fn connect() -> anyhow::Result<Self> { pub async fn connect() -> anyhow::Result<Self> {
let mut stream = TcpStream::connect(host()).await?; let stream = TcpStream::connect(host()).await?;
let mut reader = BufReader::new(stream.clone()); let reader = BufReader::new(stream.clone());
let mut this = Self { stream, reader };
// skip OK MPD line // skip OK MPD line
// TODO check if it is indeed OK // TODO check if it is indeed OK
let mut buffer = String::new(); 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()); let password = std::env::var("MPD_PASSWORD").unwrap_or(String::new());
if !password.is_empty() { if !password.is_empty() {
let password = Self::escape_str(&password); let password = Self::escape_str(&password);
let command = format!("password \"{password}\"\n"); this.command(&format!(r#"password "{password}""#)).await?;
stream.write_all(command.as_bytes()).await?;
reader.read_line(&mut buffer).await?;
} }
stream.write_all(b"binarylimit 1048576\n").await?; this.command("binarylimit 1048576").await?;
buffer.clear();
reader.read_line(&mut buffer).await?;
Ok(Self { stream, reader }) Ok(this)
} }
async fn read_binary_data(&mut self, size: usize) -> anyhow::Result<Vec<u8>> { async fn read_binary_data(&mut self, size: usize) -> anyhow::Result<Vec<u8>> {