3
2
Pārlūkot izejas kodu

Add a cmz_core binary that expands the CMZProtocol macro

Ian Goldberg 1 mēnesi atpakaļ
vecāks
revīzija
9827822c23
2 mainītis faili ar 54 papildinājumiem un 0 dzēšanām
  1. 2 0
      cmz_core/Cargo.toml
  2. 52 0
      cmz_core/src/main.rs

+ 2 - 0
cmz_core/Cargo.toml

@@ -8,3 +8,5 @@ syn = { version = "2.0", features = ["extra-traits", "visit-mut"] }
 quote = "1.0"
 darling = "0.20"
 proc-macro2 = "1.0"
+clap = { version = "4.5", features = ["derive"] }
+prettyplease = "0.2"

+ 52 - 0
cmz_core/src/main.rs

@@ -0,0 +1,52 @@
+use clap::Parser;
+use cmz_core::*;
+use std::io;
+use std::process::ExitCode;
+
+#[derive(Parser, Debug)]
+#[clap(version, about, long_about = None)]
+struct Args {
+    /// use CMZ14 (as opposed to the default of µCMZ)
+    #[arg(short = '1', long)]
+    cmz14: bool,
+
+    /// only generate the client-side code
+    #[arg(short, long)]
+    client_only: bool,
+
+    /// only generate the issuer-side code
+    #[arg(short, long)]
+    issuer_only: bool,
+}
+
+fn pretty_print(code_str: &str) {
+    let parsed_output = syn::parse_file(code_str).unwrap();
+    let formatted_output = prettyplease::unparse(&parsed_output);
+    println!("{}", formatted_output);
+}
+
+fn main() -> ExitCode {
+    let args = Args::parse();
+    let stdin = io::read_to_string(io::stdin()).unwrap();
+
+    if args.client_only && args.issuer_only {
+        eprintln!("client_only and issuer_only cannot be used together");
+        return ExitCode::FAILURE;
+    };
+    let emit_client = !args.issuer_only;
+    let emit_issuer = !args.client_only;
+    let use_mucmz = !args.cmz14;
+
+    let proto_spec: ProtoSpec = match syn::parse_str(&stdin) {
+        Err(_) => {
+            eprintln!("Could not parse stdin as a CMZ protocol specification");
+            return ExitCode::FAILURE;
+        }
+        Ok(proto_spec) => proto_spec,
+    };
+
+    let output = cmz_core(&proto_spec, use_mucmz, emit_client, emit_issuer);
+    pretty_print(&output.to_string());
+
+    ExitCode::SUCCESS
+}