1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-03 15:03:22 +02:00

flashrom_tester: Refactor Error type

Use a type implementing Error instead of a string for errors. Error
implements Display so can be easily converted to a String. This will
allow libflashrom to be more easily integrated.

BUG=b:230545739
BRANCH=None
TEST=cargo test

Change-Id: Id166053c7edfd07576e7823692cfa0ea4d438948
Signed-off-by: Evan Benn <evanbenn@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/65277
Reviewed-by: Peter Marheine <pmarheine@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
This commit is contained in:
Evan Benn
2022-06-17 14:11:18 +10:00
committed by Edward O'Callaghan
parent 4342cc0f14
commit c42ae261ae
3 changed files with 46 additions and 21 deletions

View File

@ -86,10 +86,7 @@ fn flashrom_extract_size(stdout: &str) -> Result<i64, FlashromError> {
{
None => return Err("Found no purely-numeric lines in flashrom output".into()),
Some(Err(e)) => {
return Err(format!(
"Failed to parse flashrom size output as integer: {}",
e
))
return Err(format!("Failed to parse flashrom size output as integer: {}", e).into())
}
Some(Ok(sz)) => Ok(sz),
}
@ -246,7 +243,7 @@ impl crate::Flashrom for FlashromCmd {
info!("Successfully {}abled write-protect", status);
Ok(true)
}
Err(e) => Err(format!("Cannot {}able write-protect: {}", status, e)),
Err(e) => Err(format!("Cannot {}able write-protect: {}", status, e).into()),
}
}
@ -406,7 +403,7 @@ fn flashrom_dispatch<S: AsRef<str>>(
let output = match Command::new(path).args(&args).output() {
Ok(x) => x,
Err(e) => return Err(format!("Failed to run flashrom: {}", e)),
Err(e) => return Err(format!("Failed to run flashrom: {}", e).into()),
};
if !output.status.success() {
// There is two cases on failure;
@ -418,7 +415,8 @@ fn flashrom_dispatch<S: AsRef<str>>(
"{}\nExited with error code: {}",
String::from_utf8_lossy(&output.stderr),
code
));
)
.into());
}
None => return Err("Process terminated by a signal".into()),
}
@ -444,7 +442,7 @@ pub fn dut_ctrl_servo_type() -> Result<(Vec<u8>, Vec<u8>), FlashromError> {
fn dut_ctrl(args: &[&str]) -> Result<(Vec<u8>, Vec<u8>), FlashromError> {
let output = match Command::new("dut-control").args(args).output() {
Ok(x) => x,
Err(e) => return Err(format!("Failed to run dut-control: {}", e)),
Err(e) => return Err(format!("Failed to run dut-control: {}", e).into()),
};
if !output.status.success() {
// There is two cases on failure;

View File

@ -38,6 +38,8 @@ extern crate log;
mod cmd;
use std::{error, fmt};
pub use cmd::{dut_ctrl_toggle_wp, FlashromCmd};
#[derive(Copy, Clone, PartialEq, Debug)]
@ -81,7 +83,27 @@ impl FlashChip {
}
}
pub type FlashromError = String;
#[derive(Debug, PartialEq)]
pub struct FlashromError {
msg: String,
}
impl fmt::Display for FlashromError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
impl error::Error for FlashromError {}
impl<T> From<T> for FlashromError
where
T: Into<String>,
{
fn from(msg: T) -> Self {
FlashromError { msg: msg.into() }
}
}
pub struct ROMWriteSpecifics<'a> {
pub layout_file: Option<&'a str>,