xref: /wasmtime-44.0.1/cranelift/src/cat.rs (revision a0442ea0)
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 
6 use crate::utils::read_to_string;
7 use anyhow::{Context, Result};
8 use clap::Parser;
9 use cranelift_reader::parse_functions;
10 use std::path::{Path, PathBuf};
11 
12 /// Outputs .clif file
13 #[derive(Parser)]
14 pub struct Options {
15     /// Specify input file(s) to be used. Use '-' for stdin.
16     #[arg(required = true)]
17     files: Vec<PathBuf>,
18 }
19 
run(options: &Options) -> Result<()>20 pub 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 
cat_one(path: &Path) -> Result<()>30 fn 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 }
44