From 137a807b876cfc413f79e20290a5c505cecab0c5 Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 21 Sep 2024 11:54:45 +0800 Subject: [PATCH] improve error handling --- check_diff/src/lib.rs | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/check_diff/src/lib.rs b/check_diff/src/lib.rs index 33cfcf84795..6abf591138f 100644 --- a/check_diff/src/lib.rs +++ b/check_diff/src/lib.rs @@ -2,17 +2,18 @@ use std::env; use std::io; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; +use std::str::Utf8Error; +use std::string::FromUtf8Error; use tracing::info; pub enum CheckDiffError { FailedGit(GitError), FailedCommand(String), - FailedUtf8(String), + FailedUtf8(Utf8Error), FailedSourceBuild(String), FailedCopy(String), FailedCargoVersion(String), FailedVersioning(String), - FailedPathBuf(String), IO(std::io::Error), } @@ -51,6 +52,12 @@ impl From for CheckDiffError { } } +impl From for CheckDiffError { + fn from(error: FromUtf8Error) -> Self { + CheckDiffError::FailedUtf8(error.utf8_error()) + } +} + pub enum GitError { FailedClone { stdout: Vec, stderr: Vec }, FailedRemoteAdd { stdout: Vec, stderr: Vec }, @@ -168,28 +175,15 @@ pub fn get_ld_lib_path() -> Result { )); }; - let Ok(sysroot) = String::from_utf8(command.stdout) else { - return Err(CheckDiffError::FailedUtf8( - "Error converting sysroot to string".to_string(), - )); - }; + let sysroot = String::from_utf8(command.stdout)?; let ld_lib_path = format!("{}/lib", sysroot.trim_end()); return Ok(ld_lib_path); } pub fn get_cargo_version() -> Result { - let Ok(command) = Command::new("cargo").args(["--version"]).output() else { - return Err(CheckDiffError::FailedCargoVersion( - "Failed to obtain cargo version".to_string(), - )); - }; - - let Ok(cargo_version) = String::from_utf8(command.stdout) else { - return Err(CheckDiffError::FailedUtf8( - "Error converting cargo version to string".to_string(), - )); - }; + let command = Command::new("cargo").args(["--version"]).output()?; + let cargo_version = String::from_utf8(command.stdout)?; return Ok(cargo_version); } @@ -206,11 +200,8 @@ pub fn get_binary_version(binary: &Path, ld_lib_path: &String) -> Result