pkstring: implement TryFrom<String> and TryFrom<&str>

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2020-06-30 00:02:56 +02:00
parent be04cdcaa1
commit f906a69aaa
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -135,3 +135,31 @@ impl Into<Vec<u8>> for PKString {
vec
}
}
impl TryFrom<&str> for PKString {
type Error = Error;
fn try_from(data: &str) -> Result<PKString, Error> {
for chr in data.chars() {
if ! STRTOHEX.contains_key(&chr) {
return Err(Error::InvalidCharacter);
}
}
Ok(PKString(String::from(data)))
}
}
impl TryFrom<String> for PKString {
type Error = Error;
fn try_from(data: String) -> Result<PKString, Error> {
for chr in data.chars() {
if ! STRTOHEX.contains_key(&chr) {
return Err(Error::InvalidCharacter);
}
}
Ok(PKString(data))
}
}