From 22bdffa92f0071094250c5d392f8e36268033649 Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Sat, 27 Aug 2022 22:53:46 +0300
Subject: [PATCH] FromStr for BytesPattern

---
 lib/examples/patchsomedll.rs |  2 +-
 lib/src/lib.rs               | 12 +++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

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