fix(stability): avoid link handler panic on bad index (#1356)

root cause needs investigating, but for now fix the crash.
This commit is contained in:
Thomas Linford 2022-04-28 11:45:21 +02:00 committed by GitHub
parent 88e63e5689
commit 188febfc05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,17 +50,31 @@ impl LinkHandler {
}
pub fn output_osc8(&self, link_anchor: Option<LinkAnchor>) -> Option<String> {
link_anchor.map(|link| match link {
link_anchor
.map(|link| match link {
LinkAnchor::Start(index) => {
let link = self.links.get(&index).unwrap();
let link = self.links.get(&index);
let output = link.map(|link| {
let id = link
.id
.as_ref()
.map_or("".to_string(), |id| format!("id={}", id));
format!("\u{1b}]8;{};{}{}", id, link.uri, TERMINATOR)
});
if output.is_none() {
log::warn!(
"attempted to output osc8 link start, but id: {} was not found!",
index
);
}
LinkAnchor::End => format!("\u{1b}]8;;{}", TERMINATOR),
output
}
LinkAnchor::End => Some(format!("\u{1b}]8;;{}", TERMINATOR)),
})
.flatten()
}
}
@ -108,4 +122,11 @@ mod tests {
let expected = format!("\u{1b}]8;;{}", TERMINATOR);
assert_eq!(link_handler.output_osc8(anchor).unwrap(), expected);
}
#[test]
fn return_none_on_missing_link_id() {
let link_handler = LinkHandler::default();
let anchor = LinkAnchor::Start(100);
assert_eq!(link_handler.output_osc8(Some(anchor)), None);
}
}