Skip to content

Commit

Permalink
refactor: added logs
Browse files Browse the repository at this point in the history
  • Loading branch information
m62624 committed Aug 31, 2023
1 parent de94b44 commit 2fcc19f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 17 deletions.
70 changes: 56 additions & 14 deletions flexible_inspect_rs/src/rules/rule_bytes/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ pub trait FromBytes<T: FromStr + Copy + Debug + ToString> {
impl FromBytes<i8> for i8 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i8> {
if bytes.len() == I8_LEN {
info!("conversion is possible for `i8`, value {:?}", bytes);
let mut array_bytes: [u8; I8_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i8::from_be_bytes(array_bytes));
let result = i8::from_be_bytes(array_bytes);
info!("conversion result `i8`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -80,9 +83,12 @@ impl FromBytes<i8> for i8 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i8> {
if bytes.len() == I8_LEN {
info!("conversion is possible for `i8`, value {:?}", bytes);
let mut array_bytes: [u8; I8_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i8::from_le_bytes(array_bytes));
let result = i8::from_le_bytes(array_bytes);
info!("conversion result `i8`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -101,9 +107,12 @@ impl FromBytes<i8> for i8 {
impl FromBytes<i16> for i16 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i16> {
if bytes.len() == I16_LEN {
info!("conversion is possible for `i16`, value {:?}", bytes);
let mut array_bytes: [u8; I16_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i16::from_be_bytes(array_bytes));
let result = i16::from_be_bytes(array_bytes);
info!("conversion result `i16`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -120,9 +129,12 @@ impl FromBytes<i16> for i16 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i16> {
if bytes.len() == I16_LEN {
info!("conversion is possible for `i16`, value {:?}", bytes);
let mut array_bytes: [u8; I16_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i16::from_le_bytes(array_bytes));
let result = i16::from_le_bytes(array_bytes);
info!("conversion result `i16`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -141,9 +153,12 @@ impl FromBytes<i16> for i16 {
impl FromBytes<i32> for i32 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i32> {
if bytes.len() == I32_LEN {
info!("conversion is possible for `i32`, value {:?}", bytes);
let mut array_bytes: [u8; I32_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i32::from_be_bytes(array_bytes));
let result = i32::from_be_bytes(array_bytes);
info!("conversion result `i32`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -160,9 +175,12 @@ impl FromBytes<i32> for i32 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i32> {
if bytes.len() == I32_LEN {
info!("conversion is possible for `i32`, value {:?}", bytes);
let mut array_bytes: [u8; I32_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i32::from_le_bytes(array_bytes));
let result = i32::from_le_bytes(array_bytes);
info!("conversion result `i32`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -181,9 +199,12 @@ impl FromBytes<i32> for i32 {
impl FromBytes<i64> for i64 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i64> {
if bytes.len() == I64_LEN {
info!("conversion is possible for `i64`, value {:?}", bytes);
let mut array_bytes: [u8; I64_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i64::from_be_bytes(array_bytes));
let result = i64::from_be_bytes(array_bytes);
info!("conversion result `i64`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -200,9 +221,12 @@ impl FromBytes<i64> for i64 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i64> {
if bytes.len() == I64_LEN {
info!("conversion is possible for `i64`, value {:?}", bytes);
let mut array_bytes: [u8; I64_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i64::from_le_bytes(array_bytes));
let result = i64::from_le_bytes(array_bytes);
info!("conversion result `i64`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -221,9 +245,12 @@ impl FromBytes<i64> for i64 {
impl FromBytes<i128> for i128 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i128> {
if bytes.len() == I128_LEN {
info!("conversion is possible for `i128`, value {:?}", bytes);
let mut array_bytes: [u8; I128_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i128::from_be_bytes(array_bytes));
let result = i128::from_be_bytes(array_bytes);
info!("conversion result `i128`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -240,9 +267,12 @@ impl FromBytes<i128> for i128 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i128> {
if bytes.len() == I128_LEN {
info!("conversion is possible for `i128`, value {:?}", bytes);
let mut array_bytes: [u8; I128_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(i128::from_le_bytes(array_bytes));
let result = i128::from_le_bytes(array_bytes);
info!("conversion result `i128`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -261,9 +291,12 @@ impl FromBytes<i128> for i128 {
impl FromBytes<f32> for f32 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<f32> {
if bytes.len() == F32_LEN {
info!("conversion is possible for `f32`, value {:?}", bytes);
let mut array_bytes: [u8; F32_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(f32::from_be_bytes(array_bytes));
let result = f32::from_be_bytes(array_bytes);
info!("conversion result `f32`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -280,9 +313,12 @@ impl FromBytes<f32> for f32 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<f32> {
if bytes.len() == F32_LEN {
info!("conversion is possible for `f32`, value {:?}", bytes);
let mut array_bytes: [u8; F32_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(f32::from_le_bytes(array_bytes));
let result = f32::from_le_bytes(array_bytes);
info!("conversion result `f32`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -301,9 +337,12 @@ impl FromBytes<f32> for f32 {
impl FromBytes<f64> for f64 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<f64> {
if bytes.len() == F64_LEN {
info!("conversion is possible for `f64`, value {:?}", bytes);
let mut array_bytes: [u8; F64_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(f64::from_be_bytes(array_bytes));
let result = f64::from_be_bytes(array_bytes);
info!("conversion result `f64`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand All @@ -320,9 +359,12 @@ impl FromBytes<f64> for f64 {

fn from_le_bytes_non_const(bytes: &[u8]) -> Option<f64> {
if bytes.len() == F64_LEN {
info!("conversion is possible for `f64`, value {:?}", bytes);
let mut array_bytes: [u8; F64_LEN] = Default::default();
array_bytes.copy_from_slice(bytes);
return Some(f64::from_le_bytes(array_bytes));
let result = f64::from_le_bytes(array_bytes);
info!("conversion result `f64`, value {:?}", result);
return Some(result);
} else {
warn!(
"{}",
Expand Down
68 changes: 65 additions & 3 deletions flexible_inspect_rs/src/rules/rule_bytes/runner_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{
str::FromStr,
};

use log::info;

use super::rules::{next::NextStep, traits::IntoSpecificCaptureType};

use super::{convert::FromBytes, *};
Expand All @@ -26,12 +28,32 @@ fn single_range_bytes_check<
.filter(|&num| {
let num = match read_mode {
ReadMode::FromBeBytes => {
info!(
"(range mode {}) mode {} for data {:?}",
"`any`".yellow(),
"`be bytes`".yellow(),
num
);
T::from_be_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromLeBytes => {
info!(
"(range mode {}) mode {} for data {:?}",
"`any`".yellow(),
"`le bytes`".yellow(),
num
);
T::from_le_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromUtf8 => T::from_utf8(num.as_bytes().unwrap()),
ReadMode::FromUtf8 => {
info!(
"(range mode {}) mode {} for data {:?}",
"`any`".yellow(),
"`utf8`".yellow(),
num
);
T::from_utf8(num.as_bytes().unwrap())
}
};
num.map(|num| range.contains(&num)).unwrap_or(false)
})
Expand All @@ -45,12 +67,32 @@ fn single_range_bytes_check<
.filter(|&num| {
let num = match read_mode {
ReadMode::FromBeBytes => {
info!(
"(range mode {}) mode {} for data {:?}",
"`all`".yellow(),
"`be bytes`".yellow(),
num
);
T::from_be_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromLeBytes => {
info!(
"(range mode {}) mode {} for data {:?}",
"`all`".yellow(),
"`le bytes`".yellow(),
num
);
T::from_le_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromUtf8 => T::from_utf8(num.as_bytes().unwrap()),
ReadMode::FromUtf8 => {
info!(
"(range mode {}) mode {} for data {:?}",
"`all`".yellow(),
"`utf8`".yellow(),
num
);
T::from_utf8(num.as_bytes().unwrap())
}
};
num.map(|num| range.contains(&num)).unwrap_or(false)
})
Expand All @@ -65,12 +107,32 @@ fn single_range_bytes_check<
.filter(|&num| {
let num = match read_mode {
ReadMode::FromBeBytes => {
info!(
"(range mode {}) mode {} for data {:?}",
"`exactly`".yellow(),
"`be bytes`".yellow(),
num
);
T::from_be_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromLeBytes => {
info!(
"(range mode {}) mode {} for data {:?}",
"`exactly`".yellow(),
"`le bytes`".yellow(),
num
);
T::from_le_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromUtf8 => T::from_utf8(num.as_bytes().unwrap()),
ReadMode::FromUtf8 => {
info!(
"(range mode {}) mode {} for data {:?}",
"`exactly`".yellow(),
"`utf8`".yellow(),
num
);
T::from_utf8(num.as_bytes().unwrap())
}
};
num.map(|num| range.contains(&num)).unwrap_or(false)
})
Expand Down
17 changes: 17 additions & 0 deletions flexible_inspect_rs/src/rules/rule_str/runner_range.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use log::info;

use super::rules::{next::NextStep, traits::IntoSpecificCaptureType};
use super::{convert::convert_and_filter, *};
use std::{
Expand All @@ -17,6 +19,11 @@ fn single_range_str_check<
) -> bool {
match range_mode {
RangeMode::Any => {
info!(
"(range mode {}) for data {:?}",
"`any`".yellow(),
captures.text_for_capture
);
captures
.text_for_capture
.iter()
Expand All @@ -29,6 +36,11 @@ fn single_range_str_check<
> 0
}
RangeMode::All => {
info!(
"(range mode {}) for data {:?}",
"`all`".yellow(),
captures.text_for_capture
);
captures
.text_for_capture
.iter()
Expand All @@ -41,6 +53,11 @@ fn single_range_str_check<
== captures.text_for_capture.len()
}
RangeMode::Exactly(target_count) => {
info!(
"(range mode {}) for data {:?}",
"`exactly`".yellow(),
captures.text_for_capture
);
let required_count = target_count.min(captures.text_for_capture.len());
captures
.text_for_capture
Expand Down

0 comments on commit 2fcc19f

Please sign in to comment.