From 342a58272c38c3752412eba6236e89f4ccf0be50 Mon Sep 17 00:00:00 2001 From: Luca Fulchir Date: Wed, 8 Feb 2023 22:48:58 +0100 Subject: [PATCH] Decoding from string for Address Signed-off-by: Luca Fulchir --- src/dnssec/record.rs | 65 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/dnssec/record.rs b/src/dnssec/record.rs index 5669876..c06e411 100644 --- a/src/dnssec/record.rs +++ b/src/dnssec/record.rs @@ -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 { + if let Ok(id_u8) = raw.parse::() { + 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 { + if let Ok(priority_u8) = raw.parse::() { + 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 { + if let Ok(weight_u8) = raw.parse::() { + 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