From 1796f08e587fa8ecc64a133e827c9161b2181fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 14 Nov 2021 15:51:59 +0100 Subject: [PATCH] pkstrings: Split Invalid errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- pkstrings/src/pkstring.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkstrings/src/pkstring.rs b/pkstrings/src/pkstring.rs index 82b6a48..f466e4f 100644 --- a/pkstrings/src/pkstring.rs +++ b/pkstrings/src/pkstring.rs @@ -88,13 +88,15 @@ const fn hextostr(hex: u8) -> Option { #[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 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 for PKString { fn try_from(data: String) -> Result { for chr in data.chars() { if strtohex(&chr).is_none() { - return Err(Error::InvalidCharacter); + return Err(Error::InvalidCharacter(chr)); } }