Fully refactored project architecture, imports and etc.
This commit is contained in:
7
src/controller/controller.rs
Normal file
7
src/controller/controller.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use gtk4 as gtk;
|
||||
use gtk::*;
|
||||
|
||||
pub fn state_controller(switch: &Switch, label: &Label) -> (){
|
||||
if switch.state() == true { label.set_label("Режим: проверка"); }
|
||||
else { label.set_label("Режим: кодирование"); }
|
||||
}
|
||||
34
src/controller/event_handlers/button_event_handlers.rs
Normal file
34
src/controller/event_handlers/button_event_handlers.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
pub mod button_event_handlers_module{
|
||||
|
||||
use crate::{
|
||||
model::model::model_module::*,
|
||||
gtk::{
|
||||
*,
|
||||
prelude::*
|
||||
},
|
||||
};
|
||||
|
||||
impl<F, C> EventHandler<F, C>
|
||||
where F: Fn(&C) + FnOnce(&C) + FnMut(&C){
|
||||
pub fn new(component: C, callback: F) -> EventHandler<F, C>{
|
||||
Self{
|
||||
component,
|
||||
callback,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BtnEventHandler{
|
||||
fn on_click(self) -> ();
|
||||
}
|
||||
|
||||
impl<F, C> BtnEventHandler for EventHandler<F, C>
|
||||
where F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static, C: ButtonExt + WidgetExt{
|
||||
fn on_click(self) -> () {
|
||||
self.component.connect_clicked(move |button| {
|
||||
(self.callback)(button)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
2
src/controller/event_handlers/mod.rs
Normal file
2
src/controller/event_handlers/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod button_event_handlers;
|
||||
pub mod switch_event_handlers;
|
||||
30
src/controller/event_handlers/switch_event_handlers.rs
Normal file
30
src/controller/event_handlers/switch_event_handlers.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
pub mod switch_event_handlers_module{
|
||||
|
||||
use crate::{
|
||||
model::model::model_module::*,
|
||||
view::components::switch::switch_module::SwitchExt,
|
||||
gtk::{
|
||||
*,
|
||||
prelude::*
|
||||
},
|
||||
};
|
||||
|
||||
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{
|
||||
fn on_toggle(self) -> () {
|
||||
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("");
|
||||
}
|
||||
|
||||
}
|
||||
4
src/controller/mod.rs
Normal file
4
src/controller/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod controller;
|
||||
pub mod view_utils;
|
||||
pub mod model_utils;
|
||||
pub mod event_handlers;
|
||||
159
src/controller/model_utils/hamming_code_seven_four.rs
Normal file
159
src/controller/model_utils/hamming_code_seven_four.rs
Normal file
@@ -0,0 +1,159 @@
|
||||
pub mod hamming_code{
|
||||
|
||||
use std::{borrow::Borrow, collections::HashMap};
|
||||
|
||||
use crate::{
|
||||
model::model::model_module::*,
|
||||
controller::view_utils::input_utils::input_utils_module::*,
|
||||
};
|
||||
|
||||
/// **Синдромы**
|
||||
///
|
||||
/// ошибочная позиция 1 false true true.
|
||||
///
|
||||
/// ошибочная позиция 2 false false true.
|
||||
///
|
||||
/// ошибочная позиция 3 true false true.
|
||||
///
|
||||
/// ошибочная позиция 4 false true false.
|
||||
///
|
||||
/// ошибочная позиция 5 true true false.
|
||||
///
|
||||
/// ошибочная позиция 6 true false false.
|
||||
///
|
||||
/// ошибочная позиция 7 false false false.
|
||||
|
||||
pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String, String>{
|
||||
|
||||
let length_of_code : usize = mode.clone() as usize;
|
||||
|
||||
let prepared_input : String = processing_input(&raw_input);
|
||||
|
||||
let (fc, sc) = check_correct_input(&raw_input, &prepared_input, length_of_code);
|
||||
|
||||
if !fc || !sc {
|
||||
|
||||
Err("Ошибка. Проверьте корректность ввода.".to_string())
|
||||
|
||||
} else {
|
||||
|
||||
let mut data : String = String::new();
|
||||
|
||||
let prepared_data: Vec<u8> = from_string_to_vec_bits(prepared_input);
|
||||
|
||||
match mode {
|
||||
HammingMode::Encrypt => hamming_encrypt_data(&prepared_data, &mut data, length_of_code),
|
||||
HammingMode::Decrypt => hamming_decrypt_data(&prepared_data, &mut data, length_of_code),
|
||||
}
|
||||
|
||||
return Ok(data);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn hamming_encrypt_data(
|
||||
data: &Vec<u8>,
|
||||
result_string: &mut String,
|
||||
length_of_code: usize
|
||||
) {
|
||||
let mut i : usize = length_of_code;
|
||||
|
||||
while i <= data.len(){
|
||||
|
||||
let data_bits = &data[i - length_of_code..i];
|
||||
let (check_bit_1, check_bit_2, check_bit_3) = (
|
||||
data_bits[0] ^ data_bits[1] ^ data_bits[3],
|
||||
data_bits[0] ^ data_bits[2] ^ data_bits[3],
|
||||
data_bits[1] ^ data_bits[2] ^ data_bits[3]
|
||||
);
|
||||
result_string.push_str(&*format!("{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ",
|
||||
data_bits[0],
|
||||
data_bits[1],
|
||||
data_bits[2],
|
||||
data_bits[3]));
|
||||
i += length_of_code;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hamming_decrypt_data(
|
||||
data: &Vec<u8>,
|
||||
result_string: &mut String,
|
||||
length_of_code: usize
|
||||
) {
|
||||
|
||||
let mut i : usize = length_of_code;
|
||||
|
||||
let syndromes : HashMap<usize, (bool, bool, bool)> = HashMap::from(
|
||||
[
|
||||
(1, (false, true, true)),
|
||||
(2, (false, false, true)),
|
||||
(3, (true, false, true)),
|
||||
(4, (false, true, false)),
|
||||
(5, (true, true, false)),
|
||||
(6, (true, false, false)),
|
||||
(7, (false, false, false)),
|
||||
]
|
||||
);
|
||||
|
||||
let mut errors : String = String::new();
|
||||
|
||||
while i <= data.len(){
|
||||
|
||||
let mut data_bits = &data[i - length_of_code..i];
|
||||
|
||||
let checked_bits : (bool, bool, bool) =
|
||||
(
|
||||
(data_bits[1] ^ data_bits[3] ^ data_bits[6]) == data_bits[0],
|
||||
(data_bits[1] ^ data_bits[5] ^ data_bits[6]) == data_bits[2],
|
||||
(data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4]
|
||||
);
|
||||
|
||||
match checked_bits {
|
||||
(true, true, true) => {
|
||||
i += length_of_code;
|
||||
continue;
|
||||
},
|
||||
_ => {
|
||||
|
||||
let error_position = syndromes
|
||||
.iter()
|
||||
.find(move |&(&error_position, &error)| error == checked_bits).
|
||||
unwrap().0;
|
||||
|
||||
let correctly_code : Vec<u8> = data_bits
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, bit)| {
|
||||
if index == error_position - 1 {
|
||||
if *bit == 1u8 { 0u8 } else { 1u8 }
|
||||
} else {
|
||||
*bit
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let error = format!("Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n",
|
||||
i / 7,
|
||||
&data_bits,
|
||||
error_position,
|
||||
correctly_code
|
||||
);
|
||||
|
||||
errors.push_str(error.as_str());
|
||||
|
||||
i += length_of_code;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if errors.len() == 0 {
|
||||
result_string.push_str("Все коды корректны.");
|
||||
} else {
|
||||
result_string.push_str(errors.as_str())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
1
src/controller/model_utils/mod.rs
Normal file
1
src/controller/model_utils/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod hamming_code_seven_four;
|
||||
72
src/controller/view_utils/input_utils.rs
Normal file
72
src/controller/view_utils/input_utils.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
pub mod input_utils_module {
|
||||
|
||||
use gtk4 as gtk;
|
||||
|
||||
use std::ops::Deref;
|
||||
use gtk::{*, prelude::*};
|
||||
use bitvec::{order::Lsb0, view::AsBits};
|
||||
|
||||
use crate::{
|
||||
model::model::model_module::*,
|
||||
model_utils::hamming_code_seven_four::hamming_code::*
|
||||
};
|
||||
|
||||
pub fn parse_input(input : &TextView, output : &TextView, mode: bool) -> (){
|
||||
|
||||
let (iter_start, iter_end) = input.buffer().bounds();
|
||||
let parsed_input : String = input
|
||||
.buffer()
|
||||
.text(&iter_start, &iter_end, false)
|
||||
.to_string()
|
||||
.trim()
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let operation = if mode == false {
|
||||
HammingMode::Encrypt
|
||||
} else {
|
||||
HammingMode::Decrypt
|
||||
};
|
||||
|
||||
match hamming(parsed_input, operation) {
|
||||
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 })
|
||||
|
||||
}
|
||||
|
||||
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 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()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
1
src/controller/view_utils/mod.rs
Normal file
1
src/controller/view_utils/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod input_utils;
|
||||
Reference in New Issue
Block a user