Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ParseBigIntError to an opaque struct #37

Merged
merged 1 commit into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,12 @@ impl Num for BigUint {
}

if s.is_empty() {
// create ParseIntError::Empty
let e = u64::from_str_radix(s, radix).unwrap_err();
return Err(e.into());
return Err(ParseBigIntError::empty());
}

if s.starts_with('_') {
// Must lead with a real digit!
// create ParseIntError::InvalidDigit
let e = u64::from_str_radix(s, radix).unwrap_err();
return Err(e.into());
return Err(ParseBigIntError::invalid());
}

// First normalize all characters to plain digit values
Expand All @@ -255,11 +251,7 @@ impl Num for BigUint {
if d < radix as u8 {
v.push(d);
} else {
// create ParseIntError::InvalidDigit
// Include the previous character for context.
let i = cmp::max(v.len(), 1) - 1;
let e = u64::from_str_radix(&s[i..], radix).unwrap_err();
return Err(e.into());
return Err(ParseBigIntError::invalid());
}
}

Expand Down
49 changes: 33 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ extern crate num_integer as integer;
extern crate num_traits as traits;

use std::error::Error;
use std::num::ParseIntError;
use std::fmt;

#[cfg(target_pointer_width = "32")]
Expand All @@ -100,30 +99,48 @@ type IsizePromotion = i32;
#[cfg(target_pointer_width = "64")]
type IsizePromotion = i64;

#[derive(Debug, PartialEq)]
pub enum ParseBigIntError {
ParseInt(ParseIntError),
Other,
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseBigIntError {
kind: BigIntErrorKind,
}

impl fmt::Display for ParseBigIntError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ParseBigIntError::ParseInt(ref e) => e.fmt(f),
&ParseBigIntError::Other => "failed to parse provided string".fmt(f),
#[derive(Debug, Clone, PartialEq, Eq)]
enum BigIntErrorKind {
Empty,
InvalidDigit,
}

impl ParseBigIntError {
fn __description(&self) -> &str {
use BigIntErrorKind::*;
match self.kind {
Empty => "cannot parse integer from empty string",
InvalidDigit => "invalid digit found in string",
}
}

fn empty() -> Self {
ParseBigIntError {
kind: BigIntErrorKind::Empty,
}
}

fn invalid() -> Self {
ParseBigIntError {
kind: BigIntErrorKind::InvalidDigit,
}
}
}

impl Error for ParseBigIntError {
fn description(&self) -> &str {
"failed to parse bigint/biguint"
impl fmt::Display for ParseBigIntError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(f)
}
}

impl From<ParseIntError> for ParseBigIntError {
fn from(err: ParseIntError) -> ParseBigIntError {
ParseBigIntError::ParseInt(err)
impl Error for ParseBigIntError {
fn description(&self) -> &str {
self.__description()
}
}

Expand Down