Skip to content

Commit 04e6ea2

Browse files
committed
feat: added proof of concept
1 parent ace714a commit 04e6ea2

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

taurus_poc/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "taurus_poc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

taurus_poc/build.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

taurus_poc/main.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//This is a generated main file
2+
macro_rules! add_integers {
3+
($first:expr, $second:expr) => {
4+
$first + $second
5+
};
6+
}
7+
8+
macro_rules! subtract_integers {
9+
($first:expr, $second:expr) => {
10+
$first - $second
11+
};
12+
}
13+
14+
macro_rules! multiply_integers {
15+
($first:expr, $second:expr) => {
16+
$first * $second
17+
};
18+
}
19+
fn main() {
20+
add_integers!(1, 3);
21+
subtract_integers!(2, 4);
22+
multiply_integers!(5, 10);
23+
add_integers!(3, 100);
24+
}

taurus_poc/poc.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add|1|3
2+
sub|2|4
3+
multiply|5|10
4+
add|3|100

taurus_poc/src/main.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::fs::{read_to_string};
2+
use std::io::BufRead;
3+
use std::time::Instant;
4+
5+
macro_rules! add_integers {
6+
($first:expr, $second:expr) => {
7+
$first+$second
8+
};
9+
}
10+
11+
macro_rules! subtract_integers {
12+
($first:expr, $second:expr) => {
13+
$first-$second
14+
};
15+
}
16+
17+
macro_rules! multiply_integers {
18+
($first:expr, $second:expr) => {
19+
$first*$second
20+
};
21+
}
22+
23+
/*
24+
Interpreted POC 1.
25+
This is just interprets incoming flows
26+
What can be optimised?
27+
- flow till be typed so that no string processing is needed
28+
- compromise match statement into map
29+
- import macros from shared macro module
30+
31+
Av. execution time: 627.2µs
32+
*/
33+
fn main() {
34+
let start = Instant::now();
35+
36+
for line in read_to_string("taurus_poc/poc.txt").unwrap().lines() {
37+
38+
let parts: Vec<&str> = line.split('|').collect();
39+
40+
if parts.len() == 3 {
41+
let operator = parts[0];
42+
let first: i32 = parts[1].parse().unwrap_or(0);
43+
let second: i32 = parts[2].parse().unwrap_or(0);
44+
45+
let result = match operator {
46+
"add" => add_integers!(first, second),
47+
"sub" => subtract_integers!(first, second),
48+
"multiply" => multiply_integers!(first, second),
49+
_ => {
50+
println!("Unsupported operator: {}", operator);
51+
continue;
52+
}
53+
};
54+
55+
println!("{} {} {} = {}", operator, first, second, result);
56+
} else {
57+
println!("Invalid line format");
58+
}
59+
}
60+
61+
let duration = start.elapsed();
62+
println!("Time elapsed in expensive_function() is: {:?}", duration);
63+
}

0 commit comments

Comments
 (0)