Decoding from string for Address

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2023-02-08 22:48:58 +01:00
parent 3797ca869d
commit 342a58272c
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
1 changed files with 57 additions and 8 deletions

View File

@ -35,6 +35,19 @@ use ::std::{net::IpAddr, vec::Vec};
#[derive(Debug, Copy, Clone)]
pub struct PublicKeyId(u8);
impl TryFrom<&str> for PublicKeyId {
type Error = ::std::io::Error;
fn try_from(raw: &str) -> Result<Self, Self::Error> {
if let Ok(id_u8) = raw.parse::<u8>() {
return Ok(PublicKeyId(id_u8));
}
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Public Key ID must be between 0 and 256",
));
}
}
/// Public Key Type
#[derive(::num_derive::FromPrimitive, Debug, Copy, Clone)]
// public enum: use non_exhaustive to force users to add a default case
@ -115,21 +128,40 @@ impl PublicKey {
#[repr(u8)]
pub enum AddressPriority {
/// Initially contact addresses in this priority
P0 = 0,
P1 = 0,
/// First failover
P1,
/// Second failover
P2,
/// Third failover
/// Second failover
P3,
/// Fourth failover
/// Third failover
P4,
/// Fifth failover
/// Fourth failover
P5,
/// Sisth failover
/// Fifth failover
P6,
/// Seventh failover
/// Sisth failover
P7,
/// Seventh failover
P8,
}
impl TryFrom<&str> for AddressPriority {
type Error = ::std::io::Error;
fn try_from(raw: &str) -> Result<Self, Self::Error> {
if let Ok(priority_u8) = raw.parse::<u8>() {
if priority_u8 >= 1 {
if let Some(priority) =
AddressPriority::from_u8(priority_u8 - 1)
{
return Ok(priority);
}
}
}
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Priority must be between 1 and 8",
));
}
}
/// Inside of each group, weight of the address
@ -163,6 +195,23 @@ pub enum AddressWeight {
W8,
}
impl TryFrom<&str> for AddressWeight {
type Error = ::std::io::Error;
fn try_from(raw: &str) -> Result<Self, Self::Error> {
if let Ok(weight_u8) = raw.parse::<u8>() {
if weight_u8 >= 1 {
if let Some(weight) = AddressWeight::from_u8(weight_u8 - 1) {
return Ok(weight);
}
}
}
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Weight must be between 1 and 8",
));
}
}
/// Authentication server address information:
/// * ip
/// * udp port