Refactor export and add page length

This commit is contained in:
Joscha 2022-10-03 22:14:58 +02:00
parent d910047b48
commit f71092058b
3 changed files with 123 additions and 74 deletions

View file

@ -72,13 +72,14 @@ impl FirstStage {
}
}
fn insert_page(&mut self, id: u32, title: String, redirect: bool) {
fn insert_page(&mut self, id: u32, length: u32, redirect: bool, title: String) {
let link_idx = self.pages.len() as u32;
self.pages.push(Page {
link_idx,
id,
title,
length,
redirect,
title,
});
}
@ -87,7 +88,7 @@ impl FirstStage {
}
fn import_json_page(&mut self, page: JsonPage) {
self.insert_page(page.id, page.title, page.redirect.is_some());
self.insert_page(page.id, page.length, page.redirect.is_some(), page.title);
for (to, start, end) in page.links {
let to = self.insert_title(util::normalize_link(&to));
self.insert_link(to, start, end);
@ -95,7 +96,12 @@ impl FirstStage {
}
fn finalize(&mut self) {
self.insert_page(0, "dummy page at the end of all pages".to_string(), false);
self.insert_page(
0,
0,
false,
"dummy page at the end of all pages".to_string(),
);
}
fn from_stdin() -> io::Result<Self> {

View file

@ -6,15 +6,15 @@ use crate::data::AdjacencyList;
pub fn reexport(from: &Path, to: &Path) -> io::Result<()> {
eprintln!(">> Import");
let from = BufReader::new(File::open(from)?);
let data = AdjacencyList::read(from)?;
let mut from = BufReader::new(File::open(from)?);
let data = AdjacencyList::read(&mut from)?;
eprintln!(">> Consistency check");
data.check_consistency();
eprintln!(">> Export");
let to = BufWriter::new(File::create(to)?);
data.write(to)?;
let mut to = BufWriter::new(File::create(to)?);
data.write(&mut to)?;
Ok(())
}