diff options
-rw-r--r-- | src/history.rs | 24 | ||||
-rw-r--r-- | src/lib.rs | 11 |
2 files changed, 23 insertions, 12 deletions
diff --git a/src/history.rs b/src/history.rs index 5216d10..f8ffec2 100644 --- a/src/history.rs +++ b/src/history.rs @@ -13,6 +13,11 @@ pub struct History { data: Vec<Reading>, } +pub enum NoETA { + Pending, + Fuck, +} + impl History { pub fn record(&mut self, queue_size: u32) { self.data.push(Reading { @@ -21,30 +26,31 @@ impl History { }); } - pub fn completion_time(&self) -> Option<js_sys::Date> { + pub fn completion_time(&self) -> Result<js_sys::Date, NoETA> { // TODO make this not suck let &Reading { time: first_time, queue_size: first_size, .. - } = self.data.first()?; + } = self.data.first().ok_or(NoETA::Pending)?; let &Reading { time: last_time, queue_size: last_size, - } = self.data.last()?; + } = self.data.last().ok_or(NoETA::Pending)?; let overall_time_elapsed = last_time - first_time; if overall_time_elapsed.is_zero() { - return None; - } - let overall_queue_motion = first_size - last_size; - if overall_queue_motion == 0 { - return None; + return Err(NoETA::Pending); } + let overall_queue_motion = match first_size.checked_sub(last_size) { + Some(0) => return Err(NoETA::Pending), + Some(x) => x, + None => return Err(NoETA::Fuck), + }; let duration_per_step = overall_time_elapsed / overall_queue_motion as i32; let remaining_duration = duration_per_step * last_size as i32; let completion_time = last_time + remaining_duration; let completion_time_gmt = completion_time.timestamp_millis(); - Some(js_sys::Date::new(&JsValue::from( + Ok(js_sys::Date::new(&JsValue::from( completion_time_gmt as f64, ))) } @@ -13,6 +13,8 @@ use web_sys::*; mod history; mod ocr; +use history::{History, NoETA}; + // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. #[cfg(feature = "wee_alloc")] @@ -110,7 +112,7 @@ async fn do_boot() { let document = gloo::utils::document(); - let history = Rc::new(RefCell::new(history::History::default())); + let history = Rc::new(RefCell::new(History::default())); let (manual_update_tx, manual_update_rx) = mpsc::channel::<()>(5); @@ -252,7 +254,7 @@ async fn do_boot() { history.record(queue_size); let estimated_finish = history.completion_time(); let eta_text = match estimated_finish { - Some(finish) => { + Ok(finish) => { let empty_array = js_sys::Array::new(); let options = { let kv_pair = js_sys::Array::new(); @@ -271,7 +273,10 @@ async fn do_boot() { format.call1(&JsValue::UNDEFINED, &finish).unwrap(); locale_string.as_string().unwrap() } - None => format!("pending"), + Err(NoETA::Pending) => format!("pending"), + Err(NoETA::Fuck) => { + format!("fucked (try reloading i guess?)") + } }; let label = format!(" - ETA {}", eta_text); eta.set_inner_text(&label); |