diff --git a/lib/examples/patchsomedll.rs b/lib/examples/patchsomedll.rs index 9c7cd58..2f174fe 100644 --- a/lib/examples/patchsomedll.rs +++ b/lib/examples/patchsomedll.rs @@ -19,7 +19,7 @@ fn main() { let replacement = binnvecs!("06 5A 18 74 2D 62 12 6A 13 4A 2B 0E 6F 0F 36 7A 28 0A 37 67 0A 4B 01 73 14"); - let pattern = BytesPattern::from(src_pattern); + let pattern = src_pattern.parse().unwrap(); let file = File::open(src_file).expect("src open"); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 839cd2e..70c81b7 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -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, @@ -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 { + if str_val.is_empty() { + return Err("string is empty"); + } + let mut elements: Vec<&str> = str_val.split(' ').collect(); let mask: Vec = 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))) } }