1#![deny(missing_docs)]
4
5use clap::Parser;
6use cranelift_codegen::ir::Function;
7use cranelift_reader::parse_functions;
8use std::fs::File;
9use std::io;
10use std::io::prelude::*;
11use std::process;
12
13fn call_ser(file: &str, pretty: bool) -> Result<(), String> {
14 let ret_of_parse = parse_functions(file);
15 match ret_of_parse {
16 Ok(funcs) => {
17 let ser_str = if pretty {
18 serde_json::to_string_pretty(&funcs).unwrap()
19 } else {
20 serde_json::to_string(&funcs).unwrap()
21 };
22 println!("{ser_str}");
23 Ok(())
24 }
25 Err(_pe) => Err("There was a parsing error".to_string()),
26 }
27}
28
29fn call_de(file: &File) -> Result<(), String> {
30 let de: Vec<Function> = match serde_json::from_reader(file) {
31 Result::Ok(val) => val,
32 Result::Err(err) => panic!("{}", err),
33 };
34 println!("{de:?}");
35 Ok(())
36}
37
38#[derive(Parser, Debug)]
40#[command(about)]
41enum Args {
42 Serialize {
44 #[arg(long, short)]
46 pretty: bool,
47
48 file: String,
50 },
51 Deserialize {
53 file: String,
55 },
56}
57
58fn main() {
59 let res_serde = match Args::parse() {
60 Args::Serialize { pretty, file } => {
61 let mut contents = String::new();
62 let mut file = File::open(file).expect("Unable to open the file");
63 file.read_to_string(&mut contents)
64 .expect("Unable to read the file");
65
66 call_ser(&contents, pretty)
67 }
68 Args::Deserialize { file } => {
69 let file = File::open(file).expect("Unable to open the file");
70 call_de(&file)
71 }
72 };
73
74 if let Err(mut msg) = res_serde {
75 if !msg.ends_with('\n') {
76 msg.push('\n');
77 }
78 io::stdout().flush().expect("flushing stdout");
79 io::stderr().write_all(msg.as_bytes()).unwrap();
80 process::exit(1);
81 }
82}