Skip to content

Commit

Permalink
Replace .is_err with match statements
Browse files Browse the repository at this point in the history
Add a todo section
  • Loading branch information
dormant-user committed Sep 28, 2024
1 parent 1b8af06 commit 1f3c75f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
24 changes: 10 additions & 14 deletions src/legacy/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ use crate::squire;
/// # Returns
///
/// A `Option` containing the processor information if successful, otherwise `None`.
fn get_processor_info_darwin(lib_path: &str) -> Result<String, &'static str> {
let result = squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"], true);
if result.is_err() {
return Err("Failed to get processor info");
}
Ok(result.unwrap())
fn get_processor_info_darwin(lib_path: &str) -> Result<String, String> {
squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"], true)
}

/// Function to get processor information on Linux.
Expand All @@ -29,10 +25,10 @@ fn get_processor_info_darwin(lib_path: &str) -> Result<String, &'static str> {
/// # Returns
///
/// A `Option` containing the processor information if successful, otherwise `None`.
fn get_processor_info_linux(lib_path: &str) -> Result<String, &'static str> {
fn get_processor_info_linux(lib_path: &str) -> Result<String, String> {
let file = match File::open(lib_path) {
Ok(file) => file,
Err(_) => return Err("Failed to open file"),
Err(_) => return Err(format!("Failed to open '{}'", lib_path)),
};
for line in io::BufReader::new(file).lines() {
match line {
Expand All @@ -44,10 +40,10 @@ fn get_processor_info_linux(lib_path: &str) -> Result<String, &'static str> {
}
}
}
Err(_) => return Err("Error reading line"),
Err(_) => return Err(format!("Error reading lines in '{}'", lib_path)),
}
}
Err("Model name not found")
Err(format!("Model name not found in '{}'", lib_path))
}

/// Function to get processor information on Windows.
Expand All @@ -59,17 +55,17 @@ fn get_processor_info_linux(lib_path: &str) -> Result<String, &'static str> {
/// # Returns
///
/// A `Option` containing the processor information if successful, otherwise `None`.
fn get_processor_info_windows(lib_path: &str) -> Result<String, &'static str> {
fn get_processor_info_windows(lib_path: &str) -> Result<String, String> {
let result = squire::util::run_command(lib_path, &["cpu", "get", "name"], true);
let output = match result {
Ok(output) => output,
Err(_) => return Err("Failed to get processor info"),
Err(_) => return Err("Failed to get processor info".to_string()),
};
let lines: Vec<&str> = output.trim().split('\n').collect();
if lines.len() > 1 {
Ok(lines[1].trim().to_string())
} else {
Err("Invalid output from command")
Err("Invalid output from command".to_string())
}
}

Expand All @@ -86,7 +82,7 @@ pub fn get_name() -> Option<String> {
"windows" => get_processor_info_windows("C:\\Windows\\System32\\wbem\\wmic.exe"),
_ => {
log::error!("Unsupported operating system: {}", operating_system);
Err("Unsupported operating system")
Err("Unsupported operating system".to_string())
}
};
match result {
Expand Down
7 changes: 4 additions & 3 deletions src/legacy/disks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ fn darwin_disks(lib_path: &str) -> Vec<HashMap<String, String>> {
.split(":")
.nth(1)
.unwrap_or_default()
.trim().to_string()
.trim()
.to_string()
);
}
if info_line.contains("Disk Size:") {
Expand Down Expand Up @@ -200,9 +201,9 @@ fn reformat_windows(data: &mut HashMap<String, Value>) -> HashMap<String, String
reformatted_data.insert(
"DeviceID".to_string(),
data.get("DeviceID")
.unwrap()
.unwrap_or(&Value::String("".to_string()))
.as_str()
.unwrap()
.unwrap_or_default()
.to_string()
);
reformatted_data
Expand Down
9 changes: 5 additions & 4 deletions src/legacy/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ fn get_gpu_info_linux(lib_path: &str) -> Vec<HashMap<String, String>> {
&[],
true,
);
if result.is_err() {
return Vec::new();
}
let output = match result {
Ok(output) => output,
Err(_) => return Vec::new(),
};
let mut gpu_info = Vec::new();
for line in result.unwrap().lines() {
for line in output.lines() {
if line.contains("VGA") {
let gpu = line.split(':').last().unwrap().trim();
let mut info = HashMap::new();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod legacy;
/// ```
pub async fn start() -> io::Result<()> {
let metadata = constant::build_info();
// todo: Move all commandline process to config - so it configurable via .env files
let config = squire::startup::get_config(&metadata);

squire::startup::init_logger(config.debug, config.utc_logging, &metadata.crate_name);
Expand Down

0 comments on commit 1f3c75f

Please sign in to comment.