frontend-utils: Do not panic when recent limit is zero and file is empty

This commit is contained in:
sleepycatcoding 2024-05-09 19:29:03 +03:00
parent 4e39598da4
commit 18f86634a4
1 changed files with 21 additions and 0 deletions

View File

@ -26,6 +26,11 @@ impl<'a> RecentsWriter<'a> {
/// Pushes a new recent entry on the entry stack, if same entry already exists, it will get moved to the top.
pub fn push(&mut self, recent: Recent, limit: usize) {
if limit == 0 {
// Do not even bother.
return;
}
self.with_underlying_table(|values, array| {
// First, lets check if we already have existing entry with the same URL and move it to the top.
let existing = values.iter().position(|x| x.url == recent.url);
@ -109,4 +114,20 @@ mod tests {
fn clear() {
test("[[recent]]\nurl = \"file:///file_one.swf\"\n[[recent]]\nurl = \"file:///file_two.swf\"\n[[recent]]\nurl = \"file:///3.swf\"\n", |writer| writer.clear(), "");
}
#[test]
fn zero_limit() {
test(
"",
|writer| {
writer.push(
Recent {
url: Url::parse("file:///no_crash.swf").unwrap(),
},
0,
)
},
"",
);
}
}