1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-04 18:29:45 +02:00

Implement common SinkError and SinkResult (#820)

* Make error messages more consistent and concise.

* `impl From<AlsaError> for io::Error` so `AlsaErrors` can be thrown to player as `io::Errors`. This little bit of boilerplate goes a long way to simplifying things further down in the code. And will make any needed future changes easier.

* Bonus: handle ALSA backend buffer sizing a little better.
This commit is contained in:
Jason Gray 2021-09-27 13:46:26 -05:00 committed by GitHub
parent 57937a10d9
commit 8d70fd910e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 275 additions and 220 deletions

View file

@ -1,4 +1,4 @@
use super::{Open, Sink, SinkAsBytes};
use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult};
use crate::config::AudioFormat;
use crate::convert::Converter;
use crate::decoder::AudioPacket;
@ -23,14 +23,14 @@ impl Open for StdoutSink {
}
impl Sink for StdoutSink {
fn start(&mut self) -> io::Result<()> {
fn start(&mut self) -> SinkResult<()> {
if self.output.is_none() {
let output: Box<dyn Write> = match self.path.as_deref() {
Some(path) => {
let open_op = OpenOptions::new()
.write(true)
.open(path)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(|e| SinkError::ConnectionRefused(e.to_string()))?;
Box::new(open_op)
}
None => Box::new(io::stdout()),
@ -46,14 +46,18 @@ impl Sink for StdoutSink {
}
impl SinkAsBytes for StdoutSink {
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> {
match self.output.as_deref_mut() {
Some(output) => {
output.write_all(data)?;
output.flush()?;
output
.write_all(data)
.map_err(|e| SinkError::OnWrite(e.to_string()))?;
output
.flush()
.map_err(|e| SinkError::OnWrite(e.to_string()))?;
}
None => {
return Err(io::Error::new(io::ErrorKind::Other, "Output is None"));
return Err(SinkError::NotConnected("Output is None".to_string()));
}
}