From fdec3f29fcfb6bfafc3462fce9a66b78ef3f4c55 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 27 Jan 2022 12:35:50 +0100 Subject: [PATCH] Redirect CheckResponse deserialization to improve interface --- src/common.rs | 38 ++++++++++++++++++++++++++++++++++---- src/contact/check.rs | 14 ++++---------- src/domain/check.rs | 8 ++++---- src/host/check.rs | 14 ++++---------- src/lib.rs | 4 ++-- tests/basic.rs | 2 +- 6 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/common.rs b/src/common.rs index bb09e95..973b7f2 100644 --- a/src/common.rs +++ b/src/common.rs @@ -41,7 +41,7 @@ impl Extension for NoExtension { /// Type that represents the <name> tag for host check response #[derive(Deserialize, Debug)] -pub struct Available { +struct Available { /// The resource name #[serde(rename = "$value")] pub id: StringValue<'static>, @@ -52,7 +52,7 @@ pub struct Available { /// Type that represents the <cd> tag for domain check response #[derive(Deserialize, Debug)] -pub struct CheckResponseDataItem { +struct CheckResponseDataItem { /// Data under the <name> tag #[serde(rename = "name", alias = "id")] pub resource: Available, @@ -62,7 +62,7 @@ pub struct CheckResponseDataItem { /// Type that represents the <chkData> tag for host check response #[derive(Deserialize, Debug)] -pub struct CheckData { +struct CheckData { /// Data under the <cd> tag #[serde(rename = "cd")] pub list: Vec, @@ -70,12 +70,42 @@ pub struct CheckData { /// Type that represents the <resData> tag for host check response #[derive(Deserialize, Debug)] -pub struct CheckResponse { +struct DeserializedCheckResponse { /// Data under the <chkData> tag #[serde(rename = "chkData")] pub check_data: CheckData, } +#[derive(Debug)] +pub struct Checked { + pub id: String, + pub available: bool, + pub reason: Option, +} + +#[derive(Deserialize, Debug)] +#[serde(from = "DeserializedCheckResponse")] +pub struct CheckResponse { + pub list: Vec, +} + +impl From for CheckResponse { + fn from(rsp: DeserializedCheckResponse) -> Self { + Self { + list: rsp + .check_data + .list + .into_iter() + .map(|item| Checked { + id: item.resource.id.0.into_owned(), + available: item.resource.available, + reason: item.reason.map(|r| r.0.into_owned()), + }) + .collect(), + } + } +} + /// The