1+ use std:: fs:: File ;
2+ use std:: io:: { BufRead , BufReader , Write } ;
3+
4+ /*
5+ Compiled POC 1.
6+ This is just writing `rust` as string to a file
7+ What can be optimised?
8+ - marco to turn macros into a string | import from macro module
9+
10+ Av. execution time: 2.1µs
11+ */
12+ fn process_input ( input_file : & str , output_file : & str ) -> std:: io:: Result < ( ) > {
13+ let input = File :: open ( input_file) ?;
14+ let reader = BufReader :: new ( input) ;
15+ let mut output = File :: create ( output_file) ?;
16+
17+ writeln ! ( output, "\
18+ //This is a generated main file
19+ macro_rules! add_integers {{
20+ ($first:expr, $second:expr) => {{
21+ $first + $second
22+ }};
23+ }}
24+
25+ macro_rules! subtract_integers {{
26+ ($first:expr, $second:expr) => {{
27+ $first - $second
28+ }};
29+ }}
30+
31+ macro_rules! multiply_integers {{
32+ ($first:expr, $second:expr) => {{
33+ $first * $second
34+ }};
35+ }}
36+ fn main() {{" ) ?;
37+
38+ for line in reader. lines ( ) {
39+ let line = line?;
40+ let parts: Vec < & str > = line. split ( '|' ) . collect ( ) ;
41+
42+ if parts. len ( ) == 3 {
43+ match parts[ 0 ] {
44+ "add" => writeln ! ( output, " add_integers!({}, {});" , parts[ 1 ] , parts[ 2 ] ) ?,
45+ "sub" => writeln ! ( output, " subtract_integers!({}, {});" , parts[ 1 ] , parts[ 2 ] ) ?,
46+ "multiply" => writeln ! ( output, " multiply_integers!({}, {});" , parts[ 1 ] , parts[ 2 ] ) ?,
47+ _ => eprintln ! ( "Unknown operation: {}" , parts[ 0 ] ) ,
48+ }
49+ }
50+ }
51+
52+ writeln ! ( output, "}}" ) ?;
53+ Ok ( ( ) )
54+ }
55+
56+ fn main ( ) -> std:: io:: Result < ( ) > {
57+ process_input ( "./poc.txt" , "main.rs" )
58+ }
0 commit comments