clif_util/
cat.rs

1//! The `cat` sub-command.
2//!
3//! Read a sequence of Cranelift IR files and print them again to stdout. This has the effect of
4//! normalizing formatting and removing comments.
5
6use crate::utils::read_to_string;
7use anyhow::{Context, Result};
8use clap::Parser;
9use cranelift_reader::parse_functions;
10use std::path::{Path, PathBuf};
11
12/// Outputs .clif file
13#[derive(Parser)]
14pub struct Options {
15    /// Specify input file(s) to be used. Use '-' for stdin.
16    #[arg(required = true)]
17    files: Vec<PathBuf>,
18}
19
20pub fn run(options: &Options) -> Result<()> {
21    for (i, f) in options.files.iter().enumerate() {
22        if i != 0 {
23            println!();
24        }
25        cat_one(f)?
26    }
27    Ok(())
28}
29
30fn cat_one(path: &Path) -> Result<()> {
31    let buffer = read_to_string(path)?;
32    let items =
33        parse_functions(&buffer).with_context(|| format!("failed to parse {}", path.display()))?;
34
35    for (idx, func) in items.into_iter().enumerate() {
36        if idx != 0 {
37            println!();
38        }
39        print!("{func}");
40    }
41
42    Ok(())
43}