AIT/src/controller/view_utils/input_utils.rs

62 lines
1.6 KiB
Rust
Raw Normal View History

2024-03-10 11:54:32 +03:00
use gtk4 as gtk;
use bitvec::{order::Lsb0, view::AsBits};
use gtk::{prelude::*, *};
use std::ops::Deref;
2024-03-10 11:54:32 +03:00
use crate::{model::model::*, model_utils::hamming_code_seven_four::*};
2024-03-10 11:54:32 +03:00
pub fn parse_input(input: &TextView, output: &TextView, mode: bool) -> () {
2024-03-10 11:54:32 +03:00
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input
2024-03-10 11:54:32 +03:00
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse()
.unwrap();
let operation = if mode == false {
HammingMode::Encrypt
} else {
HammingMode::Decrypt
};
2024-03-10 11:54:32 +03:00
match hamming(parsed_input, operation) {
Ok(res) => output.buffer().set_text(res.trim_end()),
Err(rej) => output.buffer().set_text(rej.as_str()),
}
2024-03-10 11:54:32 +03:00
}
pub fn processing_input(input: &String) -> String {
2024-03-10 11:54:32 +03:00
input
.split_ascii_whitespace()
.filter(|&x| x != "")
.fold(String::new(), |c: String, n: &str| c + n)
2024-03-10 11:54:32 +03:00
}
pub fn check_correct_input(input: &String, prepared_input: &String, l: usize) -> (bool, bool) {
2024-04-16 23:06:56 +03:00
let first_condition = input
.as_str()
.chars()
.all(|c| c == '1' || c == '0' || c == ' ');
2024-03-10 11:54:32 +03:00
let second_condition = prepared_input.len() % l == 0;
2024-03-10 11:54:32 +03:00
(first_condition, second_condition)
}
pub fn from_string_to_vec_bits(raw_data: String) -> Vec<u8> {
2024-03-10 11:54:32 +03:00
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("");
}