Skip to content

Commit a8282fa

Browse files
authored
Merge pull request #14 from bobtwinkles/output_format
Add support for outputting to files
2 parents 09f033b + 71e727d commit a8282fa

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/cli.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use failure::Error;
55
use std::time::{Duration, Instant};
66
use structopt::StructOpt;
77

8+
use std::io;
89
use std::path::PathBuf;
910

1011
arg_enum! {
@@ -26,6 +27,8 @@ pub struct Opt {
2627
skip_timing: bool,
2728
#[structopt(short = "v")]
2829
verbose: bool,
30+
#[structopt(short = "o", long = "output")]
31+
output_directory: Option<PathBuf>,
2932
#[structopt(raw(required = "true"))]
3033
fact_dirs: Vec<PathBuf>,
3134
}
@@ -52,7 +55,7 @@ pub fn main(opt: Opt) -> Result<(), Error> {
5255
println!("Time: {:0.3}s", seconds + millis);
5356
}
5457
if !opt.skip_tuples {
55-
output.dump(tables);
58+
output.dump(&opt.output_directory, tables).expect("Failed to write output");
5659
}
5760
}
5861

src/output/dump.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::intern::*;
33
use fxhash::FxHashMap;
44
use std::collections::{BTreeMap, BTreeSet};
55
use std::hash::Hash;
6+
use std::io::{self, Write};
67

78
crate trait OutputDump {
89
fn push_all(
@@ -13,10 +14,9 @@ crate trait OutputDump {
1314
);
1415
}
1516

16-
crate fn dump_rows(title: &str, intern: &InternerTables, value: &impl OutputDump) {
17-
println!("# {}", title);
18-
println!();
19-
17+
crate fn dump_rows(stream: &mut Write,
18+
intern: &InternerTables,
19+
value: &impl OutputDump) -> io::Result<()> {
2020
let mut rows = Vec::new();
2121
OutputDump::push_all(value, intern, &mut vec![], &mut rows);
2222
let col_width: usize = rows.iter()
@@ -37,8 +37,10 @@ crate fn dump_rows(title: &str, intern: &InternerTables, value: &impl OutputDump
3737
}
3838
string.push_str(last);
3939

40-
println!("{}", string);
40+
writeln!(stream, "{}", string)?;
4141
}
42+
43+
Ok(())
4244
}
4345

4446
impl<K, V> OutputDump for FxHashMap<K, V>

src/output/mod.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::intern::InternerTables;
1414
use fxhash::FxHashMap;
1515
use std::borrow::Cow;
1616
use std::collections::{BTreeMap, BTreeSet};
17+
use std::io::{self, Write};
18+
use std::path::PathBuf;
1719

1820
mod dump;
1921
mod timely;
@@ -48,13 +50,35 @@ impl Output {
4850
}
4951
}
5052

51-
crate fn dump(&self, intern: &InternerTables) {
52-
dump::dump_rows("borrow_live_at", intern, &self.borrow_live_at);
53+
crate fn dump(&self, output_dir: &Option<PathBuf>, intern: &InternerTables) -> io::Result<()> {
54+
dump::dump_rows(&mut writer_for(output_dir, "borrow_live_at")?, intern, &self.borrow_live_at)?;
5355

5456
if self.dump_enabled {
55-
dump::dump_rows("restricts", intern, &self.restricts);
56-
dump::dump_rows("region_live_at", intern, &self.region_live_at);
57-
dump::dump_rows("subset", intern, &self.subset);
57+
dump::dump_rows(&mut writer_for(output_dir, "restricts")?, intern, &self.restricts)?;
58+
dump::dump_rows(&mut writer_for(output_dir, "region_live_at")?, intern, &self.region_live_at)?;
59+
dump::dump_rows(&mut writer_for(output_dir, "subset")?, intern, &self.subset)?;
60+
}
61+
return Ok(());
62+
63+
fn writer_for(out_dir: &Option<PathBuf>, name: &str) -> io::Result<Box<Write>> {
64+
// create a writer for the provided output.
65+
// If we have an output directory use that, otherwise just dump to stdout
66+
use std::fs;
67+
use std::path::Path;
68+
69+
Ok(match out_dir {
70+
Some(dir) => {
71+
fs::create_dir_all(&dir)?;
72+
let mut of = dir.join(name);
73+
of.set_extension("facts");
74+
Box::new(fs::File::create(of)?)
75+
},
76+
None => {
77+
let mut stdout = io::stdout();
78+
write!(&mut stdout, "# {}\n\n", name)?;
79+
Box::new(stdout)
80+
}
81+
})
5882
}
5983
}
6084

0 commit comments

Comments
 (0)