diff options
-rw-r--r-- | src/call/nameserver.rs | 102 | ||||
-rw-r--r-- | src/common/mod.rs | 76 | ||||
-rw-r--r-- | src/common/nameserver.rs | 183 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/response/nameserver.rs | 156 |
5 files changed, 263 insertions, 255 deletions
diff --git a/src/call/nameserver.rs b/src/call/nameserver.rs index fa26d41..884edab 100644 --- a/src/call/nameserver.rs +++ b/src/call/nameserver.rs @@ -1,107 +1,7 @@ use super::Call; -use crate::response::nameserver::UrlRdrType; -use crate::{Error, Result}; +use crate::common::nameserver::{RecordType, UrlRdrType}; use std::collections::BTreeMap; -use std::fmt; - -/// The DNS record type. -#[derive(Clone, Debug)] -pub enum RecordType { - A, - Aaaa, - Afsdb, - Alias, - Caa, - Cert, - Cname, - Hinfo, - Key, - Loc, - Mx, - NaPtr, - Ns, - OpenPgpKey, - Ptr, - Rp, - SmimeA, - Soa, - Srv, - Sshfp, - Tlsa, - Txt, - Uri, - Url, -} - -impl fmt::Display for RecordType { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - RecordType::A => write!(fmt, "A"), - RecordType::Aaaa => write!(fmt, "AAAA"), - RecordType::Afsdb => write!(fmt, "AFSDB"), - RecordType::Alias => write!(fmt, "ALIAS"), - RecordType::Caa => write!(fmt, "CAA"), - RecordType::Cert => write!(fmt, "CERT"), - RecordType::Cname => write!(fmt, "CNAME"), - RecordType::Hinfo => write!(fmt, "HINFO"), - RecordType::Key => write!(fmt, "KEY"), - RecordType::Loc => write!(fmt, "LOC"), - RecordType::Mx => write!(fmt, "MX"), - RecordType::NaPtr => write!(fmt, "NAPTR"), - RecordType::Ns => write!(fmt, "NS"), - RecordType::OpenPgpKey => write!(fmt, "OPENPGPKEY"), - RecordType::Ptr => write!(fmt, "PTR"), - RecordType::Rp => write!(fmt, "RP"), - RecordType::SmimeA => write!(fmt, "SMIMEA"), - RecordType::Soa => write!(fmt, "SOA"), - RecordType::Srv => write!(fmt, "SRV"), - RecordType::Sshfp => write!(fmt, "SSHFP"), - RecordType::Tlsa => write!(fmt, "TLSA"), - RecordType::Txt => write!(fmt, "TXT"), - RecordType::Uri => write!(fmt, "URI"), - RecordType::Url => write!(fmt, "URL"), - } - } -} - -impl From<RecordType> for xmlrpc::Value { - fn from(rt: RecordType) -> Self { - xmlrpc::Value::String(rt.to_string()) - } -} - -impl TryFrom<String> for RecordType { - type Error = Error; - fn try_from(s: String) -> Result<Self> { - match s.as_str() { - "A" => Ok(Self::A), - "AAAA" => Ok(Self::Aaaa), - "AFSDB" => Ok(Self::Afsdb), - "ALIAS" => Ok(Self::Alias), - "CAA" => Ok(Self::Caa), - "CERT" => Ok(Self::Cert), - "CNAME" => Ok(Self::Cname), - "HINFO" => Ok(Self::Hinfo), - "KEY" => Ok(Self::Key), - "LOC" => Ok(Self::Loc), - "MX" => Ok(Self::Mx), - "NAPTR" => Ok(Self::NaPtr), - "NS" => Ok(Self::Ns), - "OPENPGPKEY" => Ok(Self::OpenPgpKey), - "PTR" => Ok(Self::Ptr), - "RP" => Ok(Self::Rp), - "SMIMEA" => Ok(Self::SmimeA), - "SOA" => Ok(Self::Soa), - "SSHFP" => Ok(Self::Sshfp), - "TLSA" => Ok(Self::Tlsa), - "TXT" => Ok(Self::Txt), - "URI" => Ok(Self::Uri), - "URL" => Ok(Self::Url), - _ => Err(Error::BadVariant("RecordType", s)), - } - } -} /// Optional search constraints to find nameserver records /// the account has access to. diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 0000000..ee54eed --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1,76 @@ +use crate::{Error, Result}; + +use std::collections::BTreeMap; + +use iso8601::DateTime; + +pub(crate) fn get_str(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<String> { + let value = map + .get(key) + .ok_or(Error::Inexistent(key))? + .as_str() + .ok_or_else(|| Error::Type(key, "String", map.get(key).unwrap().clone()))?; + + Ok(value.to_owned()) +} + +pub(crate) fn get_i32(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<i32> { + let value = map + .get(key) + .ok_or(Error::Inexistent(key))? + .as_i32() + .ok_or_else(|| Error::Type(key, "Int", map.get(key).unwrap().clone()))?; + + Ok(value) +} + +pub(crate) fn get_bool(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<bool> { + let value = map + .get(key) + .ok_or(Error::Inexistent(key))? + .as_bool() + .ok_or_else(|| Error::Type(key, "Bool", map.get(key).unwrap().clone()))?; + + Ok(value) +} + +pub(crate) fn get_datetime( + map: &BTreeMap<String, xmlrpc::Value>, + key: &'static str, +) -> Result<DateTime> { + let value = map + .get(key) + .ok_or(Error::Inexistent(key))? + .as_datetime() + .ok_or_else(|| Error::Type(key, "DateTime", map.get(key).unwrap().clone()))?; + + Ok(value) +} + +pub(crate) fn get_array( + map: &BTreeMap<String, xmlrpc::Value>, + key: &'static str, +) -> Result<Vec<xmlrpc::Value>> { + let value = map + .get(key) + .ok_or(Error::Inexistent(key))? + .as_array() + .ok_or_else(|| Error::Type(key, "Array", map.get(key).unwrap().clone()))?; + + Ok(value.to_vec()) +} + +pub(crate) fn get_map( + map: &BTreeMap<String, xmlrpc::Value>, + key: &'static str, +) -> Result<BTreeMap<String, xmlrpc::Value>> { + let value = map + .get(key) + .ok_or(Error::Inexistent(key))? + .as_struct() + .ok_or_else(|| Error::Type(key, "Struct", map.get(key).unwrap().clone()))?; + + Ok(value.to_owned()) +} + +pub mod nameserver; diff --git a/src/common/nameserver.rs b/src/common/nameserver.rs new file mode 100644 index 0000000..9b8c797 --- /dev/null +++ b/src/common/nameserver.rs @@ -0,0 +1,183 @@ +use super::*; +use crate::{Error, Result}; + +use std::fmt; + +/// The DNS record type. +#[derive(Clone, Debug)] +pub enum RecordType { + A, + Aaaa, + Afsdb, + Alias, + Caa, + Cert, + Cname, + Hinfo, + Key, + Loc, + Mx, + NaPtr, + Ns, + OpenPgpKey, + Ptr, + Rp, + SmimeA, + Soa, + Srv, + Sshfp, + Tlsa, + Txt, + Uri, + Url, +} + +impl fmt::Display for RecordType { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::A => write!(fmt, "A"), + Self::Aaaa => write!(fmt, "AAAA"), + Self::Afsdb => write!(fmt, "AFSDB"), + Self::Alias => write!(fmt, "ALIAS"), + Self::Caa => write!(fmt, "CAA"), + Self::Cert => write!(fmt, "CERT"), + Self::Cname => write!(fmt, "CNAME"), + Self::Hinfo => write!(fmt, "HINFO"), + Self::Key => write!(fmt, "KEY"), + Self::Loc => write!(fmt, "LOC"), + Self::Mx => write!(fmt, "MX"), + Self::NaPtr => write!(fmt, "NAPTR"), + Self::Ns => write!(fmt, "NS"), + Self::OpenPgpKey => write!(fmt, "OPENPGPKEY"), + Self::Ptr => write!(fmt, "PTR"), + Self::Rp => write!(fmt, "RP"), + Self::SmimeA => write!(fmt, "SMIMEA"), + Self::Soa => write!(fmt, "SOA"), + Self::Srv => write!(fmt, "SRV"), + Self::Sshfp => write!(fmt, "SSHFP"), + Self::Tlsa => write!(fmt, "TLSA"), + Self::Txt => write!(fmt, "TXT"), + Self::Uri => write!(fmt, "URI"), + Self::Url => write!(fmt, "URL"), + } + } +} + +impl From<RecordType> for xmlrpc::Value { + fn from(rt: RecordType) -> Self { + xmlrpc::Value::String(rt.to_string()) + } +} + +impl TryFrom<String> for RecordType { + type Error = Error; + fn try_from(s: String) -> Result<Self> { + match s.as_str() { + "A" => Ok(Self::A), + "AAAA" => Ok(Self::Aaaa), + "AFSDB" => Ok(Self::Afsdb), + "ALIAS" => Ok(Self::Alias), + "CAA" => Ok(Self::Caa), + "CERT" => Ok(Self::Cert), + "CNAME" => Ok(Self::Cname), + "HINFO" => Ok(Self::Hinfo), + "KEY" => Ok(Self::Key), + "LOC" => Ok(Self::Loc), + "MX" => Ok(Self::Mx), + "NAPTR" => Ok(Self::NaPtr), + "NS" => Ok(Self::Ns), + "OPENPGPKEY" => Ok(Self::OpenPgpKey), + "PTR" => Ok(Self::Ptr), + "RP" => Ok(Self::Rp), + "SMIMEA" => Ok(Self::SmimeA), + "SOA" => Ok(Self::Soa), + "SRV" => Ok(Self::Srv), + "SSHFP" => Ok(Self::Sshfp), + "TLSA" => Ok(Self::Tlsa), + "TXT" => Ok(Self::Txt), + "URI" => Ok(Self::Uri), + "URL" => Ok(Self::Url), + _ => Err(Error::BadVariant("RecordType", s)), + } + } +} + +/// The domain type. Can be master or slave. +#[derive(Clone, Debug)] +pub enum DomainType { + Master, + Slave, +} + +impl fmt::Display for DomainType { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(match self { + Self::Master => "MASTER", + Self::Slave => "SLAVE", + }) + } +} + +impl TryFrom<String> for DomainType { + type Error = Error; + fn try_from(s: String) -> Result<Self> { + match s.as_str() { + "MASTER" => Ok(Self::Master), + "SLAVE" => Ok(Self::Slave), + _ => Err(Error::BadVariant("DomainType", s)), + } + } +} + +/// Information on a slave nameserver. +#[derive(Clone, Debug)] +pub struct SlaveDns { + pub hostname: String, + pub address: String, +} + +impl TryFrom<BTreeMap<String, xmlrpc::Value>> for SlaveDns { + type Error = Error; + fn try_from(map: BTreeMap<String, xmlrpc::Value>) -> Result<Self> { + Ok(Self { + hostname: get_str(&map, "name")?, + address: get_str(&map, "ip")?, + }) + } +} + +/// Type of URL redirect. Can be a HTTP 301, 302 or frame. +#[derive(Clone, Debug)] +pub enum UrlRdrType { + Permanent, + Temporary, + Frame, +} + +impl fmt::Display for UrlRdrType { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(match self { + Self::Permanent => "HEADER301", + Self::Temporary => "HEADER302", + Self::Frame => "FRAME", + }) + } +} + +impl TryFrom<String> for UrlRdrType { + type Error = Error; + fn try_from(s: String) -> Result<Self> { + match s.as_str() { + "HEADER301" => Ok(Self::Permanent), + "HEADER302" => Ok(Self::Temporary), + "FRAME" => Ok(Self::Frame), + _ => Err(Error::BadVariant("UrlRdrType", s)), + } + } +} + +impl From<UrlRdrType> for xmlrpc::Value { + fn from(url_rdr_type: UrlRdrType) -> Self { + url_rdr_type.to_string().into() + } +} @@ -1,5 +1,6 @@ pub mod call; pub mod client; +pub mod common; pub mod error; pub mod response; diff --git a/src/response/nameserver.rs b/src/response/nameserver.rs index a6ab226..24e341e 100644 --- a/src/response/nameserver.rs +++ b/src/response/nameserver.rs @@ -1,162 +1,10 @@ use super::Response; -use crate::call::nameserver::RecordType; +use crate::common::nameserver::{DomainType, RecordType, SlaveDns, UrlRdrType}; +use crate::common::*; use crate::{Error, Result}; -use std::collections::BTreeMap; -use std::fmt; - use iso8601::DateTime; -fn get_str(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<String> { - let value = map - .get(key) - .ok_or(Error::Inexistent(key))? - .as_str() - .ok_or_else(|| Error::Type(key, "String", map.get(key).unwrap().clone()))?; - - Ok(value.to_owned()) -} - -fn get_i32(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<i32> { - let value = map - .get(key) - .ok_or(Error::Inexistent(key))? - .as_i32() - .ok_or_else(|| Error::Type(key, "Int", map.get(key).unwrap().clone()))?; - - Ok(value) -} - -fn get_bool(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<bool> { - let value = map - .get(key) - .ok_or(Error::Inexistent(key))? - .as_bool() - .ok_or_else(|| Error::Type(key, "Bool", map.get(key).unwrap().clone()))?; - - Ok(value) -} - -fn get_datetime(map: &BTreeMap<String, xmlrpc::Value>, key: &'static str) -> Result<DateTime> { - let value = map - .get(key) - .ok_or(Error::Inexistent(key))? - .as_datetime() - .ok_or_else(|| Error::Type(key, "DateTime", map.get(key).unwrap().clone()))?; - - Ok(value) -} - -fn get_array( - map: &BTreeMap<String, xmlrpc::Value>, - key: &'static str, -) -> Result<Vec<xmlrpc::Value>> { - let value = map - .get(key) - .ok_or(Error::Inexistent(key))? - .as_array() - .ok_or_else(|| Error::Type(key, "Array", map.get(key).unwrap().clone()))?; - - Ok(value.to_vec()) -} - -fn get_map( - map: &BTreeMap<String, xmlrpc::Value>, - key: &'static str, -) -> Result<BTreeMap<String, xmlrpc::Value>> { - let value = map - .get(key) - .ok_or(Error::Inexistent(key))? - .as_struct() - .ok_or_else(|| Error::Type(key, "Struct", map.get(key).unwrap().clone()))?; - - Ok(value.to_owned()) -} - -/// The domain type. Can be master or slave. -#[derive(Clone, Debug)] -pub enum DomainType { - Master, - Slave, -} - -impl fmt::Display for DomainType { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.write_str(match self { - Self::Master => "MASTER", - Self::Slave => "SLAVE", - }) - } -} - -impl TryFrom<String> for DomainType { - type Error = Error; - fn try_from(s: String) -> Result<Self> { - match s.as_str() { - "MASTER" => Ok(Self::Master), - "SLAVE" => Ok(Self::Slave), - _ => Err(Error::BadVariant("DomainType", s)), - } - } -} - -/// Information on a slave nameserver. -#[derive(Clone, Debug)] -pub struct SlaveDns { - pub hostname: String, - pub address: String, -} - -impl TryFrom<BTreeMap<String, xmlrpc::Value>> for SlaveDns { - type Error = Error; - fn try_from(map: BTreeMap<String, xmlrpc::Value>) -> Result<Self> { - let slave = Self { - hostname: get_str(&map, "name")?, - address: get_str(&map, "ip")?, - }; - - Ok(slave) - } -} - -/// Type of URL redirect. Can be a HTTP 301, 302 or frame. -#[derive(Clone, Debug)] -pub enum UrlRdrType { - Permanent, - Temporary, - Frame, -} - -impl fmt::Display for UrlRdrType { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let url_rdr_type = match self { - Self::Permanent => "HEADER301", - Self::Temporary => "HEADER302", - Self::Frame => "FRAME", - }; - - fmt.write_str(url_rdr_type) - } -} - -impl TryFrom<String> for UrlRdrType { - type Error = Error; - fn try_from(s: String) -> Result<Self> { - match s.as_str() { - "HEADER301" => Ok(Self::Permanent), - "HEADER302" => Ok(Self::Temporary), - "FRAME" => Ok(Self::Frame), - _ => Err(Error::BadVariant("UrlRdrType", s)), - } - } -} - -impl From<UrlRdrType> for xmlrpc::Value { - fn from(url_rdr_type: UrlRdrType) -> Self { - url_rdr_type.to_string().into() - } -} - /// A nameserver record. Contains DNS information as well as INWX metadata. #[derive(Clone, Debug)] pub struct Record { |