refactor: move parse field algorithm to a separate file

This commit is contained in:
doryan 2024-07-29 01:18:15 +04:00
parent 18e857b40c
commit 09b05e7fa7

View File

@ -1,119 +1,51 @@
use gtk4 as gtk; use gtk4 as gtk;
use crate::controller::model_utils::signal_reducer::*; use std::{num::ParseFloatError, str::FromStr};
use crate::model::models::SchemeCharacteristics;
use gtk::{prelude::*, *};
pub fn check_characteristics( use gtk::{
raw_characteristics: &Vec<TextView>, prelude::{TextBufferExt, TextViewExt},
all_inputs_data: &mut Vec<f64>, TextBuffer,
output: &mut String, };
) -> bool {
for input in raw_characteristics {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: Result<f64, _> = input
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse();
match parsed_input { use crate::view::components::{info_bar::InfoBar, input::Input};
Ok(int) => {
all_inputs_data.push(int); pub fn get_error_message(error_instance: ParseFloatError) -> Option<&'static str> {
println!("{:?}", int); match error_instance.to_string().as_str() {
} "cannot parse float from empty string" => Some("Вы не ввели данные в поле/-я"),
Err(err) => { "invalid float literal" => Some("Вы ввели не корректные данные в поле/-я"),
if output.is_empty() { _ => None,
output.push_str("Введите пожалуйста числа в полях");
println!("{:?}", err);
}
}
} }
} }
!output.is_empty() pub fn parse_fields(all_inputs: Vec<Input>) -> Option<[f64; 6]> {
} let mut values: [f64; 6] = [0f64; 6];
pub fn start_algorithm( let info_bar = InfoBar::get_instance();
error_log_label: &Label,
raw_characteristics: &Vec<TextView>,
table: &Frame,
header: &ListBoxRow,
) {
let mut all_inputs_data: Vec<f64> = Vec::new();
let mut output: String = String::new();
if !check_characteristics(raw_characteristics, &mut all_inputs_data, &mut output) { for (i, input) in all_inputs.iter().enumerate() {
error_log_label.set_text(output.as_str()); let input_text_buffer: TextBuffer = input.clone().get_input().buffer();
} else { let try_extract_value = f64::from_str(
error_log_label.set_text(""); input_text_buffer
.text(
let new_table = ListBox::builder().hexpand(true).halign(Align::Fill).build(); &input_text_buffer.start_iter(),
&input_text_buffer.end_iter(),
new_table.append(header); false,
)
let (L, Rm, Cm, Vs, Rs, f): SchemeCharacteristics = ( .as_str()
all_inputs_data[0], .trim(),
all_inputs_data[1],
all_inputs_data[2],
all_inputs_data[3],
all_inputs_data[4],
all_inputs_data[5],
); );
match try_extract_value {
Ok(value) => values[i] = value,
Err(error) => {
let error_kind: Option<&str> = get_error_message(error);
let mut frequencies: Vec<usize> = vec![f as usize, 1usize]; info_bar.set_text_label(error_kind);
info_bar.show_infobar(5u64);
frequencies.append(&mut (0..100).step_by(5).collect()); return None;
for i in frequencies {
if i == 0 {
continue;
}
let table_row = ListBoxRow::new();
let columns = ColumnView::builder().reorderable(false).build();
let frequency_column = ColumnViewColumn::builder()
.title(format!("{i}"))
.fixed_width(200)
.expand(true)
.build();
let Xc: f64 = reactive_resistance_of_capacitor(Cm, L, f);
let Z: f64 = full_resistance_of_capacitor(Xc, Vs, Rm, L);
let V: f64 = voltage_from_signal_source(Vs, Xc, Z);
let xi: f64 = coef_of_signal_reduce(Vs, V);
let reactive_resistance_column = ColumnViewColumn::builder()
.title(format!("{:?}", Xc))
.fixed_width(200)
.expand(true)
.build();
let voltage_column = ColumnViewColumn::builder()
.title(format!("{:?}", V))
.fixed_width(200)
.expand(true)
.build();
let coef_column = ColumnViewColumn::builder()
.title(format!("{:?}", xi))
.fixed_width(200)
.expand(true)
.build();
columns.append_column(&frequency_column);
columns.append_column(&reactive_resistance_column);
columns.append_column(&voltage_column);
columns.append_column(&coef_column);
table_row.set_child(Some(&columns));
new_table.append(&table_row);
table.set_child(Some(&new_table));
} }
} }
} }
Some(values)
}