11use crate :: device_file:: read;
2- use crate :: ErrorBox ;
2+ use anyhow :: { anyhow , Error , Result } ;
33use futures_util:: { FutureExt , StreamExt , TryFutureExt } ;
44use smol:: fs:: File ;
55use smol:: lock:: Mutex ;
@@ -28,10 +28,10 @@ pub struct Als {
2828}
2929
3030impl 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}
0 commit comments