mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-24 21:23:08 +00:00
refactor: clean up conversion and crossterm errors (#1493)
* refactor: clean up conversion errors * refactor: remove crossterm error
This commit is contained in:
parent
571a801bf8
commit
d97d75f797
3 changed files with 6 additions and 29 deletions
|
@ -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());
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue