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

Add InvalidCharError::char #124

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,29 @@ pub struct InvalidCharError {

impl InvalidCharError {
/// Returns the invalid character byte.
#[deprecated(note = "Use `InvalidCharError::char` instead.")]
pub fn invalid_char(&self) -> u8 { self.invalid }
/// Returns the position of the invalid character byte.
pub fn pos(&self) -> usize { self.pos }
/// Returns the invalid character.
///
/// # Errors
///
/// Returns the raw byte if it is not valid ascii.
pub fn char(&self) -> Result<char, u8> {
match self.invalid.is_ascii() {
true => Ok(char::from(self.invalid)),
false => Err(self.invalid),
}
}
}

impl fmt::Display for InvalidCharError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid hex char {} at pos {}", self.invalid, self.pos)
match self.char() {
Ok(c) => write!(f, "invalid hex char {} at pos {}", c, self.pos),
Err(u) => write!(f, "invalid hex char {} at pos {}", u, self.pos),
}
}
}

Expand Down
92 changes: 60 additions & 32 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,56 +53,84 @@ mod tests {
#[test]
#[cfg(feature = "alloc")]
fn hex_error() {
use crate::error::{InvalidCharError, OddLengthStringError};

let oddlen = "0123456789abcdef0";
let badchar1 = "Z123456789abcdef";
let badchar2 = "012Y456789abcdeb";
let badchar3 = "«23456789abcdef";

assert_eq!(Vec::<u8>::from_hex(oddlen), Err(OddLengthStringError { len: 17 }.into()));
match Vec::<u8>::from_hex(oddlen) {
Err(HexToBytesError::OddLengthString(e)) => assert_eq!(e.length(), 17),
v => panic!("Wrong return value: {:?}", v),
}

assert_eq!(
<[u8; 4]>::from_hex(oddlen),
Err(InvalidLengthError { invalid: 17, expected: 8 }.into())
);
assert_eq!(
Vec::<u8>::from_hex(badchar1),
Err(InvalidCharError { pos: 0, invalid: b'Z' }.into())
);
assert_eq!(
Vec::<u8>::from_hex(badchar2),
Err(InvalidCharError { pos: 3, invalid: b'Y' }.into())
);
assert_eq!(
Vec::<u8>::from_hex(badchar3),
Err(InvalidCharError { pos: 0, invalid: 194 }.into())
);

match Vec::<u8>::from_hex(badchar1) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 0);
assert_eq!(e.char(), Ok('Z'));
}
v => panic!("Wrong return value: {:?}", v),
}

match Vec::<u8>::from_hex(badchar2) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 3);
assert_eq!(e.char(), Ok('Y'));
}
v => panic!("Wrong return value: {:?}", v),
}

match Vec::<u8>::from_hex(badchar3) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 0);
assert_eq!(e.char(), Err(194));
}
v => panic!("Wrong return value: {:?}", v),
}
}

#[test]
fn hex_error_position() {
use crate::error::InvalidCharError;
let badpos1 = "Z123456789abcdef";
let badpos2 = "012Y456789abcdeb";
let badpos3 = "0123456789abcdeZ";
let badpos4 = "0123456789abYdef";

assert_eq!(
HexToBytesIter::new(badpos1).unwrap().next().unwrap(),
Err(InvalidCharError { pos: 0, invalid: b'Z' })
);
assert_eq!(
HexToBytesIter::new(badpos2).unwrap().nth(1).unwrap(),
Err(InvalidCharError { pos: 3, invalid: b'Y' })
);
assert_eq!(
HexToBytesIter::new(badpos3).unwrap().next_back().unwrap(),
Err(InvalidCharError { pos: 15, invalid: b'Z' })
);
assert_eq!(
HexToBytesIter::new(badpos4).unwrap().nth_back(1).unwrap(),
Err(InvalidCharError { pos: 12, invalid: b'Y' })
);
match Vec::<u8>::from_hex(badpos1) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 0);
assert_eq!(e.char(), Ok('Z'));
}
v => panic!("Wrong return value: {:?}", v),
};

match Vec::<u8>::from_hex(badpos2) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 3);
assert_eq!(e.char(), Ok('Y'));
}
v => panic!("Wrong return value: {:?}", v),
};

match Vec::<u8>::from_hex(badpos3) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 15);
assert_eq!(e.char(), Ok('Z'));
}
v => panic!("Wrong return value: {:?}", v),
};

match Vec::<u8>::from_hex(badpos4) {
Err(HexToBytesError::InvalidChar(e)) => {
assert_eq!(e.pos(), 12);
assert_eq!(e.char(), Ok('Y'));
}
v => panic!("Wrong return value: {:?}", v),
};
}

#[test]
Expand Down