Fix crash when inserting characters longer than one byte in the text input for byte threshold notification setting

This commit is contained in:
GyulyVGC 2025-03-10 18:12:11 +01:00
parent db88533419
commit 7984c1c8c8

View file

@ -101,16 +101,17 @@ impl BytesNotification {
let default = existing.unwrap_or_default();
let mut byte_multiple_inserted = ByteMultiple::B;
let new_threshold = if value.is_empty() {
let chars: Vec<char> = value.trim().chars().collect();
let new_threshold = if chars.is_empty() {
0
} else if !value.trim().chars().map(char::is_numeric).any(|x| !x) {
} else if !chars.iter().map(|c| char::is_numeric(*c)).any(|x| !x) {
// no multiple
value.parse::<u64>().unwrap_or(default.previous_threshold)
} else {
// multiple
let last_char = value.chars().last().unwrap();
byte_multiple_inserted = ByteMultiple::from_char(last_char);
let without_multiple = value[0..value.len() - 1].trim().to_string();
let last_char = chars.last().unwrap();
byte_multiple_inserted = ByteMultiple::from_char(*last_char);
let without_multiple: String = chars[0..chars.len() - 1].iter().collect();
if without_multiple.parse::<u64>().is_ok()
&& TryInto::<u64>::try_into(
without_multiple.parse::<u128>().unwrap()
@ -182,13 +183,11 @@ mod tests {
previous_threshold: 123, threshold: Some(123), byte_multiple: ByteMultiple::B, ..BytesNotification::default() })]
#[case("500k", BytesNotification {
previous_threshold: 500_000, threshold: Some(500_000),byte_multiple: ByteMultiple::KB, ..BytesNotification::default() })]
#[case("420 m", BytesNotification {
#[case("420m", BytesNotification {
previous_threshold: 420_000_000, threshold: Some(420_000_000),byte_multiple: ByteMultiple::MB, ..BytesNotification::default() })]
#[case("foob@r", BytesNotification{
threshold: Some(800000),
..Default::default()
})]
#[case(" 888 g", BytesNotification {
#[case("744Ь", BytesNotification {
previous_threshold: 744, threshold: Some(744),byte_multiple: ByteMultiple::B, ..BytesNotification::default() })]
#[case("888g", BytesNotification {
previous_threshold: 888_000_000_000, threshold: Some(888_000_000_000),byte_multiple: ByteMultiple::GB, ..BytesNotification::default() })]
fn test_can_instantiate_bytes_notification_from_string(
#[case] input: &str,