From 543e5a052e659baafbfe4d783224e1cd79830137 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Tue, 20 May 2025 19:12:58 +0300 Subject: [PATCH] fs: fix read() typesig --- src/cpu.rs | 4 ++-- src/fs.rs | 12 ++++++------ src/power_supply.rs | 5 +++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 7b8ef99..0179746 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -86,7 +86,7 @@ impl Cpu { pub fn get_available_governors(&self) -> Vec { let Self { number, .. } = self; - let Ok(Some(content)) = fs::read(format!( + let Some(Ok(content)) = fs::read(format!( "/sys/devices/system/cpu/cpu{number}/cpufreq/scaling_available_governors" )) else { return Vec::new(); @@ -127,7 +127,7 @@ impl Cpu { pub fn get_available_epps(&self) -> Vec { let Self { number, .. } = self; - let Ok(Some(content)) = fs::read(format!( + let Some(Ok(content)) = fs::read(format!( "/sys/devices/system/cpu/cpu{number}/cpufreq/energy_performance_available_preferences" )) else { return Vec::new(); diff --git a/src/fs.rs b/src/fs.rs index f3eeb2c..b1d1c71 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -15,17 +15,17 @@ pub fn read_dir(path: impl AsRef) -> anyhow::Result { .with_context(|| format!("failed to read directory '{path}'", path = path.display())) } -pub fn read(path: impl AsRef) -> anyhow::Result> { +pub fn read(path: impl AsRef) -> Option> { let path = path.as_ref(); match fs::read_to_string(path) { - Ok(string) => Ok(Some(string)), + Ok(string) => Some(Ok(string)), - Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(None), + Err(error) if error.kind() == io::ErrorKind::NotFound => None, - Err(error) => { - Err(error).with_context(|| format!("failed to read '{path}", path = path.display())) - } + Err(error) => Some( + Err(error).with_context(|| format!("failed to read '{path}", path = path.display())), + ), } } diff --git a/src/power_supply.rs b/src/power_supply.rs index 4e66bec..f1dcb41 100644 --- a/src/power_supply.rs +++ b/src/power_supply.rs @@ -127,9 +127,10 @@ impl PowerSupply { let type_path = self.path.join("type"); let type_ = fs::read(&type_path) + .with_context(|| format!("'{path}' doesn't exist", path = type_path.display()))? .with_context(|| format!("failed to read '{path}'", path = type_path.display()))?; - type_.ok_or_else(|| anyhow!("'{path}' doesn't exist", path = type_path.display())) + Ok(type_) } pub fn rescan(&mut self) -> anyhow::Result<()> { @@ -206,7 +207,7 @@ impl PowerSupply { pub fn get_available_platform_profiles() -> Vec { let path = "/sys/firmware/acpi/platform_profile_choices"; - let Ok(Some(content)) = fs::read(path) else { + let Some(Ok(content)) = fs::read(path) else { return Vec::new(); };