FromStr for BytesPattern
This commit is contained in:
@@ -2,6 +2,7 @@ extern crate data_encoding;
|
||||
|
||||
use data_encoding::HEXUPPER;
|
||||
use std::ops::Deref;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct BytesPattern {
|
||||
bytes: Vec<u8>,
|
||||
@@ -46,8 +47,13 @@ impl BytesPattern {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for BytesPattern {
|
||||
fn from(str_val: &str) -> Self {
|
||||
impl FromStr for BytesPattern {
|
||||
type Err = &'static str;
|
||||
fn from_str(str_val: &str) -> Result<Self, Self::Err> {
|
||||
if str_val.is_empty() {
|
||||
return Err("string is empty");
|
||||
}
|
||||
|
||||
let mut elements: Vec<&str> = str_val.split(' ').collect();
|
||||
let mask: Vec<bool> = elements.iter().map(|item| *item == "??").collect();
|
||||
|
||||
@@ -61,7 +67,7 @@ impl From<&str> for BytesPattern {
|
||||
.decode(elements.join("").as_bytes())
|
||||
.expect("decode pattern");
|
||||
|
||||
Self::from((bytes, mask))
|
||||
Ok(Self::from((bytes, mask)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user