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

View File

@ -1,26 +1,16 @@
use crate::{ use crate::{gtk::prelude::*, model::model::*, view::components::switch::SwitchExt};
model::model::*,
view::components::switch::SwitchExt,
gtk::{
*,
prelude::*
},
};
pub trait SwEventHandler { pub trait SwEventHandler {
fn on_toggle(self) -> (); fn on_toggle(self) -> ();
} }
impl<F, C> SwEventHandler for EventHandler<F, C> 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) -> () { fn on_toggle(self) -> () {
self.component.connect_state_notify(move |switch| { self.component
(self.callback)(switch) .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], (data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4],
); );
match checked_bits { if let (true, true, true) = checked_bits {
(true, true, true) => {
general_length += length_of_code; general_length += length_of_code;
continue; continue;
} } else {
_ => {
let error_position = syndromes let error_position = syndromes
.iter() .iter()
.find(move |&(&_error_position, &error)| error == checked_bits) .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() .enumerate()
.map(|(index, bit)| { .map(|(index, bit)| {
if index == error_position - 1 { if index == error_position - 1 {
if *bit == 1u8 { (*bit == 0u8).into()
0u8
} else {
1u8
}
} else { } else {
*bit *bit
} }
@ -124,11 +118,14 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
general_length += length_of_code; general_length += length_of_code;
} }
} }
}
if data.len() > 0 {
if errors.len() == 0 { if errors.len() == 0 {
result_string.push_str("Все коды корректны."); result_string.push_str("Все коды корректны.");
} else { } 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 gtk4 as gtk;
use std::ops::Deref;
use gtk::{*, prelude::*};
use bitvec::{order::Lsb0, view::AsBits}; use bitvec::{order::Lsb0, view::AsBits};
use gtk::{prelude::*, *};
use std::ops::Deref;
use crate::{ use crate::{model::model::*, model_utils::hamming_code_seven_four::*};
model::model::*,
model_utils::hamming_code_seven_four::*
};
pub fn parse_input(input: &TextView, output: &TextView, mode: bool) -> () { pub fn parse_input(input: &TextView, output: &TextView, mode: bool) -> () {
let (iter_start, iter_end) = input.buffer().bounds(); let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input let parsed_input: String = input
.buffer() .buffer()
@ -30,39 +26,33 @@ pub fn parse_input(input : &TextView, output : &TextView, mode: bool) -> (){
Ok(res) => output.buffer().set_text(res.trim_end()), Ok(res) => output.buffer().set_text(res.trim_end()),
Err(rej) => output.buffer().set_text(rej.as_str()), Err(rej) => output.buffer().set_text(rej.as_str()),
} }
} }
pub fn processing_input(input: &String) -> String { pub fn processing_input(input: &String) -> String {
input input
.split_ascii_whitespace() .split_ascii_whitespace()
.filter(|&x| { .filter(|&x| x != "")
x != "" .fold(String::new(), |c: String, n: &str| c + n)
})
.fold(String::new(), |c: String, n: &str| { c + n })
} }
pub fn check_correct_input(input: &String, prepared_input: &String, l: usize) -> (bool, bool) { 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; let second_condition = prepared_input.len() % l == 0;
(first_condition, second_condition) (first_condition, second_condition)
} }
pub fn from_string_to_vec_bits(raw_data: String) -> Vec<u8> { pub fn from_string_to_vec_bits(raw_data: String) -> Vec<u8> {
raw_data raw_data
.as_bits::<Lsb0>() .as_bits::<Lsb0>()
.iter() .iter()
.step_by(8) .step_by(8)
.map(|x| *x.deref() as u8) .map(|x| *x.deref() as u8)
.collect() .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) .build(monospace, input_wrapping, input_height)
.get(); .get();
wire_length_input.set_halign(input_alignment);
wire_length_input.set_hexpand(true);
let resistance_per_meter_input = Input::builder() let resistance_per_meter_input = Input::builder()
.set_label("Rм, Ом:") .set_label("Rм, Ом:")
.set_margins(MarginData::EqualsMargin(5)) .set_margins(MarginData::EqualsMargin(5))
@ -35,9 +32,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height) .build(monospace, input_wrapping, input_height)
.get(); .get();
resistance_per_meter_input.set_halign(input_alignment);
resistance_per_meter_input.set_hexpand(true);
let capacity_per_meter_input = Input::builder() let capacity_per_meter_input = Input::builder()
.set_label("Cм, пФ:") .set_label("Cм, пФ:")
.set_margins(MarginData::EqualsMargin(5)) .set_margins(MarginData::EqualsMargin(5))
@ -45,18 +39,24 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height) .build(monospace, input_wrapping, input_height)
.get(); .get();
capacity_per_meter_input.set_halign(input_alignment);
capacity_per_meter_input.set_hexpand(true);
let wrapper_first_row = Wrapper::col_builder() let wrapper_first_row = Wrapper::col_builder()
.halign(Align::Fill) .halign(Align::Fill)
.hexpand(true) .hexpand(true)
.spacing(5) .spacing(5)
.build(); .build();
wrapper_first_row.append(&wire_length_input); let first_row_elements: Vec<&Box> = vec![
wrapper_first_row.append(&resistance_per_meter_input); &wire_length_input,
wrapper_first_row.append(&capacity_per_meter_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() let info_signal_voltage_input = Input::builder()
.set_label("Vи, мВ:") .set_label("Vи, мВ:")
@ -65,9 +65,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height) .build(monospace, input_wrapping, input_height)
.get(); .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() let resistance_of_info_voltage_source_input = Input::builder()
.set_label("Rи, Ом:") .set_label("Rи, Ом:")
.set_margins(MarginData::EqualsMargin(5)) .set_margins(MarginData::EqualsMargin(5))
@ -75,9 +72,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height) .build(monospace, input_wrapping, input_height)
.get(); .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() let info_voltage_source_frequency_input = Input::builder()
.set_label("f, мГц:") .set_label("f, мГц:")
.set_margins(MarginData::EqualsMargin(5)) .set_margins(MarginData::EqualsMargin(5))
@ -85,18 +79,24 @@ pub fn signal_reducing_page(wrapper: &Box) {
.build(monospace, input_wrapping, input_height) .build(monospace, input_wrapping, input_height)
.get(); .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() let wrapper_second_row = Wrapper::col_builder()
.halign(Align::Fill) .halign(Align::Fill)
.hexpand(true) .hexpand(true)
.spacing(5) .spacing(5)
.build(); .build();
wrapper_second_row.append(&info_signal_voltage_input); let second_row_elements: Vec<&Box> = vec![
wrapper_second_row.append(&resistance_of_info_voltage_source_input); &info_signal_voltage_input,
wrapper_second_row.append(&info_voltage_source_frequency_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(); let main_wpapper = Wrapper::row_builder().spacing(5).build();