FromStr for BytesPattern

This commit is contained in:
2022-08-27 22:53:46 +03:00
parent 3deb961413
commit 22bdffa92f
2 changed files with 10 additions and 4 deletions

View File

@@ -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)))
}
}