Skip to content

Commit

Permalink
chore(rust-sdk): add the checking code for NNPreload::from_str
Browse files Browse the repository at this point in the history
Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss committed Oct 26, 2023
1 parent 74a49e1 commit 444e8d9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ impl std::str::FromStr for NNPreload {
type Err = WasmEdgeError;

fn from_str(preload: &str) -> std::result::Result<Self, Self::Err> {
let preload: Vec<&str> = preload.split(':').collect();
let nn_preload: Vec<&str> = preload.split(':').collect();
if nn_preload.len() != 4 {
return Err(WasmEdgeError::Operation(format!(
"Failed to convert to NNPreload value. Invalid preload string: {}. The correct format is: 'alias:backend:target:path'",
preload
)));
}
let (alias, backend, target, path) = (
preload[0].to_string(),
preload[1]
nn_preload[0].to_string(),
nn_preload[1]
.parse::<GraphEncoding>()
.map_err(|err| WasmEdgeError::Operation(err.to_string()))?,
preload[2]
nn_preload[2]
.parse::<ExecutionTarget>()
.map_err(|err| WasmEdgeError::Operation(err.to_string()))?,
std::path::PathBuf::from(preload[3]),
std::path::PathBuf::from(nn_preload[3]),
);

Ok(Self::new(alias, backend, target, path))
Expand Down Expand Up @@ -136,6 +142,19 @@ fn test_generate_nnpreload_from_str() {
),
err
);

// invalid preload string: invalid format
let preload = "default:GGML:CPU";
let result = NNPreload::from_str(preload);
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(
WasmEdgeError::Operation(
"Failed to convert to NNPreload value. Invalid preload string: default:GGML:CPU. The correct format is: 'alias:backend:target:path'"
.to_string()
),
err
);
}

/// Describes the encoding of the graph.
Expand Down

0 comments on commit 444e8d9

Please sign in to comment.