refactor: rewrite function logic with use custom Error type

This commit is contained in:
doryan 2024-08-10 01:10:43 +04:00
parent cad9823c83
commit 5a8a4b00ce

View File

@ -18,27 +18,25 @@ static SYNDROMES: LazyLock<HashMap<usize, (bool, bool, bool)>> = LazyLock::new(|
])
});
pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String, String> {
pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String> {
let length_of_code: usize = mode.clone() as usize;
let prepared_input: String = processing_input(&raw_input);
let (fc, sc): (bool, bool) =
let (first_condition, second_condition): (bool, bool) =
check_correct_binary_code(&raw_input, &prepared_input, length_of_code);
if !fc || !sc {
Err("Ошибка. Проверьте корректность ввода.".to_string())
if raw_input.is_empty() {
Err("Введите код.".into())
} else if !first_condition || !second_condition {
Err("Проверьте корректность ввода.".into())
} else {
let mut data: String = String::new();
let prepared_data: Vec<u8> = from_string_to_vec_bits(prepared_input);
match mode {
HammingMode::Encrypt => hamming_encrypt_data(&prepared_data, &mut data, length_of_code),
HammingMode::Decrypt => hamming_decrypt_data(&prepared_data, &mut data, length_of_code),
HammingMode::Encrypt => Ok(hamming_encrypt_data(&prepared_data, length_of_code)),
HammingMode::Decrypt => Ok(hamming_decrypt_data(&prepared_data, length_of_code)),
}
Ok(data)
}
}