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.
This commit is contained in:
Robin Krahl 2020-10-06 19:43:06 +02:00
parent e222a1f443
commit 8e8f7728e2
No known key found for this signature in database
GPG Key ID: 8E9B0870524F69D8

View File

@ -79,6 +79,14 @@ impl HtmlView {
self.rendered_html = Some(rendered_html); self.rendered_html = Some(rendered_html);
size 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 { impl cursive::View for HtmlView {
@ -137,20 +145,14 @@ impl cursive::View for HtmlView {
fn on_event(&mut self, event: event::Event) -> event::EventResult { fn on_event(&mut self, event: event::Event) -> event::EventResult {
use event::{Event, EventResult, Key}; use event::{Event, EventResult, Key};
let links = if let Some(rendered_html) = &self.rendered_html { let (focus, links) = if let Some(val) = self.focus_and_links() {
if rendered_html.links.is_empty() { val
return EventResult::Ignored;
} else {
&rendered_html.links
}
} else { } else {
return EventResult::Ignored; return EventResult::Ignored;
}; };
let focus = if let Some(focus) = self.focus { if links.is_empty() {
focus
} else {
return EventResult::Ignored; return EventResult::Ignored;
}; }
match event { match event {
Event::Key(Key::Left) => { Event::Key(Key::Left) => {
@ -218,9 +220,9 @@ impl cursive::View for HtmlView {
} }
fn important_area(&self, _: cursive::XY<usize>) -> cursive::Rect { fn important_area(&self, _: cursive::XY<usize>) -> cursive::Rect {
if let Some((focus, rendered_html)) = self.focus.zip(self.rendered_html.as_ref()) { if let Some((focus, links)) = self.focus_and_links() {
let origin = rendered_html.links[focus].position; let origin = links[focus].position;
cursive::Rect::from_size(origin, (rendered_html.links[focus].width, 1)) cursive::Rect::from_size(origin, (links[focus].width, 1))
} else { } else {
cursive::Rect::from((0, 0)) cursive::Rect::from((0, 0))
} }