@@ -81,13 +81,15 @@ enum InFormats {
81
81
Json ,
82
82
Ron ,
83
83
Yaml ,
84
+ Bin ,
84
85
}
85
86
86
87
#[ derive( ArgEnum , Clone ) ]
87
88
enum OutFormats {
88
89
Json ,
89
90
Ron ,
90
91
Yaml ,
92
+ Bin ,
91
93
}
92
94
93
95
#[ allow( clippy:: enum_glob_use) ]
@@ -106,35 +108,41 @@ fn main() -> std::io::Result<()> {
106
108
} => {
107
109
use InFormats :: * ;
108
110
109
- let parser: Box < dyn FnOnce ( String , InstSet ) -> Executor > = match format {
110
- Pasm => Box :: new ( |s , set| parse:: parse ( s , set) ) ,
111
- Json => Box :: new ( |s , set| {
112
- serde_json:: from_str :: < CompiledProg > ( & s )
111
+ let parser: Box < dyn FnOnce ( Vec < u8 > , InstSet ) -> Executor > = match format {
112
+ Pasm => Box :: new ( |v , set| parse:: parse ( String :: from_utf8_lossy ( & v ) , set) ) ,
113
+ Json => Box :: new ( |v , set| {
114
+ serde_json:: from_str :: < CompiledProg > ( & String :: from_utf8_lossy ( & v ) )
113
115
. unwrap ( )
114
116
. to_executor ( set)
115
117
} ) ,
116
- Ron => {
117
- Box :: new ( |s, set| ron:: from_str :: < CompiledProg > ( & s) . unwrap ( ) . to_executor ( set) )
118
- }
119
- Yaml => Box :: new ( |s, set| {
120
- serde_yaml:: from_str :: < CompiledProg > ( & s)
118
+ Ron => Box :: new ( |v, set| {
119
+ ron:: from_str :: < CompiledProg > ( & String :: from_utf8_lossy ( & v) )
121
120
. unwrap ( )
122
121
. to_executor ( set)
123
122
} ) ,
123
+ Yaml => Box :: new ( |v, set| {
124
+ serde_yaml:: from_str :: < CompiledProg > ( & String :: from_utf8_lossy ( & v) )
125
+ . unwrap ( )
126
+ . to_executor ( set)
127
+ } ) ,
128
+ Bin => Box :: new ( |v, set| {
129
+ bincode:: decode_from_slice :: < CompiledProg , _ > ( & v, bincode:: config:: standard ( ) )
130
+ . unwrap ( )
131
+ . 0
132
+ . to_executor ( set)
133
+ } ) ,
124
134
} ;
125
135
126
136
init_logger ( verbosity) ;
127
- let prog = std:: fs:: read_to_string ( path) ?;
128
-
137
+ let prog_bytes = std:: fs:: read ( path) ?;
129
138
let mut timer = bench. then ( std:: time:: Instant :: now) ;
130
139
131
- let mut executor = parser ( prog , INST_SET ) ;
140
+ let mut executor = parser ( prog_bytes , INST_SET ) ;
132
141
133
142
timer = timer. map ( |t| {
134
143
println ! ( "Total parse time: {:?}" , t. elapsed( ) ) ;
135
144
std:: time:: Instant :: now ( )
136
145
} ) ;
137
-
138
146
if timer. is_some ( ) || verbosity > 0 {
139
147
println ! ( "Execution starts on next line" ) ;
140
148
}
@@ -152,26 +160,31 @@ fn main() -> std::io::Result<()> {
152
160
} => {
153
161
use OutFormats :: * ;
154
162
155
- let serializer: Box < dyn FnOnce ( CompiledProg ) -> String > = match format {
163
+ let serializer: Box < dyn FnOnce ( CompiledProg ) -> Vec < u8 > > = match format {
156
164
Json => Box :: new ( |prog| {
157
165
use serde_json:: ser:: { to_string, to_string_pretty} ;
158
166
159
- if minify {
167
+ ( if minify {
160
168
to_string ( & prog) . unwrap ( )
161
169
} else {
162
170
to_string_pretty ( & prog) . unwrap ( )
163
- }
171
+ } )
172
+ . into_bytes ( )
164
173
} ) ,
165
174
Ron => Box :: new ( |prog| {
166
175
use ron:: ser:: { to_string, to_string_pretty, PrettyConfig } ;
167
176
168
- if minify {
177
+ ( if minify {
169
178
to_string ( & prog) . unwrap ( )
170
179
} else {
171
180
to_string_pretty ( & prog, PrettyConfig :: default ( ) ) . unwrap ( )
172
- }
181
+ } )
182
+ . into_bytes ( )
183
+ } ) ,
184
+ Yaml => Box :: new ( |prog| serde_yaml:: to_string ( & prog) . unwrap ( ) . into_bytes ( ) ) ,
185
+ Bin => Box :: new ( |prog| {
186
+ bincode:: encode_to_vec ( & prog, bincode:: config:: standard ( ) ) . unwrap ( )
173
187
} ) ,
174
- Yaml => Box :: new ( |prog| serde_yaml:: to_string ( & prog) . unwrap ( ) ) ,
175
188
} ;
176
189
177
190
init_logger ( verbosity) ;
@@ -183,11 +196,12 @@ fn main() -> std::io::Result<()> {
183
196
Json => ".json" ,
184
197
Ron => ".ron" ,
185
198
Yaml => ".yaml" ,
199
+ Bin => ".bin" ,
186
200
} ) ;
187
201
input
188
202
} ) ;
189
203
190
- std:: fs:: write ( output, serializer ( compiled) ) ?;
204
+ std:: fs:: write ( output, & * serializer ( compiled) ) ?;
191
205
}
192
206
}
193
207
0 commit comments