Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benluiwj authored and ytmimi committed Oct 16, 2024
1 parent 67b473c commit 5fbcc08
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions check_diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down Expand Up @@ -51,6 +52,12 @@ impl From<GitError> for CheckDiffError {
}
}

impl From<FromUtf8Error> for CheckDiffError {
fn from(error: FromUtf8Error) -> Self {
CheckDiffError::FailedUtf8(error.utf8_error())
}
}

pub enum GitError {
FailedClone { stdout: Vec<u8>, stderr: Vec<u8> },
FailedRemoteAdd { stdout: Vec<u8>, stderr: Vec<u8> },
Expand Down Expand Up @@ -168,28 +175,15 @@ pub fn get_ld_lib_path() -> Result<String, CheckDiffError> {
));
};

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<String, CheckDiffError> {
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);
}
Expand All @@ -206,11 +200,8 @@ pub fn get_binary_version(binary: &Path, ld_lib_path: &String) -> Result<String,
)));
};

let Ok(binary_version) = String::from_utf8(command.stdout) else {
return Err(CheckDiffError::FailedUtf8(
"Error converting binary version to string".to_string(),
));
};
let binary_version = String::from_utf8(command.stdout)?;

return Ok(binary_version);
}

Expand Down

0 comments on commit 5fbcc08

Please sign in to comment.