From 55d02c933abaf97b8b03d807dc8326fa36ae1a71 Mon Sep 17 00:00:00 2001 From: merkrafter Date: Wed, 10 Nov 2021 18:52:52 +0100 Subject: [PATCH] Move sign (+/-) processing into own function --- src/bin/hexyl.rs | 57 +++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index 76378df..cfe84bd 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -437,26 +437,7 @@ enum ByteOffsetParseError { fn parse_byte_offset(n: &str, block_size: PositiveI64) -> Result { use ByteOffsetParseError::*; - let (n, kind) = { - let mut chars = n.chars(); - let next_char = chars.next(); - let check_empty_after_sign = || { - if chars.clone().next().is_none() { - Err(EmptyAfterSign) - } else { - Ok(chars.as_str()) - } - }; - match next_char { - Some('+') => ( - check_empty_after_sign()?, - ByteOffsetKind::ForwardFromLastOffset, - ), - Some('-') => (check_empty_after_sign()?, ByteOffsetKind::BackwardFromEnd), - None => return Err(Empty), - _ => (n, ByteOffsetKind::ForwardFromBeginning), - } - }; + let (n, kind) = process_sign_of(n)?; let into_byte_offset = |value| { Ok(ByteOffset { @@ -546,6 +527,30 @@ fn extract_num_and_unit_from(n: &str) -> Result<(i64, Unit), ByteOffsetParseErro } } +/// Extracts a [ByteOffsetKind] based on the sign at the beginning of the given string. +/// Returns the input string without the sign (or an equal string if there wasn't any sign). +fn process_sign_of(n: &str) -> Result<(&str, ByteOffsetKind), ByteOffsetParseError> { + use ByteOffsetParseError::*; + let mut chars = n.chars(); + let next_char = chars.next(); + let check_empty_after_sign = || { + if chars.clone().next().is_none() { + Err(EmptyAfterSign) + } else { + Ok(chars.as_str()) + } + }; + match next_char { + Some('+') => Ok(( + check_empty_after_sign()?, + ByteOffsetKind::ForwardFromLastOffset, + )), + Some('-') => Ok((check_empty_after_sign()?, ByteOffsetKind::BackwardFromEnd)), + None => Err(Empty), + _ => Ok((n, ByteOffsetKind::ForwardFromBeginning)), + } +} + #[test] fn unit_multipliers() { use Unit::*; @@ -560,6 +565,18 @@ fn unit_multipliers() { assert_eq!(Tebibyte.get_multiplier(), 1024 * Gibibyte.get_multiplier()); } +#[test] +fn test_process_sign() { + use ByteOffsetKind::*; + use ByteOffsetParseError::*; + assert_eq!(process_sign_of("123"), Ok(("123", ForwardFromBeginning))); + assert_eq!(process_sign_of("+123"), Ok(("123", ForwardFromLastOffset))); + assert_eq!(process_sign_of("-123"), Ok(("123", BackwardFromEnd))); + assert_eq!(process_sign_of("-"), Err(EmptyAfterSign)); + assert_eq!(process_sign_of("+"), Err(EmptyAfterSign)); + assert_eq!(process_sign_of(""), Err(Empty)); +} + #[test] fn extract_num_and_unit() { use ByteOffsetParseError::*;