From 25ed4f0acb4d80a5e638b4611872b022016b1feb Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Wed, 1 May 2019 22:47:11 -0400 Subject: [PATCH] made helper functions for EndpointStatus --- src/handlers.rs | 29 +++++++++++++++++++++++++++++ src/updater.rs | 23 ++++++----------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/handlers.rs b/src/handlers.rs index 2925882..59f8083 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -15,6 +15,35 @@ pub struct EndpointStatus { pub error: Option, } +impl EndpointStatus { + pub fn ok(location: String, endpoint: String) -> Self { + EndpointStatus { + status: 0, + location, + endpoint, + error: None, + } + } + + pub fn warn(location: String, endpoint: String, error: Option) -> Self { + EndpointStatus { + status: 1, + location, + endpoint, + error, + } + } + + pub fn error(location: String, endpoint: String, error: Option) -> Self { + EndpointStatus { + status: 2, + location, + endpoint, + error, + } + } +} + #[derive(Serialize, Debug)] pub struct StatusGroup { pub label: String, diff --git a/src/updater.rs b/src/updater.rs index 423dc1b..42838e1 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -77,28 +77,17 @@ fn get_result( }); } - EndpointStatus { - status: if error.is_some() { 1 } else { 0 }, - location: url, - endpoint: label, - error, + if error.is_some() { + EndpointStatus::warn(url, label, error) + } else { + EndpointStatus::ok(url, label) } } Err(e) => { if let Some(true) = endpoint.should_err { - EndpointStatus { - status: 0, - location: url, - endpoint: label, - error: None, - } + EndpointStatus::ok(url, label) } else { - EndpointStatus { - status: 2, - location: url, - endpoint: label, - error: Some(format!("{}", e)), - } + EndpointStatus::error(url, label, Some(format!("{}", e))) } } }