-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cat.rs
43 lines (37 loc) · 1.04 KB
/
cat.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
//! The `cat` sub-command.
//!
//! Read a sequence of Cranelift IR files and print them again to stdout. This has the effect of
//! normalizing formatting and removing comments.
use crate::utils::read_to_string;
use anyhow::{Context, Result};
use clap::Parser;
use cranelift_reader::parse_functions;
use std::path::{Path, PathBuf};
/// Outputs .clif file
#[derive(Parser)]
pub struct Options {
/// Specify input file(s) to be used. Use '-' for stdin.
#[arg(required = true)]
files: Vec<PathBuf>,
}
pub fn run(options: &Options) -> Result<()> {
for (i, f) in options.files.iter().enumerate() {
if i != 0 {
println!();
}
cat_one(f)?
}
Ok(())
}
fn cat_one(path: &Path) -> Result<()> {
let buffer = read_to_string(path)?;
let items =
parse_functions(&buffer).with_context(|| format!("failed to parse {}", path.display()))?;
for (idx, func) in items.into_iter().enumerate() {
if idx != 0 {
println!();
}
print!("{}", func);
}
Ok(())
}