refactor: clean up conversion and crossterm errors (#1493)

* refactor: clean up conversion errors

* refactor: remove crossterm error
This commit is contained in:
Clement Tsang 2024-07-14 04:01:44 -04:00 committed by GitHub
parent 571a801bf8
commit d97d75f797
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 6 additions and 29 deletions

View file

@ -1,6 +1,6 @@
use hashbrown::HashMap;
use crate::utils::error;
use crate::utils::error::{self, BottomError};
#[derive(Debug, Default)]
pub struct UserTable {
@ -17,13 +17,12 @@ impl UserTable {
let passwd = unsafe { libc::getpwuid(uid) };
if passwd.is_null() {
Err(error::BottomError::GenericError(
"passwd is inaccessible".into(),
))
Err(BottomError::GenericError("passwd is inaccessible".into()))
} else {
// SAFETY: We return early if passwd is null.
let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) }
.to_str()?
.to_str()
.map_err(|err| BottomError::GenericError(err.to_string()))?
.to_string();
self.uid_user_mapping.insert(uid, username.clone());

View file

@ -9,7 +9,7 @@ use anyhow::Result;
use hashbrown::{HashMap, HashSet};
use super::{is_temp_filtered, TempHarvest, TemperatureType};
use crate::{app::filter::Filter, utils::error::BottomError};
use crate::app::filter::Filter;
const EMPTY_NAME: &str = "Unknown";
@ -23,11 +23,7 @@ struct HwmonResults {
/// Parses and reads temperatures that were in millidegree Celsius, and if
/// successful, returns a temperature in Celsius.
fn parse_temp(path: &Path) -> Result<f32> {
Ok(fs::read_to_string(path)?
.trim_end()
.parse::<f32>()
.map_err(|e| BottomError::ConversionError(e.to_string()))?
/ 1_000.0)
Ok(fs::read_to_string(path)?.trim_end().parse::<f32>()? / 1_000.0)
}
/// Get all candidates from hwmon and coretemp. It will also return the number

View file

@ -11,9 +11,6 @@ pub enum BottomError {
/// An error when there is an IO exception.
#[error("IO exception, {0}")]
InvalidIo(String),
/// An error when the Crossterm library encounters a problem.
#[error("Error caused by Crossterm, {0}")]
CrosstermError(String),
/// An error to represent generic errors.
#[error("Error, {0}")]
GenericError(String),
@ -23,9 +20,6 @@ pub enum BottomError {
/// An error to represent errors with the config.
#[error("Configuration file error, {0}")]
ConfigError(String),
/// An error to represent errors with converting between data types.
#[error("Conversion error, {0}")]
ConversionError(String),
}
impl From<std::io::Error> for BottomError {
@ -51,15 +45,3 @@ impl From<toml_edit::de::Error> for BottomError {
BottomError::ConfigError(err.to_string())
}
}
impl From<std::str::Utf8Error> for BottomError {
fn from(err: std::str::Utf8Error) -> Self {
BottomError::ConversionError(err.to_string())
}
}
impl From<std::string::FromUtf8Error> for BottomError {
fn from(err: std::string::FromUtf8Error) -> Self {
BottomError::ConversionError(err.to_string())
}
}