11use base64:: display:: Base64Display ;
22use base64:: engine:: general_purpose:: STANDARD ;
3+ use clap:: { Arg , ArgAction , Command } ;
34
45use simfony:: { Arguments , CompiledProgram } ;
56use std:: env;
@@ -13,30 +14,69 @@ fn main() {
1314 }
1415}
1516
16- #[ cfg( feature = "serde" ) ]
1717fn run ( ) -> Result < ( ) , String > {
18- let args: Vec < String > = env:: args ( ) . collect ( ) ;
18+ let command = {
19+ Command :: new ( env ! ( "CARGO_BIN_NAME" ) )
20+ . about (
21+ "\
22+ Compile the given Simfony program and print the resulting Simplicity base64 string.\n \
23+ If a Simfony witness is provided, then use it to satisfy the program (requires \
24+ feature 'serde' to be enabled).\
25+ "
26+ )
27+ . arg (
28+ Arg :: new ( "prog_file" )
29+ . required ( true )
30+ . value_name ( "PROGRAM_FILE" )
31+ . action ( ArgAction :: Set )
32+ . help ( "Simfony program file to build" )
33+ )
34+ } ;
1935
20- if args. len ( ) < 2 {
21- println ! ( "Usage: {} PROGRAM_FILE [WITNESS_FILE]" , args[ 0 ] ) ;
22- println ! (
23- "Compile the given Simfony program and print the resulting Simplicity base64 string."
24- ) ;
25- println ! ( "If a Simfony witness is provided, then use it to satisfy the program." ) ;
26- return Ok ( ( ) ) ;
27- }
36+ #[ cfg( feature = "serde" ) ]
37+ let command = {
38+ command. arg (
39+ Arg :: new ( "wit_file" )
40+ . value_name ( "WITNESS_FILE" )
41+ . action ( ArgAction :: Set )
42+ . help ( "File containing the witness data" )
43+ )
44+ } ;
2845
29- let prog_file = & args[ 1 ] ;
46+ let matches = {
47+ command
48+ . arg (
49+ Arg :: new ( "debug" )
50+ . long ( "debug" )
51+ . action ( ArgAction :: SetTrue )
52+ . help ( "Include debug symbols in the output" )
53+ )
54+ . get_matches ( )
55+ } ;
56+
57+ let prog_file = matches. get_one :: < String > ( "prog_file" ) . unwrap ( ) ;
3058 let prog_path = std:: path:: Path :: new ( prog_file) ;
3159 let prog_text = std:: fs:: read_to_string ( prog_path) . map_err ( |e| e. to_string ( ) ) ?;
32- let compiled = CompiledProgram :: new ( prog_text, Arguments :: default ( ) , false ) ?;
60+ let include_debug_symbols = matches. get_flag ( "debug" ) ;
61+
62+ let compiled = CompiledProgram :: new ( prog_text, Arguments :: default ( ) , include_debug_symbols) ?;
3363
34- if args. len ( ) >= 3 {
35- let wit_file = & args[ 2 ] ;
36- let wit_path = std:: path:: Path :: new ( wit_file) ;
37- let wit_text = std:: fs:: read_to_string ( wit_path) . map_err ( |e| e. to_string ( ) ) ?;
38- let witness = serde_json:: from_str :: < simfony:: WitnessValues > ( & wit_text) . unwrap ( ) ;
64+ #[ cfg( feature = "serde" ) ]
65+ let witness_opt = {
66+ matches
67+ . get_one :: < String > ( "wit_file" )
68+ . map ( |wit_file| -> Result < simfony:: WitnessValues , String > {
69+ let wit_path = std:: path:: Path :: new ( wit_file) ;
70+ let wit_text = std:: fs:: read_to_string ( wit_path) . map_err ( |e| e. to_string ( ) ) ?;
71+ let witness = serde_json:: from_str :: < simfony:: WitnessValues > ( & wit_text) . unwrap ( ) ;
72+ Ok ( witness)
73+ } )
74+ . transpose ( ) ?
75+ } ;
76+ #[ cfg( not( feature = "serde" ) ) ]
77+ let witness_opt: Option < simfony:: WitnessValues > = None ;
3978
79+ if let Some ( witness) = witness_opt {
4080 let satisfied = compiled. satisfy ( witness) ?;
4181 let ( program_bytes, witness_bytes) = satisfied. redeem ( ) . encode_to_vec ( ) ;
4282 println ! (
@@ -58,28 +98,3 @@ fn run() -> Result<(), String> {
5898 Ok ( ( ) )
5999}
60100
61- #[ cfg( not( feature = "serde" ) ) ]
62- fn run ( ) -> Result < ( ) , String > {
63- let args: Vec < String > = env:: args ( ) . collect ( ) ;
64-
65- if args. len ( ) < 2 {
66- println ! ( "Usage: {} PROGRAM_FILE" , args[ 0 ] ) ;
67- println ! (
68- "Compile the given Simfony program and print the resulting Simplicity base64 string."
69- ) ;
70- return Ok ( ( ) ) ;
71- }
72-
73- let prog_file = & args[ 1 ] ;
74- let prog_path = std:: path:: Path :: new ( prog_file) ;
75- let prog_text = std:: fs:: read_to_string ( prog_path) . map_err ( |e| e. to_string ( ) ) ?;
76- let compiled = CompiledProgram :: new ( prog_text, Arguments :: default ( ) , false ) ?;
77-
78- let program_bytes = compiled. commit ( ) . encode_to_vec ( ) ;
79- println ! (
80- "Program:\n {}" ,
81- Base64Display :: new( & program_bytes, & STANDARD )
82- ) ;
83-
84- Ok ( ( ) )
85- }
0 commit comments