forked from rust-lang/regex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.rs
73 lines (65 loc) · 1.88 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::{
io::{self, Write},
path::Path,
process::Command,
};
use {anyhow::Context, bstr::BString};
/// Time an arbitrary operation.
pub fn timeit<T>(run: impl FnOnce() -> T) -> (T, std::time::Duration) {
let start = std::time::Instant::now();
let t = run();
(t, start.elapsed())
}
/// Convenient time an operation that returns a result by packing the duration
/// into the `Ok` variant.
pub fn timeitr<T, E>(
run: impl FnOnce() -> Result<T, E>,
) -> Result<(T, std::time::Duration), E> {
let (result, time) = timeit(run);
let t = result?;
Ok((t, time))
}
/// Run rustfmt on the given file.
pub fn rustfmt<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
let path = path.as_ref();
let out = Command::new("rustfmt")
.arg(path)
.output()
.context("rustfmt command failed")?;
anyhow::ensure!(
out.status.success(),
"rustfmt {}: {}",
path.display(),
BString::from(out.stderr),
);
Ok(())
}
/// A somewhat silly little thing that prints an aligned table of key-value
/// pairs. Keys can be any string and values can be anything that implements
/// Debug.
///
/// This table is used to print little bits of useful information about stuff.
#[derive(Debug)]
pub struct Table {
pairs: Vec<(String, Box<dyn std::fmt::Debug>)>,
}
impl Table {
pub fn empty() -> Table {
Table { pairs: vec![] }
}
pub fn add<D: std::fmt::Debug + 'static>(
&mut self,
label: &str,
value: D,
) {
self.pairs.push((label.to_string(), Box::new(value)));
}
pub fn print<W: io::Write>(&self, wtr: W) -> io::Result<()> {
let mut wtr = tabwriter::TabWriter::new(wtr)
.alignment(tabwriter::Alignment::Right);
for (label, value) in self.pairs.iter() {
writeln!(wtr, "{}:\t{:?}", label, value)?;
}
wtr.flush()
}
}