Skip to content

Commit

Permalink
refactor: improved code, removed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
m62624 committed Aug 29, 2023
1 parent 3542e92 commit 791da71
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 72 deletions.
16 changes: 8 additions & 8 deletions flexible_inspect_rs/src/rules/common_elements/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::marker::PhantomData;

use self::range::RangeFormat;
use super::{traits::IntoConcreteType, *};
use super::{traits::IntoSpecificCaptureType, *};
pub mod range;
// =======================================================
/// This is reserved standard value for error filling
Expand Down Expand Up @@ -81,29 +81,29 @@ pub enum Counter {

/// A structure that stores all the data for processing the capture
#[derive(Debug)]
pub struct CaptureData<'a, T: IntoConcreteType<'a>> {
pub struct CaptureData<'a, T: IntoSpecificCaptureType<'a>> {
pub text_for_capture: IndexSet<T>,
pub hashmap_for_error: HashMap<String, String>,
pub counter_value: usize,
pub phantom: PhantomData<&'a T>,
}

impl<'a> IntoConcreteType<'a> for &'a str {
fn into_str(&self) -> Option<&'a str> {
impl<'a> IntoSpecificCaptureType<'a> for &'a str {
fn as_str(&self) -> Option<&'a str> {
Some(self)
}

fn into_bytes(&self) -> Option<&'a [u8]> {
fn as_bytes(&self) -> Option<&'a [u8]> {
None
}
}

impl<'a> IntoConcreteType<'a> for &'a [u8] {
fn into_str(&self) -> Option<&'a str> {
impl<'a> IntoSpecificCaptureType<'a> for &'a [u8] {
fn as_str(&self) -> Option<&'a str> {
None
}

fn into_bytes(&self) -> Option<&'a [u8]> {
fn as_bytes(&self) -> Option<&'a [u8]> {
Some(self)
}
}
4 changes: 2 additions & 2 deletions flexible_inspect_rs/src/rules/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{rule_str::RegexRaw, traits::IntoConcreteType, *};
use super::{rule_str::RegexRaw, traits::IntoSpecificCaptureType, *};
use crate::prelude::Rule;

impl GeneralModifiers {
Expand Down Expand Up @@ -39,7 +39,7 @@ impl SlisedRules {
}
}

impl<'a, T: IntoConcreteType<'a>> CaptureData<'a, T> {
impl<'a, T: IntoSpecificCaptureType<'a>> CaptureData<'a, T> {
pub fn is_some(&self) -> bool {
!self.text_for_capture.is_empty()
}
Expand Down
4 changes: 2 additions & 2 deletions flexible_inspect_rs/src/rules/next/counter_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use log::error;

use super::*;
use crate::rules::{
traits::{IntoConcreteType, RuleBase},
traits::{IntoSpecificCaptureType, RuleBase},
Counter,
};

