From 8bb94b18470cd2e0c75001c32276d5fe11d87141 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 22 Oct 2022 19:54:08 +0200 Subject: [PATCH] Allow redirects to have 0 links --- brood/src/commands/path.rs | 12 +++++++----- brood/src/data.rs | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/brood/src/commands/path.rs b/brood/src/commands/path.rs index 53bbbad..7207f76 100644 --- a/brood/src/commands/path.rs +++ b/brood/src/commands/path.rs @@ -18,12 +18,14 @@ fn find_index_of_title(pages: &[Page], title: &str) -> u32 { fn resolve_redirects(data: &AdjacencyList, mut page_idx: u32) -> u32 { loop { - let page = &data.page(page_idx); - if page.data.redirect { - page_idx = data.link(page.link_idx).to; - } else { - break page_idx; + if data.page(page_idx).data.redirect { + if let Some(link_idx) = data.link_redirect(page_idx) { + page_idx = data.link(link_idx).to; + continue; + } } + + return page_idx; } } diff --git a/brood/src/data.rs b/brood/src/data.rs index 0ab5c78..afca0e1 100644 --- a/brood/src/data.rs +++ b/brood/src/data.rs @@ -215,15 +215,18 @@ impl AdjacencyList { } } - // Check that all redirect pages have exactly one link + // Check that all redirect pages have at most one link for page_idx in 0..self.pages.len() as u32 - 1 { let page = self.page(page_idx); if page.data.redirect { let start_idx = page.link_idx; let end_idx = self.page(page_idx + 1).link_idx; let n_links = end_idx - start_idx; - if n_links != 1 { - panic!("Redirect {:?} has {n_links} links", page.data.title); + if n_links > 1 { + panic!( + "Redirect {:?} has too many ({n_links}) links", + page.data.title + ); } } } @@ -245,6 +248,16 @@ impl AdjacencyList { start_idx..end_idx } + pub fn link_redirect(&self, page_idx: u32) -> Option { + let start_idx = self.page(page_idx).link_idx; + let end_idx = self.page(page_idx + 1).link_idx; + if start_idx == end_idx { + None + } else { + Some(start_idx) + } + } + pub fn link(&self, idx: u32) -> &Link { &self.links[idx as usize] }