Skip to content

Commit 82525ca

Browse files
committed
replace ErrorBox with anyhow::Error
1 parent 18b7929 commit 82525ca

File tree

16 files changed

+114
-122
lines changed

16 files changed

+114
-122
lines changed

src/als/iio.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::device_file::read;
2-
use crate::ErrorBox;
2+
use anyhow::{anyhow, Error, Result};
33
use futures_util::{FutureExt, StreamExt, TryFutureExt};
44
use smol::fs::File;
55
use smol::lock::Mutex;
@@ -28,10 +28,10 @@ pub struct Als {
2828
}
2929

3030
impl Als {
31-
pub async fn new(base_path: &str, thresholds: HashMap<u64, String>) -> Result<Self, ErrorBox> {
31+
pub async fn new(base_path: &str, thresholds: HashMap<u64, String>) -> Result<Self> {
3232
smol::fs::read_dir(base_path)
3333
.await
34-
.map_err(|e| ErrorBox::from(format!("Can't enumerate iio devices: {e}")))?
34+
.map_err(|e| anyhow!("Can't enumerate iio devices: {e}"))?
3535
.filter_map(|r| async { r.ok() })
3636
.then(|entry| {
3737
smol::fs::read_to_string(entry.path().join("name")).map(|name| (name, entry))
@@ -58,18 +58,18 @@ impl Als {
5858
.next()
5959
.await
6060
.map(|sensor| Self { sensor, thresholds })
61-
.ok_or_else(|| ErrorBox::from("No iio device found"))
61+
.ok_or_else(|| anyhow!("No iio device found"))
6262
}
6363

64-
pub async fn get(&self) -> Result<String, ErrorBox> {
64+
pub async fn get(&self) -> Result<String> {
6565
let raw = self.get_raw().await?;
6666
let profile = super::find_profile(raw, &self.thresholds);
6767

6868
log::trace!("ALS (iio): {} ({})", profile, raw);
6969
Ok(profile)
7070
}
7171

72-
async fn get_raw(&self) -> Result<u64, ErrorBox> {
72+
async fn get_raw(&self) -> Result<u64> {
7373
Ok(match self.sensor {
7474
Illuminance {
7575
ref value,
@@ -90,7 +90,7 @@ impl Als {
9090
}
9191
}
9292

93-
async fn parse_illuminance_raw(path: PathBuf) -> Result<SensorType, ErrorBox> {
93+
async fn parse_illuminance_raw(path: PathBuf) -> Result<SensorType> {
9494
Ok(Illuminance {
9595
value: Mutex::new(
9696
open_file(&path, "in_illuminance_raw")
@@ -114,8 +114,8 @@ async fn parse_illuminance_raw(path: PathBuf) -> Result<SensorType, ErrorBox> {
114114
})
115115
}
116116

117-
async fn parse_intensity_raw(path: PathBuf) -> Result<SensorType, ErrorBox> {
118-
async fn try_open_and_read(path: &Path, name: &str) -> Result<f64, ErrorBox> {
117+
async fn parse_intensity_raw(path: PathBuf) -> Result<SensorType> {
118+
async fn try_open_and_read(path: &Path, name: &str) -> Result<f64> {
119119
let mut f = open_file(path, name).await?;
120120
read(&mut f).await
121121
}
@@ -131,7 +131,7 @@ async fn parse_intensity_raw(path: PathBuf) -> Result<SensorType, ErrorBox> {
131131
})
132132
}
133133

134-
async fn parse_illuminance_input(path: PathBuf) -> Result<SensorType, ErrorBox> {
134+
async fn parse_illuminance_input(path: PathBuf) -> Result<SensorType> {
135135
Ok(Illuminance {
136136
value: Mutex::new(
137137
open_file(&path, "in_illuminance_input")
@@ -143,14 +143,14 @@ async fn parse_illuminance_input(path: PathBuf) -> Result<SensorType, ErrorBox>
143143
})
144144
}
145145

146-
async fn parse_intensity_rgb(path: PathBuf) -> Result<SensorType, ErrorBox> {
146+
async fn parse_intensity_rgb(path: PathBuf) -> Result<SensorType> {
147147
Ok(Intensity {
148148
r: Mutex::new(open_file(&path, "in_intensity_red_raw").await?),
149149
g: Mutex::new(open_file(&path, "in_intensity_green_raw").await?),
150150
b: Mutex::new(open_file(&path, "in_intensity_blue_raw").await?),
151151
})
152152
}
153153

154-
async fn open_file(path: &Path, name: &str) -> Result<File, ErrorBox> {
155-
File::open(path.join(name)).await.map_err(ErrorBox::from)
154+
async fn open_file(path: &Path, name: &str) -> Result<File> {
155+
File::open(path.join(name)).await.map_err(Error::msg)
156156
}

src/als/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use anyhow::Result;
12
use itertools::Itertools;
23
use std::collections::HashMap;
34

4-
use crate::ErrorBox;
5-
65
pub mod controller;
76
pub mod iio;
87
pub mod none;
@@ -18,7 +17,7 @@ pub enum Als {
1817
}
1918

2019
impl Als {
21-
pub async fn get(&self) -> Result<String, ErrorBox> {
20+
pub async fn get(&self) -> Result<String> {
2221
match self {
2322
Als::Webcam(als) => als.get().await,
2423
Als::Iio(als) => als.get().await,

src/als/none.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::ErrorBox;
1+
use anyhow::Result;
22

33
#[derive(Default)]
44
pub struct Als {}
55

66
impl Als {
7-
pub async fn get(&self) -> Result<String, ErrorBox> {
7+
pub async fn get(&self) -> Result<String> {
88
Ok("none".to_string())
99
}
1010
}

src/als/time.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use anyhow::Result;
12
use chrono::{Local, Timelike};
23
use std::collections::HashMap;
34

4-
use crate::ErrorBox;
5-
65
pub struct Als {
76
thresholds: HashMap<u64, String>,
87
}
@@ -12,7 +11,7 @@ impl Als {
1211
Self { thresholds }
1312
}
1413

15-
pub async fn get(&self) -> Result<String, ErrorBox> {
14+
pub async fn get(&self) -> Result<String> {
1615
let raw = Local::now().hour() as u64;
1716
let profile = super::find_profile(raw, &self.thresholds);
1817

src/als/webcam.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::channel_ext::ReceiverExt;
22
use crate::frame::compute_perceived_lightness_percent;
3-
use crate::ErrorBox;
3+
use anyhow::{anyhow, Result};
44
use itertools::Itertools;
55
use smol::channel::{Receiver, Sender};
66
use smol::lock::Mutex;
@@ -44,15 +44,15 @@ impl Webcam {
4444
thread::sleep(Duration::from_millis(WAITING_SLEEP_MS));
4545
}
4646

47-
fn frame(&mut self) -> Result<(Vec<u8>, usize), ErrorBox> {
47+
fn frame(&mut self) -> Result<(Vec<u8>, usize)> {
4848
let (device, pixels) = Self::setup(self.video)?;
4949
let mut stream = Stream::new(&device, Type::VideoCapture)?;
5050
let (rgbs, _) = stream.next()?;
5151

5252
Ok((rgbs.to_vec(), pixels))
5353
}
5454

55-
fn setup(video: usize) -> Result<(Device, usize), ErrorBox> {
55+
fn setup(video: usize) -> Result<(Device, usize)> {
5656
let device = Device::new(video)?;
5757
let mut format = device.format()?;
5858
format.fourcc = FourCC::new(b"RGB3");
@@ -67,7 +67,7 @@ impl Webcam {
6767
.collect_vec()
6868
})
6969
.min_by(|&(w1, h1), &(w2, h2)| h1.cmp(&h2).then(w1.cmp(&w2)))
70-
.ok_or("Unable to find minimum resolution")?;
70+
.ok_or(anyhow!("Unable to find minimum resolution"))?;
7171

7272
format.height = height;
7373
format.width = width;
@@ -92,15 +92,15 @@ impl Als {
9292
}
9393
}
9494

95-
pub async fn get(&self) -> Result<String, ErrorBox> {
95+
pub async fn get(&self) -> Result<String> {
9696
let raw = self.get_raw().await?;
9797
let profile = super::find_profile(raw, &self.thresholds);
9898

9999
log::trace!("ALS (webcam): {} ({})", profile, raw);
100100
Ok(profile)
101101
}
102102

103-
async fn get_raw(&self) -> Result<u64, ErrorBox> {
103+
async fn get_raw(&self) -> Result<u64> {
104104
let new_value = self
105105
.webcam_rx
106106
.recv_maybe_last()
@@ -127,15 +127,15 @@ mod tests {
127127
}
128128

129129
#[apply(test!)]
130-
async fn test_get_raw_returns_default_value_when_no_data_from_webcam() -> Result<(), ErrorBox> {
130+
async fn test_get_raw_returns_default_value_when_no_data_from_webcam() -> Result<()> {
131131
let (als, _) = setup().await;
132132

133133
assert_eq!(DEFAULT_LUX, als.get_raw().await?);
134134
Ok(())
135135
}
136136

137137
#[apply(test!)]
138-
async fn test_get_raw_returns_value_from_webcam() -> Result<(), ErrorBox> {
138+
async fn test_get_raw_returns_value_from_webcam() -> Result<()> {
139139
let (als, webcam_tx) = setup().await;
140140

141141
webcam_tx.send(42).await?;
@@ -145,7 +145,7 @@ mod tests {
145145
}
146146

147147
#[apply(test!)]
148-
async fn test_get_raw_returns_most_recent_value_from_webcam() -> Result<(), ErrorBox> {
148+
async fn test_get_raw_returns_most_recent_value_from_webcam() -> Result<()> {
149149
let (als, webcam_tx) = setup().await;
150150

151151
webcam_tx.send(42).await?;
@@ -157,8 +157,7 @@ mod tests {
157157
}
158158

159159
#[apply(test!)]
160-
async fn test_get_raw_returns_last_known_value_from_webcam_when_no_new_data(
161-
) -> Result<(), ErrorBox> {
160+
async fn test_get_raw_returns_last_known_value_from_webcam_when_no_new_data() -> Result<()> {
162161
let (als, webcam_tx) = setup().await;
163162

164163
webcam_tx.send(42).await?;

src/brightness/backlight.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::device_file::{read, write};
2-
use crate::ErrorBox;
2+
use anyhow::{anyhow, Error, Result};
33
use dbus::channel::Sender;
44
use dbus::{self, blocking::Connection, Message};
55
use inotify::{Inotify, WatchMask};
@@ -25,7 +25,7 @@ pub struct Backlight {
2525
}
2626

2727
impl Backlight {
28-
pub async fn new(path: &str, min_brightness: u64) -> Result<Self, ErrorBox> {
28+
pub async fn new(path: &str, min_brightness: u64) -> Result<Self> {
2929
let brightness_path = Path::new(path).join("brightness");
3030

3131
let current_brightness = fs::read(&brightness_path)?;
@@ -47,7 +47,7 @@ impl Backlight {
4747
let id = Path::new(path)
4848
.file_name()
4949
.and_then(|x| x.to_str())
50-
.ok_or("Unable to identify backlight ID")?;
50+
.ok_or(anyhow!("Unable to identify backlight ID"))?;
5151

5252
let subsystem = Path::new(path)
5353
.parent()
@@ -57,7 +57,7 @@ impl Backlight {
5757
"backlight" | "leds" => Some(x),
5858
_ => None,
5959
})
60-
.ok_or(format!(
60+
.ok_or(anyhow!(
6161
"Unable to identify backlight subsystem out of {path}, please open an issue on GitHub"
6262
))?;
6363

@@ -107,8 +107,8 @@ impl Backlight {
107107
})
108108
}
109109

110-
pub async fn get(&mut self) -> Result<u64, ErrorBox> {
111-
async fn update(this: &mut Backlight) -> Result<u64, ErrorBox> {
110+
pub async fn get(&mut self) -> Result<u64> {
111+
async fn update(this: &mut Backlight) -> Result<u64> {
112112
let value = read(&mut this.file).await? as u64;
113113
this.current = Some(value);
114114
Ok(value)
@@ -130,15 +130,20 @@ impl Backlight {
130130
}
131131
}
132132

133-
pub async fn set(&mut self, value: u64) -> Result<u64, ErrorBox> {
133+
pub async fn set(&mut self, value: u64) -> Result<u64> {
134134
let value = value.clamp(self.min_brightness, self.max_brightness);
135135

136136
if self.has_write_permission {
137137
write(&mut self.file, value as f64).await?;
138138
} else if let Some(dbus) = &self.dbus {
139139
dbus.connection
140-
.send(dbus.message.duplicate()?.append1(value as u32))
141-
.map_err(|_| "Unable to send brightness change message via dbus")?;
140+
.send(
141+
dbus.message
142+
.duplicate()
143+
.map_err(Error::msg)?
144+
.append1(value as u32),
145+
)
146+
.map_err(|_| anyhow!("Unable to send brightness change message via dbus"))?;
142147
self.pending_dbus_write = true;
143148
} else {
144149
Err(std::io::Error::from(ErrorKind::PermissionDenied))?

src/brightness/controller.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Controller {
131131
#[cfg(test)]
132132
mod tests {
133133
use super::*;
134-
use crate::ErrorBox;
134+
use anyhow::Result;
135135
use macro_rules_attribute::apply;
136136
use smol::channel;
137137
use smol_macros::test;
@@ -160,7 +160,7 @@ mod tests {
160160
}
161161

162162
#[apply(test!)]
163-
async fn test_step_first_run() -> Result<(), ErrorBox> {
163+
async fn test_step_first_run() -> Result<()> {
164164
let (mut controller, prediction_tx, user_rx) = setup(brightness_mock(vec![42], vec![]));
165165

166166
// even if predictor already wants a change...
@@ -179,7 +179,7 @@ mod tests {
179179
}
180180

181181
#[apply(test!)]
182-
async fn test_step_first_run_brightness_zero() -> Result<(), ErrorBox> {
182+
async fn test_step_first_run_brightness_zero() -> Result<()> {
183183
// if the current brightness value is zero...
184184
let (mut controller, prediction_tx, user_rx) = setup(brightness_mock(vec![0], vec![]));
185185

@@ -199,7 +199,7 @@ mod tests {
199199
}
200200

201201
#[apply(test!)]
202-
async fn test_step_user_changed_brightness() -> Result<(), ErrorBox> {
202+
async fn test_step_user_changed_brightness() -> Result<()> {
203203
let (mut controller, prediction_tx, user_rx) = setup(brightness_mock(vec![42], vec![]));
204204

205205
// when last brightness differs from the current one

src/brightness/ddcutil.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use anyhow::{anyhow, Result};
12
use ddc_hi::{Ddc, Display, FeatureCode};
23
use itertools::Itertools;
34
use lazy_static::lazy_static;
45
use smol::lock::Mutex;
56

6-
use crate::ErrorBox;
7-
87
lazy_static! {
98
static ref DDC_MUTEX: Mutex<()> = Mutex::new(());
109
}
@@ -18,10 +17,10 @@ pub struct DdcUtil {
1817
}
1918

2019
impl DdcUtil {
21-
pub fn new(name: &str, min_brightness: u64) -> Result<Self, ErrorBox> {
20+
pub fn new(name: &str, min_brightness: u64) -> Result<Self> {
2221
let mut display = find_display_by_name(name, true)
2322
.or_else(|| find_display_by_name(name, false))
24-
.ok_or("Unable to find display")?;
23+
.ok_or(anyhow!("Unable to find display"))?;
2524
let max_brightness = get_max_brightness(&mut display)?;
2625

2726
Ok(Self {
@@ -31,7 +30,7 @@ impl DdcUtil {
3130
})
3231
}
3332

34-
pub async fn get(&mut self) -> Result<u64, ErrorBox> {
33+
pub async fn get(&mut self) -> Result<u64> {
3534
let _lock = DDC_MUTEX.lock().await;
3635
Ok(self
3736
.display
@@ -41,7 +40,7 @@ impl DdcUtil {
4140
.value() as u64)
4241
}
4342

44-
pub async fn set(&mut self, value: u64) -> Result<u64, ErrorBox> {
43+
pub async fn set(&mut self, value: u64) -> Result<u64> {
4544
let _lock = DDC_MUTEX.lock().await;
4645
let value = value.clamp(self.min_brightness, self.max_brightness);
4746
self.display
@@ -52,7 +51,7 @@ impl DdcUtil {
5251
}
5352
}
5453

55-
fn get_max_brightness(display: &mut Display) -> Result<u64, ErrorBox> {
54+
fn get_max_brightness(display: &mut Display) -> Result<u64> {
5655
Ok(display
5756
.handle
5857
.get_vcp_feature(DDC_BRIGHTNESS_FEATURE)?

0 commit comments

Comments
 (0)