From 12464c2844179cde4f8b9093236afb4d7b7f6321 Mon Sep 17 00:00:00 2001 From: Tom Parker-Shemilt Date: Tue, 10 Mar 2020 21:17:59 +0000 Subject: [PATCH] Redo (not-)working with a enum, not a pair of variables --- src/main.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index e807cec..b0cbcab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,12 +182,17 @@ fn get_url(url: String) -> BoxFuture<'static, (String, Result<(), CheckerError>) }.boxed() } +#[derive(Debug, Serialize, Deserialize)] +enum Working { + Yes, + No(String) +} + #[derive(Debug, Serialize, Deserialize)] struct Link { last_working: Option>, updated_at: DateTime, - working: bool, - message: String + working: Working, } type Results = BTreeMap; @@ -214,7 +219,7 @@ async fn main() -> Result<(), Error> { } used.insert(url.clone()); if let Some(link) = results.get(&url) { - if link.working { + if let Working::Yes = link.working { let since = Local::now() - link.updated_at; if since < min_between_checks { return; @@ -273,14 +278,12 @@ async fn main() -> Result<(), Error> { if let Some(link) = results.get_mut(&url) { link.updated_at = Local::now(); link.last_working = Some(Local::now()); - link.working = true; - link.message = String::from("") + link.working = Working::Yes; } else { results.insert(url.clone(), Link { updated_at: Local::now(), last_working: Some(Local::now()), - working: true, - message: String::from("") + working: Working::Yes }); } }, @@ -312,14 +315,12 @@ async fn main() -> Result<(), Error> { }; if let Some(link) = results.get_mut(&url) { link.updated_at = Local::now(); - link.working = false; - link.message = message; + link.working = Working::No(message); link.last_working = None; } else { results.insert(url.clone(), Link { updated_at: Local::now(), - working: false, - message: message, + working: Working::No(message), last_working: None }); } @@ -340,7 +341,7 @@ async fn main() -> Result<(), Error> { let mut failed: u32 = 0; for (_url, link) in results.iter() { - if !link.working { + if let Working::No(ref msg) = link.working { if link.last_working.is_none() { println!("{:?}", link); failed +=1; @@ -352,7 +353,7 @@ async fn main() -> Result<(), Error> { println!("{:?}", link); failed +=1; } else { - println!("Failure occurred but only {} ago, so we're not worrying yet: {}", since, link.message); + println!("Failure occurred but only {} ago, so we're not worrying yet: {}", since, msg); } } }