pkstrings: Split Invalid errors

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2021-11-14 15:51:59 +01:00
parent d54e39dcf6
commit 1796f08e58
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -88,13 +88,15 @@ const fn hextostr(hex: u8) -> Option<char> {
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
InvalidCharacter,
InvalidByte(u8),
InvalidCharacter(char),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
Error::InvalidCharacter => write!(f, "Invalid character"),
Error::InvalidByte(ord) => write!(f, "Invalid byte: {:?}", ord),
Error::InvalidCharacter(chr) => write!(f, "Invalid character: {:?}", chr),
}
}
}
@ -124,7 +126,7 @@ impl TryFrom<u8> for PKString {
match hextostr(ord) {
Some(chr) => buf.push(chr.clone()),
None => return Err(Error::InvalidCharacter),
None => return Err(Error::InvalidByte(ord)),
}
Ok(PKString(buf))
@ -140,7 +142,7 @@ impl TryFrom<&[u8]> for PKString {
for ord in data {
match hextostr(*ord) {
Some(chr) => buf.push(chr.clone()),
None => return Err(Error::InvalidCharacter),
None => return Err(Error::InvalidByte(*ord)),
}
}
@ -170,7 +172,7 @@ impl TryFrom<String> for PKString {
fn try_from(data: String) -> Result<PKString, Error> {
for chr in data.chars() {
if strtohex(&chr).is_none() {
return Err(Error::InvalidCharacter);
return Err(Error::InvalidCharacter(chr));
}
}