pkstrings: Split Invalid errors
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
d54e39dcf6
commit
1796f08e58
1 changed files with 7 additions and 5 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue