alsa: Use ? operator instead of and_then
This commit is contained in:
committed by
Kenny Levinsen
parent
3572d22ba8
commit
b9ff6725cd
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
use crate::color::Color;
|
use crate::color::Color;
|
||||||
use crate::widgets::bar_widget::{BarWidget, BarWidgetImpl};
|
use crate::widgets::bar_widget::{BarWidget, BarWidgetImpl};
|
||||||
|
|
||||||
@@ -12,7 +11,10 @@ const SELEM_ID: u32 = 0u32;
|
|||||||
fn alsa_error_to_io_error(fail: &'static str, err: &alsa::Error) -> ::std::io::Error {
|
fn alsa_error_to_io_error(fail: &'static str, err: &alsa::Error) -> ::std::io::Error {
|
||||||
let kind = ::std::io::ErrorKind::Other;
|
let kind = ::std::io::ErrorKind::Other;
|
||||||
let func = err.func();
|
let func = err.func();
|
||||||
let errno = err.errno().map(|errno| format!("; errno = {}", errno)).unwrap_or(String::new());
|
let errno = err
|
||||||
|
.errno()
|
||||||
|
.map(|errno| format!("; errno = {}", errno))
|
||||||
|
.unwrap_or(String::new());
|
||||||
return ::std::io::Error::new(kind, format!("{}: {}{}", fail, func, errno));
|
return ::std::io::Error::new(kind, format!("{}: {}{}", fail, func, errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,35 +53,32 @@ impl Alsa {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_master_volume(&self) -> ::std::io::Result<f32> {
|
pub fn get_master_volume(&self) -> ::std::io::Result<f32> {
|
||||||
self.get_master()
|
let master = self.get_master()?;
|
||||||
.and_then(|master| {
|
|
||||||
let (min, max) = master.get_playback_volume_range();
|
let (min, max) = master.get_playback_volume_range();
|
||||||
master.get_playback_volume(SelemChannelId::mono())
|
master
|
||||||
|
.get_playback_volume(SelemChannelId::mono())
|
||||||
.map(|volume| alsa_volume_to_f32(volume, min, max))
|
.map(|volume| alsa_volume_to_f32(volume, min, max))
|
||||||
.map_err(|e| alsa_error_to_io_error("Failed to get `Master` volume", &e))
|
.map_err(|e| alsa_error_to_io_error("Failed to get `Master` volume", &e))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn set_master_volume(&self, volume: f32) -> ::std::io::Result<()> {
|
pub fn set_master_volume(&self, volume: f32) -> ::std::io::Result<()> {
|
||||||
self.get_master()
|
let master = self.get_master()?;
|
||||||
.and_then(|master| {
|
|
||||||
let (min, max) = master.get_playback_volume_range();
|
let (min, max) = master.get_playback_volume_range();
|
||||||
let volume = f32_to_alsa_volume(volume, min, max);
|
let volume = f32_to_alsa_volume(volume, min, max);
|
||||||
master.set_playback_volume_all(volume)
|
master
|
||||||
|
.set_playback_volume_all(volume)
|
||||||
.map_err(|e| alsa_error_to_io_error("Failed to set `Master` volume", &e))
|
.map_err(|e| alsa_error_to_io_error("Failed to set `Master` volume", &e))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn inc_master_volume(&self, diff: f32) -> ::std::io::Result<()> {
|
pub fn inc_master_volume(&self, diff: f32) -> ::std::io::Result<()> {
|
||||||
self.get_master()
|
let master = self.get_master()?;
|
||||||
.and_then(|master| {
|
|
||||||
let (min, max) = master.get_playback_volume_range();
|
let (min, max) = master.get_playback_volume_range();
|
||||||
master.get_playback_volume(SelemChannelId::mono())
|
master
|
||||||
|
.get_playback_volume(SelemChannelId::mono())
|
||||||
.and_then(|volume| {
|
.and_then(|volume| {
|
||||||
let volume = alsa_volume_to_f32(volume, min, max);
|
let volume = alsa_volume_to_f32(volume, min, max);
|
||||||
let volume = f32_to_alsa_volume(volume + diff, min, max);
|
let volume = f32_to_alsa_volume(volume + diff, min, max);
|
||||||
master.set_playback_volume_all(volume)
|
master.set_playback_volume_all(volume)
|
||||||
})
|
})
|
||||||
.map_err(|e| alsa_error_to_io_error("Failed to inc/dec `Master` volume", &e))
|
.map_err(|e| alsa_error_to_io_error("Failed to inc/dec `Master` volume", &e))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +89,7 @@ impl BarWidgetImpl for Alsa {
|
|||||||
fn value(&self) -> f32 {
|
fn value(&self) -> f32 {
|
||||||
match self.get_master_volume() {
|
match self.get_master_volume() {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => panic!(e)
|
Err(e) => panic!(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn color(&self) -> Color {
|
fn color(&self) -> Color {
|
||||||
|
|||||||
Reference in New Issue
Block a user