Public Key try_from_str

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2023-02-08 23:41:03 +01:00
parent 342a58272c
commit 2c00e57bc6
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
1 changed files with 20 additions and 5 deletions

View File

@ -68,15 +68,30 @@ impl PublicKeyType {
} }
} }
impl TryFrom<&str> for PublicKeyType {
type Error = ::std::io::Error;
fn try_from(raw: &str) -> Result<Self, Self::Error> {
if let Ok(type_u8) = raw.parse::<u8>() {
if let Some(kind) = PublicKeyType::from_u8(type_u8) {
return Ok(kind);
}
}
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Public Key Type 0 is the only one supported",
));
}
}
/// Public Key, with its type and id /// Public Key, with its type and id
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PublicKey { pub struct PublicKey {
/// public key raw data /// public key raw data
raw: Vec<u8>, pub raw: Vec<u8>,
/// type of public key /// type of public key
kind: PublicKeyType, pub kind: PublicKeyType,
/// id of public key /// id of public key
id: PublicKeyId, pub id: PublicKeyId,
} }
impl PublicKey { impl PublicKey {
@ -334,10 +349,10 @@ impl Address {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Record { pub struct Record {
/// Public keys used by any authentication server /// Public keys used by any authentication server
public_keys: Vec<PublicKey>, pub public_keys: Vec<PublicKey>,
/// List of all authentication servers' addresses. /// List of all authentication servers' addresses.
/// Multiple ones can point to the same authentication server /// Multiple ones can point to the same authentication server
addresses: Vec<Address>, pub addresses: Vec<Address>,
} }
impl Record { impl Record {