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 {
|
fn resolve_redirects(data: &AdjacencyList<PageInfo, LinkInfo>, mut page_idx: u32) -> u32 {
|
||||||
loop {
|
loop {
|
||||||
let page = &data.page(page_idx);
|
if data.page(page_idx).data.redirect {
|
||||||
if page.data.redirect {
|
if let Some(link_idx) = data.link_redirect(page_idx) {
|
||||||
page_idx = data.link(page.link_idx).to;
|
page_idx = data.link(link_idx).to;
|
||||||
} else {
|
continue;
|
||||||
break page_idx;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
for page_idx in 0..self.pages.len() as u32 - 1 {
|
||||||
let page = self.page(page_idx);
|
let page = self.page(page_idx);
|
||||||
if page.data.redirect {
|
if page.data.redirect {
|
||||||
let start_idx = page.link_idx;
|
let start_idx = page.link_idx;
|
||||||
let end_idx = self.page(page_idx + 1).link_idx;
|
let end_idx = self.page(page_idx + 1).link_idx;
|
||||||
let n_links = end_idx - start_idx;
|
let n_links = end_idx - start_idx;
|
||||||
if n_links != 1 {
|
if n_links > 1 {
|
||||||
panic!("Redirect {:?} has {n_links} links", page.data.title);
|
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
|
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> {
|
pub fn link(&self, idx: u32) -> &Link<L> {
|
||||||
&self.links[idx as usize]
|
&self.links[idx as usize]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue