From 8e8f7728e2a1494db0985b381d7c9786083d3c0e Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 6 Oct 2020 19:43:06 +0200 Subject: [PATCH] Replace Option::zip with custom function In the previous commit, we used the Option::zip method to make querying the focus and the links in the HtmlView struct easier. But Option::zip has only been added in Rust 1.46, so we replace it with a custom utility function to keep compatibility with Rust 1.40. --- src/viewer/tui/views.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/viewer/tui/views.rs b/src/viewer/tui/views.rs index 7d588aa..a310784 100644 --- a/src/viewer/tui/views.rs +++ b/src/viewer/tui/views.rs @@ -79,6 +79,14 @@ impl HtmlView { self.rendered_html = Some(rendered_html); size } + + /// Returns the current focus and the list of links if both are available. + fn focus_and_links(&self) -> Option<(usize, &[Link])> { + match (self.focus, self.rendered_html.as_ref().map(|h| &h.links)) { + (Some(focus), Some(links)) => Some((focus, links)), + _ => None, + } + } } impl cursive::View for HtmlView { @@ -137,20 +145,14 @@ impl cursive::View for HtmlView { fn on_event(&mut self, event: event::Event) -> event::EventResult { use event::{Event, EventResult, Key}; - let links = if let Some(rendered_html) = &self.rendered_html { - if rendered_html.links.is_empty() { - return EventResult::Ignored; - } else { - &rendered_html.links - } + let (focus, links) = if let Some(val) = self.focus_and_links() { + val } else { return EventResult::Ignored; }; - let focus = if let Some(focus) = self.focus { - focus - } else { + if links.is_empty() { return EventResult::Ignored; - }; + } match event { Event::Key(Key::Left) => { @@ -218,9 +220,9 @@ impl cursive::View for HtmlView { } fn important_area(&self, _: cursive::XY) -> cursive::Rect { - if let Some((focus, rendered_html)) = self.focus.zip(self.rendered_html.as_ref()) { - let origin = rendered_html.links[focus].position; - cursive::Rect::from_size(origin, (rendered_html.links[focus].width, 1)) + if let Some((focus, links)) = self.focus_and_links() { + let origin = links[focus].position; + cursive::Rect::from_size(origin, (links[focus].width, 1)) } else { cursive::Rect::from((0, 0)) }