Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaschaplin committed Jun 8, 2023
1 parent b46f2aa commit 9d3f3ea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: docker login
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -25,7 +25,7 @@ jobs:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -39,7 +39,7 @@ jobs:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -55,7 +55,7 @@ jobs:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Thomas Chaplin
Copyright (c) 2023 Thomas Chaplin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
40 changes: 0 additions & 40 deletions src/lib.rs

This file was deleted.

43 changes: 40 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
mod lib;
use lib::calculate_percentage_change;
use lib::get_input;
use std::env::args;

fn main() {
let first = get_input(1);
let second = get_input(2);
let percentage_change = calculate_percentage_change(first, second);
println!("{}", percentage_change);
}

pub fn get_input(index: usize) -> f32 {
let args: Vec<String> = args().collect();
let result: f32 = match args[index].trim().parse() {
Ok(num) => num,
Err(_) => panic!(
"Failed to read value \"{}\", we were expecting a u32 or f32!",
args[index].trim()
),
};
result
}

pub fn calculate_percentage_change(first: f32, second: f32) -> String {
if first < second {
return format!("+{}%", (second - first) / first * 100.0);
}
if first > second {
format!("-{}%", (first - second) / first * 100.0)
} else {
"0%".to_string()
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_calculate_percentage_change() {
assert_eq!(calculate_percentage_change(1_f32, 2_f32), "+100%");
assert_eq!(calculate_percentage_change(2_f32, 1_f32), "-50%");
assert_eq!(calculate_percentage_change(1_f32, 1_f32), "0%");
assert_eq!(calculate_percentage_change(1.2, 2.4), "+100%");
assert_eq!(calculate_percentage_change(2.4, 1.2), "-50%");
assert_eq!(calculate_percentage_change(1.1, 1.1), "0%");
}
}

0 comments on commit 9d3f3ea

Please sign in to comment.