Skip to content

Commit 920479c

Browse files
committed
Add rewrite of calc.py/filepicker in rust
1 parent 805a98e commit 920479c

File tree

6 files changed

+170
-6
lines changed

6 files changed

+170
-6
lines changed

.github/workflows/calc.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Calc
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
paths:
7+
- calc/**
8+
pull_request:
9+
branches: [ main ]
10+
paths:
11+
- calc/**
12+
workflow_dispatch:
13+
14+
env:
15+
CARGO_TERM_COLOR: always
16+
17+
jobs:
18+
build:
19+
runs-on: windows-2019
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Build
23+
run: cargo build --verbose --release --manifest-path ./calc/Cargo.toml
24+
- name: Upload a Build Artifact
25+
uses: actions/[email protected]
26+
with:
27+
# Artifact name
28+
# optional, default is artifact
29+
# A file, directory or wildcard pattern that describes what to upload
30+
path: D:\a\Atlas-Utilities\Atlas-Utilities\calc\target\release\calc.exe

.gitignore

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
# MacOS
1818
*.DS_Store
1919

20-
21-
# pyinstaller builds
22-
build/
23-
dist/
24-
*.spec
25-
__pycache__/
20+
# Rust
21+
/target/
22+
**/target/
23+
**/Cargo.lock
24+
# These are backup files generated by rustfmt
25+
**/*.rs.bk

calc/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "calc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
csv = "1.1.6"
10+
11+
[profile.release]
12+
opt-level = 3
13+
lto = true
14+
codegen-units = 1
15+
panic = "abort"
16+
debug = 0
17+
18+
[profile.dev]
19+
debug = 1

calc/src/main.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
fn main() {
2+
let args: Vec<String> = std::env::args().collect();
3+
if args.len() > 1 {
4+
match args[1].as_ref() {
5+
"parse" => parse(args[2].as_ref()),
6+
"add" => add(args[2].as_ref(), args[3].as_ref()),
7+
"rnd" => println!("{}", args[2].parse::<f64>().unwrap().round()),
8+
"div" => div(args[2].as_ref(), args[3].as_ref()),
9+
"divint" => divint(args[2].as_ref(), args[3].as_ref()),
10+
"help" => help(),
11+
_ => help(),
12+
}
13+
} else {
14+
help();
15+
}
16+
}
17+
18+
fn parse(path: &str) {
19+
// OCAT CSVs change amount of headers after the first line, so make reader flexible
20+
let mut reader = csv::ReaderBuilder::new()
21+
.has_headers(true)
22+
.flexible(true)
23+
.from_path(path)
24+
.unwrap();
25+
let mut sum: f64 = 0.0;
26+
let mut sorted_values: Vec<f64> = Vec::new();
27+
for result in reader.records() {
28+
let value = result.unwrap()[12].parse::<f64>().unwrap();
29+
// push into vector for sorting later
30+
sorted_values.push(value);
31+
// for counting benchmark time
32+
sum+=value;
33+
}
34+
// sort values in descending order
35+
sorted_values.sort_by(|a, b| b.partial_cmp(a).unwrap());
36+
let mut current_total: f64 = 0.0;
37+
// collect lows
38+
for present in sorted_values {
39+
current_total += present;
40+
if current_total >= (0.01 / 100.0 * sum) {
41+
println!("{}", 1000.0/present as f32);
42+
break;
43+
}
44+
}
45+
}
46+
47+
fn add(val1: &str, val2: &str) {
48+
let val1 = val1.parse::<f64>().unwrap();
49+
let val2 = val2.parse::<f64>().unwrap();
50+
println!("{}", val1 + val2);
51+
}
52+
53+
fn div(val1: &str, val2: &str) {
54+
let val1 = val1.parse::<f64>().unwrap();
55+
let val2 = val2.parse::<f64>().unwrap();
56+
println!("{}", val1 / val2);
57+
}
58+
59+
fn divint(val1: &str, val2: &str) {
60+
let val1 = val1.parse::<i32>().unwrap();
61+
let val2 = val2.parse::<i32>().unwrap();
62+
println!("{}", val1 / val2);
63+
}
64+
65+
fn help() {
66+
println!("{}", "
67+
Usage:
68+
calc parse <path> - calculate %0.01 lows of an OCAT CSV
69+
calc add <val1> <val2> - adds two values
70+
calc rnd <val> - rounds <val> to the nearest integer
71+
calc div <val1> <val2> - integer division
72+
calc divint <val1> <val2> - float division
73+
calc help - prints this message");
74+
}

filepicker-rs/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "filepicker"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rfd = "0.6.2"
8+
9+
[profile.release]
10+
opt-level = 3
11+
lto = true
12+
codegen-units = 1
13+
panic = "abort"

filepicker-rs/src/main.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fn main() {
2+
// get first argument as string
3+
// example "exe"
4+
let args: Vec<String> = std::env::args().collect();
5+
// check if argument is empty
6+
if args.len() == 2 {
7+
// launch prompt
8+
let res = rfd::FileDialog::new()
9+
// add file extension filter
10+
.add_filter(&args[1], &[&args[1]])
11+
.pick_file();
12+
// check if file prompt was cancelled
13+
if res == None {
14+
println!("cancelled by user");
15+
} else{
16+
println!("{}", res.unwrap().to_str().unwrap().replace("\\", "/"));
17+
}
18+
} else {
19+
let res = rfd::FileDialog::new()
20+
.add_filter("All Files", &["*.*"])
21+
.pick_file();
22+
if res == None {
23+
println!("cancelled by user");
24+
} else {
25+
println!("{}", res.unwrap().to_str().unwrap().replace("\\", "/"));
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)