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 std::{num::ParseFloatError, str::FromStr};
|
||||
use std::str::FromStr;
|
||||
|
||||
use gtk::{
|
||||
prelude::{TextBufferExt, TextViewExt},
|
||||
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> {
|
||||
match error_instance.to_string().as_str() {
|
||||
pub fn get_error_message(error: Error) -> Option<&'static str> {
|
||||
match error.to_string().as_str() {
|
||||
"cannot parse float from empty string" => Some("Вы не ввели данные в поле/-я"),
|
||||
"invalid float literal" => Some("Вы ввели не корректные данные в поле/-я"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_fields(all_inputs: Vec<Input>) -> Result<SignalReduce, ParseFloatError> {
|
||||
let mut values: [f64; 6] = [0f64; 6];
|
||||
pub fn parse_fields(all_inputs: Vec<Input>) -> Result<SignalReduce> {
|
||||
let mut values: [f64; 6] = [0.0; 6];
|
||||
|
||||
for (i, input) in all_inputs.iter().enumerate() {
|
||||
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
|
||||
.text(
|
||||
&input_text_buffer.start_iter(),
|
||||
|
@ -31,14 +34,16 @@ pub fn parse_fields(all_inputs: Vec<Input>) -> Result<SignalReduce, ParseFloatEr
|
|||
)
|
||||
.as_str()
|
||||
.trim(),
|
||||
);
|
||||
match try_extract_value {
|
||||
Ok(value) => values[i] = value,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
)?;
|
||||
values[i] = extracted_value;
|
||||
}
|
||||
|
||||
Ok(SignalReduce(
|
||||
values[0], values[1], values[2], values[3], values[4], values[5],
|
||||
))
|
||||
Ok(SignalReduce {
|
||||
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