fix(fn): rewrite code with using a custom type of Error
This commit is contained in:
parent
656e75ff35
commit
e97b1b281d
|
@ -1,28 +1,31 @@
|
||||||
use gtk4 as gtk;
|
use gtk4 as gtk;
|
||||||
|
|
||||||
use std::{num::ParseFloatError, str::FromStr};
|
use std::str::FromStr;
|
||||||
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
prelude::{TextBufferExt, TextViewExt},
|
prelude::{TextBufferExt, TextViewExt},
|
||||||
TextBuffer,
|
TextBuffer,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{model::models::SignalReduce, view::components::input::Input};
|
use crate::{
|
||||||
|
model::{models::SignalReduce, Error, Result},
|
||||||
|
view::components::input::Input,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn get_error_message(error_instance: ParseFloatError) -> Option<&'static str> {
|
pub fn get_error_message(error: Error) -> Option<&'static str> {
|
||||||
match error_instance.to_string().as_str() {
|
match error.to_string().as_str() {
|
||||||
"cannot parse float from empty string" => Some("Вы не ввели данные в поле/-я"),
|
"cannot parse float from empty string" => Some("Вы не ввели данные в поле/-я"),
|
||||||
"invalid float literal" => Some("Вы ввели не корректные данные в поле/-я"),
|
"invalid float literal" => Some("Вы ввели не корректные данные в поле/-я"),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_fields(all_inputs: Vec<Input>) -> Result<SignalReduce, ParseFloatError> {
|
pub fn parse_fields(all_inputs: Vec<Input>) -> Result<SignalReduce> {
|
||||||
let mut values: [f64; 6] = [0f64; 6];
|
let mut values: [f64; 6] = [0.0; 6];
|
||||||
|
|
||||||
for (i, input) in all_inputs.iter().enumerate() {
|
for (i, input) in all_inputs.iter().enumerate() {
|
||||||
let input_text_buffer: TextBuffer = input.clone().get_input().buffer();
|
let input_text_buffer: TextBuffer = input.clone().get_input().buffer();
|
||||||
let try_extract_value = f64::from_str(
|
let extracted_value = f64::from_str(
|
||||||
input_text_buffer
|
input_text_buffer
|
||||||
.text(
|
.text(
|
||||||
&input_text_buffer.start_iter(),
|
&input_text_buffer.start_iter(),
|
||||||
|
@ -31,14 +34,16 @@ pub fn parse_fields(all_inputs: Vec<Input>) -> Result<SignalReduce, ParseFloatEr
|
||||||
)
|
)
|
||||||
.as_str()
|
.as_str()
|
||||||
.trim(),
|
.trim(),
|
||||||
);
|
)?;
|
||||||
match try_extract_value {
|
values[i] = extracted_value;
|
||||||
Ok(value) => values[i] = value,
|
|
||||||
Err(error) => return Err(error),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(SignalReduce(
|
Ok(SignalReduce {
|
||||||
values[0], values[1], values[2], values[3], values[4], values[5],
|
length: values[0],
|
||||||
))
|
wire_resistance: values[1],
|
||||||
|
wire_capacity: values[2],
|
||||||
|
source_resistance: values[3],
|
||||||
|
source_voltage: values[4],
|
||||||
|
frequency: values[5],
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue