Skip to content

Commit

Permalink
Add rewrite of calc.py/filepicker in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Zusier committed Jan 14, 2022
1 parent 805a98e commit 920479c
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 6 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/calc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Calc

on:
push:
branches: [ main ]
paths:
- calc/**
pull_request:
branches: [ main ]
paths:
- calc/**
workflow_dispatch:

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose --release --manifest-path ./calc/Cargo.toml
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2.3.1
with:
# Artifact name
# optional, default is artifact
# A file, directory or wildcard pattern that describes what to upload
path: D:\a\Atlas-Utilities\Atlas-Utilities\calc\target\release\calc.exe
12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
# MacOS
*.DS_Store


# pyinstaller builds
build/
dist/
*.spec
__pycache__/
# Rust
/target/
**/target/
**/Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
19 changes: 19 additions & 0 deletions calc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "calc"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
csv = "1.1.6"

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
debug = 0

[profile.dev]
debug = 1
74 changes: 74 additions & 0 deletions calc/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
match args[1].as_ref() {
"parse" => parse(args[2].as_ref()),
"add" => add(args[2].as_ref(), args[3].as_ref()),
"rnd" => println!("{}", args[2].parse::<f64>().unwrap().round()),
"div" => div(args[2].as_ref(), args[3].as_ref()),
"divint" => divint(args[2].as_ref(), args[3].as_ref()),
"help" => help(),
_ => help(),
}
} else {
help();
}
}

fn parse(path: &str) {
// OCAT CSVs change amount of headers after the first line, so make reader flexible
let mut reader = csv::ReaderBuilder::new()
.has_headers(true)
.flexible(true)
.from_path(path)
.unwrap();
let mut sum: f64 = 0.0;
let mut sorted_values: Vec<f64> = Vec::new();
for result in reader.records() {
let value = result.unwrap()[12].parse::<f64>().unwrap();
// push into vector for sorting later
sorted_values.push(value);
// for counting benchmark time
sum+=value;
}
// sort values in descending order
sorted_values.sort_by(|a, b| b.partial_cmp(a).unwrap());
let mut current_total: f64 = 0.0;
// collect lows
for present in sorted_values {
current_total += present;
if current_total >= (0.01 / 100.0 * sum) {
println!("{}", 1000.0/present as f32);
break;
}
}
}

fn add(val1: &str, val2: &str) {
let val1 = val1.parse::<f64>().unwrap();
let val2 = val2.parse::<f64>().unwrap();
println!("{}", val1 + val2);
}

fn div(val1: &str, val2: &str) {
let val1 = val1.parse::<f64>().unwrap();
let val2 = val2.parse::<f64>().unwrap();
println!("{}", val1 / val2);
}

fn divint(val1: &str, val2: &str) {
let val1 = val1.parse::<i32>().unwrap();
let val2 = val2.parse::<i32>().unwrap();
println!("{}", val1 / val2);
}

fn help() {
println!("{}", "
Usage:
calc parse <path> - calculate %0.01 lows of an OCAT CSV
calc add <val1> <val2> - adds two values
calc rnd <val> - rounds <val> to the nearest integer
calc div <val1> <val2> - integer division
calc divint <val1> <val2> - float division
calc help - prints this message");
}
13 changes: 13 additions & 0 deletions filepicker-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "filepicker"
version = "0.1.0"
edition = "2021"

[dependencies]
rfd = "0.6.2"

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
28 changes: 28 additions & 0 deletions filepicker-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
fn main() {
// get first argument as string
// example "exe"
let args: Vec<String> = std::env::args().collect();
// check if argument is empty
if args.len() == 2 {
// launch prompt
let res = rfd::FileDialog::new()
// add file extension filter
.add_filter(&args[1], &[&args[1]])
.pick_file();
// check if file prompt was cancelled
if res == None {
println!("cancelled by user");
} else{
println!("{}", res.unwrap().to_str().unwrap().replace("\\", "/"));
}
} else {
let res = rfd::FileDialog::new()
.add_filter("All Files", &["*.*"])
.pick_file();
if res == None {
println!("cancelled by user");
} else {
println!("{}", res.unwrap().to_str().unwrap().replace("\\", "/"));
}
}
}

0 comments on commit 920479c

Please sign in to comment.