1
0
mirror of https://github.com/rust-unofficial/awesome-rust.git synced 2024-12-07 11:13:18 +02:00

Redo (not-)working with a enum, not a pair of variables

This commit is contained in:
Tom Parker-Shemilt 2020-03-10 21:17:59 +00:00
parent 8d45cc7292
commit 12464c2844

View File

@ -182,12 +182,17 @@ fn get_url(url: String) -> BoxFuture<'static, (String, Result<(), CheckerError>)
}.boxed() }.boxed()
} }
#[derive(Debug, Serialize, Deserialize)]
enum Working {
Yes,
No(String)
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Link { struct Link {
last_working: Option<DateTime<Local>>, last_working: Option<DateTime<Local>>,
updated_at: DateTime<Local>, updated_at: DateTime<Local>,
working: bool, working: Working,
message: String
} }
type Results = BTreeMap<String, Link>; type Results = BTreeMap<String, Link>;
@ -214,7 +219,7 @@ async fn main() -> Result<(), Error> {
} }
used.insert(url.clone()); used.insert(url.clone());
if let Some(link) = results.get(&url) { if let Some(link) = results.get(&url) {
if link.working { if let Working::Yes = link.working {
let since = Local::now() - link.updated_at; let since = Local::now() - link.updated_at;
if since < min_between_checks { if since < min_between_checks {
return; return;
@ -273,14 +278,12 @@ async fn main() -> Result<(), Error> {
if let Some(link) = results.get_mut(&url) { if let Some(link) = results.get_mut(&url) {
link.updated_at = Local::now(); link.updated_at = Local::now();
link.last_working = Some(Local::now()); link.last_working = Some(Local::now());
link.working = true; link.working = Working::Yes;
link.message = String::from("")
} else { } else {
results.insert(url.clone(), Link { results.insert(url.clone(), Link {
updated_at: Local::now(), updated_at: Local::now(),
last_working: Some(Local::now()), last_working: Some(Local::now()),
working: true, working: Working::Yes
message: String::from("")
}); });
} }
}, },
@ -312,14 +315,12 @@ async fn main() -> Result<(), Error> {
}; };
if let Some(link) = results.get_mut(&url) { if let Some(link) = results.get_mut(&url) {
link.updated_at = Local::now(); link.updated_at = Local::now();
link.working = false; link.working = Working::No(message);
link.message = message;
link.last_working = None; link.last_working = None;
} else { } else {
results.insert(url.clone(), Link { results.insert(url.clone(), Link {
updated_at: Local::now(), updated_at: Local::now(),
working: false, working: Working::No(message),
message: message,
last_working: None last_working: None
}); });
} }
@ -340,7 +341,7 @@ async fn main() -> Result<(), Error> {
let mut failed: u32 = 0; let mut failed: u32 = 0;
for (_url, link) in results.iter() { for (_url, link) in results.iter() {
if !link.working { if let Working::No(ref msg) = link.working {
if link.last_working.is_none() { if link.last_working.is_none() {
println!("{:?}", link); println!("{:?}", link);
failed +=1; failed +=1;
@ -352,7 +353,7 @@ async fn main() -> Result<(), Error> {
println!("{:?}", link); println!("{:?}", link);
failed +=1; failed +=1;
} else { } 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);
} }
} }
} }