REFACTOR: clean code and some bug fixed

This commit is contained in:
doryan 2024-04-16 21:45:31 +04:00
parent 48057646a8
commit a58110a3f8
5 changed files with 93 additions and 112 deletions

View File

@ -1,7 +1,11 @@
use gtk4 as gtk;
use gtk::*;
use gtk4 as gtk;
pub fn state_controller(switch: &Switch, label: &Label) -> () {
if switch.state() == true { label.set_label("Режим: проверка"); }
else { label.set_label("Режим: кодирование"); }
if switch.state() == true {
label.set_label("Режим: проверка");
} else {
label.set_label("Режим: кодирование");
}
}

View File

@ -1,26 +1,16 @@
use crate::{
model::model::*,
view::components::switch::SwitchExt,
gtk::{
*,
prelude::*
},
};
use crate::{gtk::prelude::*, model::model::*, view::components::switch::SwitchExt};
pub trait SwEventHandler {
fn on_toggle(self) -> ();
}
impl<F, C> SwEventHandler for EventHandler<F, C>
where F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static, C: SwitchExt + WidgetExt{
where
F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static,
C: SwitchExt + WidgetExt,
{
fn on_toggle(self) -> () {
self.component.connect_state_notify(move |switch| {
(self.callback)(switch)
});
self.component
.connect_state_notify(move |switch| (self.callback)(switch));
}
}
pub fn clearing(output : &TextView, input: &TextView){
input.buffer().set_text("");
output.buffer().set_text("");
}

View File

@ -83,12 +83,10 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
(data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4],
);
match checked_bits {
(true, true, true) => {
if let (true, true, true) = checked_bits {
general_length += length_of_code;
continue;
}
_ => {
} else {
let error_position = syndromes
.iter()
.find(move |&(&_error_position, &error)| error == checked_bits)
@ -100,11 +98,7 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
.enumerate()
.map(|(index, bit)| {
if index == error_position - 1 {
if *bit == 1u8 {
0u8
} else {
1u8
}
(*bit == 0u8).into()
} else {
*bit
}
@ -124,11 +118,14 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
general_length += length_of_code;
}
}
}
if data.len() > 0 {
if errors.len() == 0 {
result_string.push_str("Все коды корректны.");
} else {
result_string.push_str(errors.as_str())
result_string.push_str(errors.as_str());
}
} else {
result_string.push_str("Введите код для проверки.")
}
}

View File

@ -1,16 +1,12 @@
use gtk4 as gtk;
use std::ops::Deref;
use gtk::{*, prelude::*};
use bitvec::{order::Lsb0, view::AsBits};
use gtk::{prelude::*, *};
use std::ops::Deref;
use crate::{
model::model::*,
model_utils::hamming_code_seven_four::*
};
use crate::{model::model::*, model_utils::hamming_code_seven_four::*};
pub fn parse_input(input: &TextView, output: &TextView, mode: bool) -> () {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input
.buffer()
@ -30,39 +26,33 @@ pub fn parse_input(input : &TextView, output : &TextView, mode: bool) -> (){
Ok(res) => output.buffer().set_text(res.trim_end()),
Err(rej) => output.buffer().set_text(rej.as_str()),
}
}
pub fn processing_input(input: &String) -> String {
input
.split_ascii_whitespace()
.filter(|&x| {
x != ""
})
.fold(String::new(), |c: String, n: &str| { c + n })
.filter(|&x| x != "")
.fold(String::new(), |c: String, n: &str| c + n)
}
pub fn check_correct_input(input: &String, prepared_input: &String, l: usize) -> (bool, bool) {
let first_condition = input
.chars()
.all(|c| {c == '1' || c == '0' || c == ' '});
let first_condition = input.chars().all(|c| c == '1' || c == '0' || c == ' ');
let second_condition = prepared_input.len() % l == 0;
(first_condition, second_condition)
}
pub fn from_string_to_vec_bits(raw_data: String) -> Vec<u8> {
raw_data
.as_bits::<Lsb0>()
.iter()
.step_by(8)
.map(|x| *x.deref() as u8)
.collect()
}
pub fn clearing(output: &TextView, input: &TextView) {
input.buffer().set_text("");
output.buffer().set_text("");
}

View File

@ -25,9 +25,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height)
.get();
wire_length_input.set_halign(input_alignment);
wire_length_input.set_hexpand(true);
let resistance_per_meter_input = Input::builder()
.set_label("Rм, Ом:")
.set_margins(MarginData::EqualsMargin(5))
@ -35,9 +32,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height)
.get();
resistance_per_meter_input.set_halign(input_alignment);
resistance_per_meter_input.set_hexpand(true);
let capacity_per_meter_input = Input::builder()
.set_label("Cм, пФ:")
.set_margins(MarginData::EqualsMargin(5))
@ -45,18 +39,24 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height)
.get();
capacity_per_meter_input.set_halign(input_alignment);
capacity_per_meter_input.set_hexpand(true);
let wrapper_first_row = Wrapper::col_builder()
.halign(Align::Fill)
.hexpand(true)
.spacing(5)
.build();
wrapper_first_row.append(&wire_length_input);
wrapper_first_row.append(&resistance_per_meter_input);
wrapper_first_row.append(&capacity_per_meter_input);
let first_row_elements: Vec<&Box> = vec![
&wire_length_input,
&resistance_per_meter_input,
&capacity_per_meter_input,
];
for elem in first_row_elements {
elem.set_halign(input_alignment);
elem.set_hexpand(true);
wrapper_first_row.append(elem);
}
let info_signal_voltage_input = Input::builder()
.set_label("Vи, мВ:")
@ -65,9 +65,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height)
.get();
info_signal_voltage_input.set_halign(input_alignment);
info_signal_voltage_input.set_hexpand(true);
let resistance_of_info_voltage_source_input = Input::builder()
.set_label("Rи, Ом:")
.set_margins(MarginData::EqualsMargin(5))
@ -75,9 +72,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height)
.get();
resistance_of_info_voltage_source_input.set_halign(input_alignment);
resistance_of_info_voltage_source_input.set_hexpand(true);
let info_voltage_source_frequency_input = Input::builder()
.set_label("f, мГц:")
.set_margins(MarginData::EqualsMargin(5))
@ -85,18 +79,24 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height)
.get();
info_voltage_source_frequency_input.set_halign(input_alignment);
info_voltage_source_frequency_input.set_hexpand(true);
let wrapper_second_row = Wrapper::col_builder()
.halign(Align::Fill)
.hexpand(true)
.spacing(5)
.build();
wrapper_second_row.append(&info_signal_voltage_input);
wrapper_second_row.append(&resistance_of_info_voltage_source_input);
wrapper_second_row.append(&info_voltage_source_frequency_input);
let second_row_elements: Vec<&Box> = vec![
&info_signal_voltage_input,
&resistance_of_info_voltage_source_input,
&info_voltage_source_frequency_input,
];
for elem in second_row_elements {
elem.set_halign(input_alignment);
elem.set_hexpand(true);
wrapper_second_row.append(elem);
}
let main_wpapper = Wrapper::row_builder().spacing(5).build();