pkstring: implement TryFrom<String> and TryFrom<&str>
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
be04cdcaa1
commit
f906a69aaa
1 changed files with 28 additions and 0 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue