Allow redirects to have 0 links
This commit is contained in:
parent
2e6539cbc5
commit
8bb94b1847
2 changed files with 23 additions and 8 deletions
|
|
@ -18,12 +18,14 @@ fn find_index_of_title(pages: &[Page<PageInfo>], title: &str) -> u32 {
|
|||
|
||||
fn resolve_redirects(data: &AdjacencyList<PageInfo, LinkInfo>, 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -215,15 +215,18 @@ impl AdjacencyList<PageInfo, LinkInfo> {
|
|||
}
|
||||
}
|
||||
|
||||
// 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<P, L> AdjacencyList<P, L> {
|
|||
start_idx..end_idx
|
||||
}
|
||||
|
||||
pub fn link_redirect(&self, page_idx: u32) -> Option<u32> {
|
||||
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<L> {
|
||||
&self.links[idx as usize]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue