@@ -2,11 +2,13 @@ use anyhow::Result;
22use heck:: { ToLowerCamelCase as _, ToSnakeCase as _, ToUpperCamelCase as _} ;
33use std:: borrow:: Cow ;
44use std:: collections:: { BTreeMap , BTreeSet , HashMap , HashSet , hash_map} ;
5+ use std:: fmt;
56use std:: fmt:: Write as _;
67use std:: io:: { self , Write as _} ;
78use std:: iter;
89use std:: mem;
910use std:: process:: Command ;
11+ use std:: str:: FromStr ;
1012use std:: thread;
1113use wit_bindgen_core:: abi:: {
1214 self , AbiVariant , Bindgen , Bitcast , FlatTypes , Instruction , LiftLower , WasmType ,
@@ -31,9 +33,55 @@ const EXPORT_RETURN_AREA: &str = "exportReturnArea";
3133const SYNC_EXPORT_PINNER : & str = "syncExportPinner" ;
3234const PINNER : & str = "pinner" ;
3335
36+ #[ derive( Default , Debug , Copy , Clone ) ]
37+ pub enum Format {
38+ #[ default]
39+ True ,
40+ False ,
41+ }
42+
43+ impl fmt:: Display for Format {
44+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
45+ write ! (
46+ f,
47+ "{}" ,
48+ match self {
49+ Self :: True => "true" ,
50+ Self :: False => "false" ,
51+ }
52+ )
53+ }
54+ }
55+
56+ impl FromStr for Format {
57+ type Err = String ;
58+
59+ fn from_str ( s : & str ) -> Result < Format , String > {
60+ match s {
61+ "true" => Ok ( Format :: True ) ,
62+ "false" => Ok ( Format :: False ) ,
63+ _ => Err ( format ! ( "expected `true` or `false`; got `{s}`" ) ) ,
64+ }
65+ }
66+ }
67+
3468#[ derive( Default , Debug , Clone ) ]
3569#[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
3670pub struct Opts {
71+ /// Whether or not `gofmt` should be used (if present) to format generated
72+ /// code.
73+ #[ cfg_attr(
74+ feature = "clap" ,
75+ arg(
76+ long,
77+ default_value = "true" ,
78+ default_missing_value = "true" ,
79+ num_args = 0 ..=1 ,
80+ require_equals = true ,
81+ )
82+ ) ]
83+ pub format : Format ,
84+
3785 #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
3886 pub async_ : AsyncFilterSet ,
3987
@@ -721,7 +769,8 @@ impl WorldGenerator for Go {
721769
722770 files. push (
723771 "wit_bindings.go" ,
724- & gofmt (
772+ & maybe_gofmt (
773+ self . opts . format ,
725774 format ! (
726775 r#"package main
727776
@@ -756,7 +805,8 @@ func main() {{}}
756805
757806 files. push (
758807 & format ! ( "{prefix}{name}/wit_bindings.go" ) ,
759- & gofmt (
808+ & maybe_gofmt (
809+ self . opts . format ,
760810 format ! (
761811 "package {prefix}{name}
762812
@@ -796,7 +846,8 @@ import (
796846
797847 files. push (
798848 "wit_types/wit_tuples.go" ,
799- & gofmt (
849+ & maybe_gofmt (
850+ self . opts . format ,
800851 format ! (
801852 r#"package wit_types
802853
@@ -2959,17 +3010,19 @@ fn func_declaration(resolve: &Resolve, func: &Function) -> (String, bool) {
29593010 }
29603011}
29613012
2962- fn gofmt < ' a > ( code : & ' a [ u8 ] ) -> Cow < ' a , [ u8 ] > {
3013+ fn maybe_gofmt < ' a > ( format : Format , code : & ' a [ u8 ] ) -> Cow < ' a , [ u8 ] > {
29633014 return thread:: scope ( |s| {
2964- if let Ok ( ( reader, mut writer) ) = io:: pipe ( ) {
3015+ if let Format :: True = format
3016+ && let Ok ( ( reader, mut writer) ) = io:: pipe ( )
3017+ {
29653018 s. spawn ( move || {
29663019 _ = writer. write_all ( & code) ;
29673020 } ) ;
29683021
2969- if let Ok ( output) = Command :: new ( "gofmt" ) . stdin ( reader) . output ( ) {
2970- if output. status . success ( ) {
2971- return Cow :: Owned ( output . stdout ) ;
2972- }
3022+ if let Ok ( output) = Command :: new ( "gofmt" ) . stdin ( reader) . output ( )
3023+ && output. status . success ( )
3024+ {
3025+ return Cow :: Owned ( output . stdout ) ;
29733026 }
29743027 }
29753028
0 commit comments