Skip to content

Commit

Permalink
Use openblas_build::error::Error in FromStr of Target #78
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Sep 19, 2021
1 parent 677ff02 commit cee2fd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
28 changes: 16 additions & 12 deletions openblas-build/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{check::*, error::*};
use std::{
fmt, fs,
fs,
os::unix::io::*,
path::*,
process::{Command, Stdio},
Expand Down Expand Up @@ -118,7 +118,7 @@ pub enum Target {
}

impl FromStr for Target {
type Err = ParseTargetError;
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let target = match s.to_ascii_lowercase().as_str() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions openblas-build/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down

0 comments on commit cee2fd0

Please sign in to comment.