diff --git a/brood/src/commands.rs b/brood/src/commands.rs index 76837ed..5e71884 100644 --- a/brood/src/commands.rs +++ b/brood/src/commands.rs @@ -1,5 +1,7 @@ mod ingest; +mod path; mod reexport; pub use ingest::ingest; +pub use path::path; pub use reexport::reexport; diff --git a/brood/src/commands/path.rs b/brood/src/commands/path.rs new file mode 100644 index 0000000..d536108 --- /dev/null +++ b/brood/src/commands/path.rs @@ -0,0 +1,33 @@ +use std::fs::File; +use std::io::{self, BufReader}; +use std::path::Path; + +use crate::data::AdjacencyList; +use crate::util; + +pub fn path(datafile: &Path, from: &str, to: &str) -> io::Result<()> { + eprintln!(">> Import"); + let mut databuf = BufReader::new(File::open(datafile)?); + let mut data = AdjacencyList::read(&mut databuf)?.change_page_data(f32::INFINITY); + + eprintln!(">> Locate from and to"); + let from = util::normalize_link(from); + let to = util::normalize_link(to); + let (from_i, from_p) = data + .pages + .iter() + .enumerate() + .filter(|(_, p)| !p.redirect) + .find(|(_, p)| util::normalize_link(&p.title) == from) + .unwrap_or_else(|| panic!("no article called {from}")); + let (to_i, to_p) = data + .pages + .iter() + .enumerate() + .filter(|(_, p)| !p.redirect) + .find(|(_, p)| util::normalize_link(&p.title) == to) + .unwrap_or_else(|| panic!("no article called {to}")); + dbg!(from_i, from_p, to_i, to_p); + + Ok(()) +} diff --git a/brood/src/main.rs b/brood/src/main.rs index 609b45e..dec36d5 100644 --- a/brood/src/main.rs +++ b/brood/src/main.rs @@ -13,6 +13,8 @@ enum Command { Ingest, /// Read and reexport brood data. Reexport { to: PathBuf }, + /// Find a path from one article to another. + Path { from: String, to: String }, } #[derive(Debug, Parser)] @@ -27,5 +29,6 @@ fn main() -> io::Result<()> { match args.command { Command::Ingest => commands::ingest(&args.datafile), Command::Reexport { to } => commands::reexport(&args.datafile, &to), + Command::Path { from, to } => commands::path(&args.datafile, &from, &to), } }