AIT/src/controller/view_utils/input_utils.rs

68 lines
1.5 KiB
Rust
Raw Normal View History

2024-03-10 11:54:32 +03:00
use gtk4 as gtk;
use std::ops::Deref;
use gtk::{*, prelude::*};
use bitvec::{order::Lsb0, view::AsBits};
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()
.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
}
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
}
2024-03-10 11:54:32 +03:00
pub fn check_correct_input(input: &String, prepared_input: &String, l: usize) -> (bool, bool){
2024-03-10 11:54:32 +03:00
let first_condition = input
.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)
2024-03-10 11:54:32 +03:00
}
2024-03-10 11:54:32 +03:00
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()
}