-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ramil Mammadov
committed
Oct 23, 2024
0 parents
commit ca094cb
Showing
13 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM rust:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "Sample Rust", | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
}, | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"rust-lang.rust-analyzer", // Updated to use rust-analyzer | ||
"ms-azuretools.vscode-docker" // Added Docker extension | ||
] | ||
} | ||
}, | ||
"postCreateCommand": "rustup update && rustup component add clippy && rustup component add rustfmt" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: CI/CD | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install Rust | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
override: true | ||
|
||
- name: Check | ||
run: make check | ||
|
||
- name: Format | ||
run: make format | ||
|
||
- name: Test | ||
run: make test | ||
|
||
- name: Release | ||
run: cargo build --release | ||
|
||
- name: Upload Binary Artifact | ||
uses: actions/upload-artifact@v4 #v3 is scheduled for deprecation Nov 30 2024 check marketplace | ||
with: | ||
name: Rust Artificat Binary | ||
path: target/release/loan_payment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "loan_payment" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
clap = { version = "4.1", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
format: | ||
rustfmt src/main.rs | ||
rustfmt src/lib.rs | ||
check: | ||
cargo check | ||
test: | ||
cargo test | ||
lint: | ||
cargo clippy |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// src/lib.rs | ||
|
||
// Function to calculate the monthly interest rate | ||
pub fn calculate_monthly_rate(annual_rate: f64) -> f64 { | ||
annual_rate / 12.0 | ||
} | ||
|
||
// Function to calculate the discount factor for the loan | ||
pub fn calculate_discount_factor(monthly_rate: f64, periods: i32) -> f64 { | ||
if monthly_rate == 0.0 { | ||
1.0 // If there's no interest rate, the discount factor doesn't apply | ||
} else { | ||
1.0 - (1.0 + monthly_rate).powi(-periods) | ||
} | ||
} | ||
|
||
// Function to calculate the monthly loan payment | ||
pub fn get_monthly_payment(principal: f64, rate: f64, periods: i32) -> f64 { | ||
let monthly_rate = calculate_monthly_rate(rate); | ||
|
||
// Handle the case where interest rate is zero | ||
if rate == 0.0 { | ||
principal / periods as f64 | ||
} else { | ||
let discount_factor = calculate_discount_factor(monthly_rate, periods); | ||
(principal * monthly_rate) / discount_factor | ||
} | ||
} | ||
|
||
// Unit tests for the above functions | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_calculate_monthly_rate() { | ||
// Test with a typical annual rate | ||
assert_eq!(calculate_monthly_rate(0.12), 0.01); | ||
|
||
// Test with a 0% annual rate | ||
assert_eq!(calculate_monthly_rate(0.0), 0.0); | ||
|
||
// Test with a negative annual rate | ||
assert_eq!(calculate_monthly_rate(-0.12), -0.01); | ||
} | ||
|
||
#[test] | ||
fn test_calculate_discount_factor() { | ||
// Test with a typical input | ||
let discount_factor = calculate_discount_factor(0.01, 12); | ||
|
||
// Test with zero monthly rate (discount factor should be 1.0) | ||
assert_eq!(calculate_discount_factor(0.0, 12), 1.0); | ||
} | ||
|
||
#[test] | ||
fn test_get_monthly_payment() { | ||
// Test with typical inputs | ||
let payment = get_monthly_payment(100000.0, 0.05, 360); | ||
assert!((payment - 536.82).abs() < 0.01); // Margin of error | ||
|
||
// Test with zero interest rate (adjusted logic) | ||
assert_eq!(get_monthly_payment(100000.0, 0.0, 360), 277.77777777777777); // Principal / periods | ||
} | ||
} |
Oops, something went wrong.