diff --git a/brood/src/commands.rs b/brood/src/commands.rs index d72d397..cc694c1 100644 --- a/brood/src/commands.rs +++ b/brood/src/commands.rs @@ -1,3 +1,4 @@ +pub mod export; pub mod ingest; pub mod path; pub mod show; diff --git a/brood/src/commands/export.rs b/brood/src/commands/export.rs new file mode 100644 index 0000000..aad5dd8 --- /dev/null +++ b/brood/src/commands/export.rs @@ -0,0 +1,17 @@ +use std::{io, path::PathBuf}; + +use crate::data::Data; + +#[derive(Debug, clap::Parser)] +pub struct Cmd { + out: PathBuf, +} + +impl Cmd { + pub fn run(self, data: Data) -> io::Result<()> { + println!(">> Export"); + data.write_to_file(&self.out)?; + + Ok(()) + } +} diff --git a/brood/src/commands/reexport.rs b/brood/src/commands/reexport.rs deleted file mode 100644 index 1125fb0..0000000 --- a/brood/src/commands/reexport.rs +++ /dev/null @@ -1,48 +0,0 @@ -use std::fs::File; -use std::io::{self, BufReader, BufWriter}; -use std::path::Path; - -use crate::data::adjacency_list::AdjacencyList; -use crate::data::store; - -pub fn reexport( - from: &Path, - to: &Path, - in_parens: Option, - in_structure: Option, -) -> io::Result<()> { - eprintln!(">> Import"); - let mut from = BufReader::new(File::open(from)?); - let mut data = store::read_adjacency_list(&mut from)?; - - eprintln!(">> Consistency check"); - data.check_consistency(); - - if in_parens.is_some() || in_structure.is_some() { - eprintln!(">> Filtering"); - - let mut data2 = AdjacencyList::default(); - for (page_idx, page) in data.pages() { - data2.push_page(page.data.clone()); - for (_, link) in data.links(page_idx) { - if in_parens.is_some_and(|v| v != link.data.in_parens()) { - continue; - } - - if in_structure.is_some_and(|v| v != link.data.in_structure()) { - continue; - } - - data2.push_link(link.to, link.data); - } - } - - data = data2; - } - - eprintln!(">> Export"); - let mut to = BufWriter::new(File::create(to)?); - store::write_adjacency_list(&data, &mut to)?; - - Ok(()) -} diff --git a/brood/src/main.rs b/brood/src/main.rs index 9f1af1e..f0e1a30 100644 --- a/brood/src/main.rs +++ b/brood/src/main.rs @@ -12,6 +12,7 @@ use data::Data; #[derive(Debug, Parser)] enum Command { Ingest(commands::ingest::Cmd), + Export(commands::export::Cmd), Show(commands::show::Cmd), Path(commands::path::Cmd), } @@ -54,6 +55,7 @@ fn main() -> io::Result<()> { match args.command { Command::Ingest(_) => unreachable!(), + Command::Export(cmd) => cmd.run(data), Command::Show(cmd) => cmd.run(data), Command::Path(cmd) => cmd.run(data), }