/// The counter implementation
impl Counter {
pub fn counter_status<'a, R: RuleBase, C: IntoConcreteType<'a>>(
pub fn counter_status<'a, R: RuleBase, C: IntoSpecificCaptureType<'a>>(
rule: &R,
captures: &mut CaptureData<'a, C>,
) -> NextStep {
Expand Down
4 changes: 2 additions & 2 deletions flexible_inspect_rs/src/rules/next/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod counter_status;
mod modifier_arena;
pub mod number_range_status;
use super::{
traits::{IntoConcreteType, RuleBase},
traits::{IntoSpecificCaptureType, RuleBase},
*,
};

Expand All @@ -19,7 +19,7 @@ pub enum NextStep {

impl NextStep {
/// Mechanism with final variant, depending on modifiers we get the result
pub fn next_or_finish_or_error<'a, R: RuleBase, C: IntoConcreteType<'a>>(
pub fn next_or_finish_or_error<'a, R: RuleBase, C: IntoSpecificCaptureType<'a>>(
rule: &R,
captures: &mut CaptureData<'a, C>,
) -> NextStep {
Expand Down
2 changes: 1 addition & 1 deletion flexible_inspect_rs/src/rules/next/modifier_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;
/// Additional checks of modifiers are performed here
/// ( if there will be more of them and they will take a long time to calculate,
/// we will switch to checking via `async_task` for each modifier )
pub fn modifier_runner<'a, R: RuleBase, C: IntoConcreteType<'a>>(
pub fn modifier_runner<'a, R: RuleBase, C: IntoSpecificCaptureType<'a>>(
rule: &R,
captures: &mut CaptureData<'a, C>,
) -> NextStep {
Expand Down
2 changes: 1 addition & 1 deletion flexible_inspect_rs/src/rules/next/number_range_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::rules::rule_bytes::runner_range::single_bytes_result;
use crate::rules::rule_str::runner_range::single_str_result;

pub fn number_range_status<'a, R: RuleBase, C: IntoConcreteType<'a>>(
pub fn number_range_status<'a, R: RuleBase, C: IntoSpecificCaptureType<'a>>(
rule: &R,
captures: &mut CaptureData<'a, C>,
) -> NextStep {
Expand Down
28 changes: 14 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 @@ -62,7 +62,7 @@ impl FromBytes<i8> for i8 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i8> {
if bytes.len() == I8_LEN {
let mut array_bytes: [u8; I8_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i8::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -81,7 +81,7 @@ impl FromBytes<i8> for i8 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i8> {
if bytes.len() == I8_LEN {
let mut array_bytes: [u8; I8_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i8::from_le_bytes(array_bytes));
} else {
warn!(
Expand All @@ -102,7 +102,7 @@ impl FromBytes<i16> for i16 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i16> {
if bytes.len() == I16_LEN {
let mut array_bytes: [u8; I16_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i16::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -121,7 +121,7 @@ impl FromBytes<i16> for i16 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i16> {
if bytes.len() == I16_LEN {
let mut array_bytes: [u8; I16_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i16::from_le_bytes(array_bytes));
} else {
warn!(
Expand All @@ -142,7 +142,7 @@ impl FromBytes<i32> for i32 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i32> {
if bytes.len() == I32_LEN {
let mut array_bytes: [u8; I32_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i32::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -161,7 +161,7 @@ impl FromBytes<i32> for i32 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i32> {
if bytes.len() == I32_LEN {
let mut array_bytes: [u8; I32_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i32::from_le_bytes(array_bytes));
} else {
warn!(
Expand All @@ -182,7 +182,7 @@ impl FromBytes<i64> for i64 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i64> {
if bytes.len() == I64_LEN {
let mut array_bytes: [u8; I64_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i64::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -201,7 +201,7 @@ impl FromBytes<i64> for i64 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i64> {
if bytes.len() == I64_LEN {
let mut array_bytes: [u8; I64_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i64::from_le_bytes(array_bytes));
} else {
warn!(
Expand All @@ -222,7 +222,7 @@ impl FromBytes<i128> for i128 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<i128> {
if bytes.len() == I128_LEN {
let mut array_bytes: [u8; I128_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i128::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -241,7 +241,7 @@ impl FromBytes<i128> for i128 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<i128> {
if bytes.len() == I128_LEN {
let mut array_bytes: [u8; I128_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(i128::from_le_bytes(array_bytes));
} else {
warn!(
Expand All @@ -262,7 +262,7 @@ impl FromBytes<f32> for f32 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<f32> {
if bytes.len() == F32_LEN {
let mut array_bytes: [u8; F32_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(f32::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -281,7 +281,7 @@ impl FromBytes<f32> for f32 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<f32> {
if bytes.len() == F32_LEN {
let mut array_bytes: [u8; F32_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(f32::from_le_bytes(array_bytes));
} else {
warn!(
Expand All @@ -302,7 +302,7 @@ impl FromBytes<f64> for f64 {
fn from_be_bytes_non_const(bytes: &[u8]) -> Option<f64> {
if bytes.len() == F64_LEN {
let mut array_bytes: [u8; F64_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(f64::from_be_bytes(array_bytes));
} else {
warn!(
Expand All @@ -321,7 +321,7 @@ impl FromBytes<f64> for f64 {
fn from_le_bytes_non_const(bytes: &[u8]) -> Option<f64> {
if bytes.len() == F64_LEN {
let mut array_bytes: [u8; F64_LEN] = Default::default();
array_bytes.copy_from_slice(&bytes);
array_bytes.copy_from_slice(bytes);
return Some(f64::from_le_bytes(array_bytes));
} else {
warn!(
Expand Down
28 changes: 14 additions & 14 deletions flexible_inspect_rs/src/rules/rule_bytes/runner_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::{
str::FromStr,
};

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

use super::{convert::FromBytes, *};

fn single_range_bytes_check<
'a,
T: FromStr + Copy + Debug + Display + PartialOrd + FromBytes<T>,
C: IntoConcreteType<'a>,
C: IntoSpecificCaptureType<'a>,
>(
numbers: &mut CaptureData<'a, C>,
range: &RangeInclusive<T>,
Expand All @@ -26,12 +26,12 @@ fn single_range_bytes_check<
.filter(|&num| {
let num = match read_mode {
ReadMode::FromBeBytes => {
T::from_be_bytes_non_const(num.into_bytes().unwrap())
T::from_be_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromLeBytes => {
T::from_le_bytes_non_const(num.into_bytes().unwrap())
T::from_le_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromUtf8 => T::from_utf8(num.into_bytes().unwrap()),
ReadMode::FromUtf8 => T::from_utf8(num.as_bytes().unwrap()),
};
num.map(|num| range.contains(&num)).unwrap_or(false)
})
Expand All @@ -45,12 +45,12 @@ fn single_range_bytes_check<
.filter(|&num| {
let num = match read_mode {
ReadMode::FromBeBytes => {
T::from_be_bytes_non_const(num.into_bytes().unwrap())
T::from_be_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromLeBytes => {
T::from_le_bytes_non_const(num.into_bytes().unwrap())
T::from_le_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromUtf8 => T::from_utf8(num.into_bytes().unwrap()),
ReadMode::FromUtf8 => T::from_utf8(num.as_bytes().unwrap()),
};
num.map(|num| range.contains(&num)).unwrap_or(false)
})
Expand All @@ -65,12 +65,12 @@ fn single_range_bytes_check<
.filter(|&num| {
let num = match read_mode {
ReadMode::FromBeBytes => {
T::from_be_bytes_non_const(num.into_bytes().unwrap())
T::from_be_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromLeBytes => {
T::from_le_bytes_non_const(num.into_bytes().unwrap())
T::from_le_bytes_non_const(num.as_bytes().unwrap())
}
ReadMode::FromUtf8 => T::from_utf8(num.into_bytes().unwrap()),
ReadMode::FromUtf8 => T::from_utf8(num.as_bytes().unwrap()),
};
num.map(|num| range.contains(&num)).unwrap_or(false)
})
Expand All @@ -84,7 +84,7 @@ fn single_range_bytes_check<
pub fn single_bytes_result<
'a,
T: Debug + FromStr + Copy + Display + PartialOrd + FromBytes<T>,
C: IntoConcreteType<'a>,
C: IntoSpecificCaptureType<'a>,
>(
range_bytes: &RangeBytes,
captures: &mut CaptureData<'a, C>,
Expand All @@ -96,8 +96,8 @@ pub fn single_bytes_result<
range_bytes.range_mode,
&range_bytes.read_mode,
) {
return NextStep::Finish;
NextStep::Finish
} else {
return NextStep::Error(Some(std::mem::take(&mut captures.hashmap_for_error)));
NextStep::Error(Some(std::mem::take(&mut captures.hashmap_for_error)))
}
}
16 changes: 8 additions & 8 deletions flexible_inspect_rs/src/rules/rule_str/runner_range.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::rules::{next::NextStep, traits::IntoConcreteType};
use super::rules::{next::NextStep, traits::IntoSpecificCaptureType};
use super::{convert::convert_and_filter, *};
use std::{
fmt::{Debug, Display},
Expand All @@ -9,7 +9,7 @@ use std::{
fn single_range_str_check<
'a,
T: FromStr + Copy + Debug + Display + PartialOrd,
C: IntoConcreteType<'a>,
C: IntoSpecificCaptureType<'a>,
>(
captures: &mut CaptureData<'a, C>,
range: &RangeInclusive<T>,
Expand All @@ -21,7 +21,7 @@ fn single_range_str_check<
.text_for_capture
.iter()
.filter(|&num| {
convert_and_filter(num.into_str().unwrap())
convert_and_filter(num.as_str().unwrap())
.map(|num| range.contains(&num))
.unwrap_or(false)
})
Expand All @@ -33,7 +33,7 @@ fn single_range_str_check<
.text_for_capture
.iter()
.filter(|&num| {
convert_and_filter(num.into_str().unwrap())
convert_and_filter(num.as_str().unwrap())
.map(|num| range.contains(&num))
.unwrap_or(false)
})
Expand All @@ -46,7 +46,7 @@ fn single_range_str_check<
.text_for_capture
.iter()
.filter(|&num| {
convert_and_filter(num.into_str().unwrap())
convert_and_filter(num.as_str().unwrap())
.map(|num| range.contains(&num))
.unwrap_or(false)
})
Expand All @@ -60,15 +60,15 @@ fn single_range_str_check<
pub fn single_str_result<
'a,
T: Debug + FromStr + Copy + Display + PartialOrd,
C: IntoConcreteType<'a>,
C: IntoSpecificCaptureType<'a>,
>(
range_str: &RangeStr,
captures: &mut CaptureData<'a, C>,
value: &RangeInclusive<T>,
) -> NextStep {
if single_range_str_check(captures, value, range_str.range_mode) {
return NextStep::Finish;
NextStep::Finish
} else {
return NextStep::Error(Some(std::mem::take(&mut captures.hashmap_for_error)));
NextStep::Error(Some(std::mem::take(&mut captures.hashmap_for_error)))
}
}
Loading

0 comments on commit 791da71

Please sign in to comment.