1
Fork 0

Bring char along with InvalidDigit

This commit is contained in:
Ethan Brierley 2020-10-06 19:05:25 +01:00
parent c027844795
commit 83d294f06a
5 changed files with 12 additions and 12 deletions

View file

@ -52,7 +52,7 @@ fn update_limit(
IntErrorKind::Empty | IntErrorKind::OnlySign => { IntErrorKind::Empty | IntErrorKind::OnlySign => {
"`limit` must be a non-negative integer" "`limit` must be a non-negative integer"
} }
IntErrorKind::InvalidDigit => "not a valid integer", IntErrorKind::InvalidDigit(_) => "not a valid integer",
IntErrorKind::NegOverflow => bug!("`limit` should never underflow"), IntErrorKind::NegOverflow => bug!("`limit` should never underflow"),
IntErrorKind::Zero => bug!("zero is a valid `limit`"), IntErrorKind::Zero => bug!("zero is a valid `limit`"),
kind => bug!("unimplemented IntErrorKind variant: {:?}", kind), kind => bug!("unimplemented IntErrorKind variant: {:?}", kind),

View file

@ -92,12 +92,12 @@ pub enum IntErrorKind {
/// Among other causes, this variant will be constructed when parsing an empty string. /// Among other causes, this variant will be constructed when parsing an empty string.
#[stable(feature = "int_error_matching", since = "1.47.0")] #[stable(feature = "int_error_matching", since = "1.47.0")]
Empty, Empty,
/// Contains an invalid digit. /// Contains an digit invalid in its context.
/// ///
/// Among other causes, this variant will be constructed when parsing a string that /// Among other causes, this variant will be constructed when parsing a string that
/// contains a letter. /// contains a letter.
#[stable(feature = "int_error_matching", since = "1.47.0")] #[stable(feature = "int_error_matching", since = "1.47.0")]
InvalidDigit, InvalidDigit(#[stable(feature = "int_error_matching", since = "1.47.0")] char),
/// Integer is too large to store in target integer type. /// Integer is too large to store in target integer type.
#[stable(feature = "int_error_matching", since = "1.47.0")] #[stable(feature = "int_error_matching", since = "1.47.0")]
PosOverflow, PosOverflow,
@ -131,7 +131,7 @@ impl ParseIntError {
pub fn __description(&self) -> &str { pub fn __description(&self) -> &str {
match self.kind { match self.kind {
IntErrorKind::Empty => "cannot parse integer from empty string", IntErrorKind::Empty => "cannot parse integer from empty string",
IntErrorKind::InvalidDigit => "invalid digit found in string", IntErrorKind::InvalidDigit(_) => "invalid digit found in string",
IntErrorKind::PosOverflow => "number too large to fit in target type", IntErrorKind::PosOverflow => "number too large to fit in target type",
IntErrorKind::NegOverflow => "number too small to fit in target type", IntErrorKind::NegOverflow => "number too small to fit in target type",
IntErrorKind::Zero => "number would be zero for non-zero type", IntErrorKind::Zero => "number would be zero for non-zero type",

View file

@ -845,7 +845,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
for &c in digits { for &c in digits {
let x = match (c as char).to_digit(radix) { let x = match (c as char).to_digit(radix) {
Some(x) => x, Some(x) => x,
None => return Err(PIE { kind: InvalidDigit }), None => return Err(PIE { kind: InvalidDigit(c as char) }),
}; };
result = match result.checked_mul(radix) { result = match result.checked_mul(radix) {
Some(result) => result, Some(result) => result,
@ -861,7 +861,7 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32) -> Result<T, Par
for &c in digits { for &c in digits {
let x = match (c as char).to_digit(radix) { let x = match (c as char).to_digit(radix) {
Some(x) => x, Some(x) => x,
None => return Err(PIE { kind: InvalidDigit }), None => return Err(PIE { kind: InvalidDigit(c as char) }),
}; };
result = match result.checked_mul(radix) { result = match result.checked_mul(radix) {
Some(result) => result, Some(result) => result,

View file

@ -131,7 +131,7 @@ fn test_from_str() {
assert_eq!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero)); assert_eq!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero));
assert_eq!( assert_eq!(
"-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
Some(IntErrorKind::InvalidDigit) Some(IntErrorKind::InvalidDigit('-'))
); );
assert_eq!( assert_eq!(
"-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()), "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),

View file

@ -117,11 +117,11 @@ fn test_leading_plus() {
#[test] #[test]
fn test_invalid() { fn test_invalid() {
test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit)); test_parse::<i8>("--129", Err(IntErrorKind::InvalidDigit('-')));
test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit)); test_parse::<i8>("++129", Err(IntErrorKind::InvalidDigit('+')));
test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit)); test_parse::<u8>("Съешь", Err(IntErrorKind::InvalidDigit('Ð')));
// is this the correct error here. Maybe need a reapeat sign error here test_parse::<u8>("123Hello", Err(IntErrorKind::InvalidDigit('H')));
test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit)); test_parse::<i8>("--", Err(IntErrorKind::InvalidDigit('-')));
} }
#[test] #[test]