From cee2fd07c0edc73735d0a23d935d29777dc55dd2 Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Sat, 18 Sep 2021 18:13:19 +0900 Subject: [PATCH] Use openblas_build::error::Error in FromStr of Target #78 --- openblas-build/src/build.rs | 28 ++++++++++++++++------------ openblas-build/src/error.rs | 3 +++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/openblas-build/src/build.rs b/openblas-build/src/build.rs index 895a83d..58ac95a 100644 --- a/openblas-build/src/build.rs +++ b/openblas-build/src/build.rs @@ -2,7 +2,7 @@ use crate::{check::*, error::*}; use std::{ - fmt, fs, + fs, os::unix::io::*, path::*, process::{Command, Stdio}, @@ -118,7 +118,7 @@ pub enum Target { } impl FromStr for Target { - type Err = ParseTargetError; + type Err = Error; fn from_str(s: &str) -> Result { let target = match s.to_ascii_lowercase().as_str() { @@ -195,21 +195,16 @@ impl FromStr for Target { "zarch_generic" => Self::ZARCH_GENERIC, "z13" => Self::Z13, "z14" => Self::Z14, - _ => return Err(ParseTargetError::default()), + _ => { + return Err(Error::UnsupportedTarget { + target: s.to_string(), + }) + } }; Ok(target) } } -#[derive(Debug, Clone, PartialEq, Eq, Default)] -pub struct ParseTargetError; - -impl fmt::Display for ParseTargetError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - "provided string was not a valid target".fmt(f) - } -} - /// make option generator #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Configure { @@ -408,6 +403,15 @@ impl Configure { mod tests { use super::*; + #[test] + fn target_from_str() { + assert_eq!(Target::from_str("p2").unwrap(), Target::P2); + assert!(matches!( + Target::from_str("p3").unwrap_err(), + crate::error::Error::UnsupportedTarget { .. } + )); + } + #[ignore] #[test] fn build_default() { diff --git a/openblas-build/src/error.rs b/openblas-build/src/error.rs index 5823152..e49e9e8 100644 --- a/openblas-build/src/error.rs +++ b/openblas-build/src/error.rs @@ -21,6 +21,9 @@ pub enum Error { #[error("Library file does not exist: {}", path.display())] LibraryNotExist { path: PathBuf }, + #[error("Target {} is unsupported", target)] + UnsupportedTarget { target: String }, + #[error("Other IO errors: {0:?}")] IOError(#[from] io::Error), }