Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up conversion and crossterm errors #1493

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/data_collection/processes/unix/user_table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hashbrown::HashMap;

use crate::utils::error;
use crate::utils::error::{self, BottomError};

#[derive(Debug, Default)]
pub struct UserTable {
Expand All @@ -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());

Expand Down
8 changes: 2 additions & 6 deletions src/data_collection/temperature/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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
Expand Down
18 changes: 0 additions & 18 deletions src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand All @@ -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())
}
}