06cf8bfedba0d0c0a79d85cfa7adac61401e3120
[bigint-presentation-code.git] / register_allocator / src / main.rs
1 use bigint_presentation_code_register_allocator::{function::Function, interned::GlobalState};
2 use clap::Parser;
3 use eyre::Result;
4 use serde::Serialize;
5 use serde_json::{Deserializer, Value};
6 use std::io::{BufWriter, Write};
7
8 #[derive(Parser, Debug)]
9 #[command(version, about, long_about)]
10 struct Args {
11 #[arg(long)]
12 dump_input: bool,
13 #[arg(long)]
14 pretty: bool,
15 }
16
17 #[derive(Serialize, Debug)]
18 pub enum Info<'a> {
19 DumpInput(&'a Function),
20 }
21
22 #[derive(Serialize, Debug)]
23 pub enum Output<'a> {
24 Error(String),
25 Info(Info<'a>),
26 Success(Value),
27 }
28
29 impl<'a> Output<'a> {
30 fn write_to_stdout(&self, pretty: bool) -> Result<()> {
31 let mut stdout = BufWriter::new(std::io::stdout().lock());
32 if pretty {
33 serde_json::to_writer_pretty(&mut stdout, self)?;
34 } else {
35 serde_json::to_writer(&mut stdout, self)?;
36 }
37 writeln!(stdout)?;
38 stdout.flush()?;
39 Ok(())
40 }
41 fn from_err(e: impl Into<eyre::Report>) -> Self {
42 Self::Error(format!("{:?}", e.into()))
43 }
44 }
45
46 fn main() -> Result<()> {
47 let Args { dump_input, pretty } = Args::parse();
48 let stdin = std::io::stdin().lock();
49 for input in Deserializer::from_reader(stdin).into_iter::<Value>() {
50 GlobalState::scope(|| -> Result<()> {
51 let function = match serde_json::from_value::<Function>(input?) {
52 Ok(v) => v,
53 Err(e) => {
54 Output::from_err(e).write_to_stdout(pretty)?;
55 return Ok(());
56 }
57 };
58 if dump_input {
59 Output::Info(Info::DumpInput(&function)).write_to_stdout(pretty)?;
60 }
61 todo!()
62 })?;
63 }
64 Ok(())
65 }