-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
302 additions
and
51 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
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
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,78 @@ | ||
use clap::{Arg, ArgMatches, Command}; | ||
|
||
/// Builds the CLI command structure for the Compound Interest Calculator. | ||
/// | ||
/// This function defines the main command and its arguments, as well as a subcommand for server mode. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A `Command` instance configured with the necessary arguments and subcommands. | ||
pub fn build_cli() -> Command { | ||
Command::new("Compound Interest Calculator") | ||
.about("cis - Calculates Compound Interest.\nOutput the results of compound interest calculations as either a line graph image or JSON.") | ||
.arg( | ||
Arg::new("principal") | ||
.short('p') | ||
.long("principal") | ||
.value_name("PRINCIPAL") | ||
.help("The principal at the time you started investing. Defaults to 0"), | ||
) | ||
.arg( | ||
Arg::new("contribution") | ||
.short('c') | ||
.long("contribution") | ||
.value_name("CONTRIBUTION") | ||
.help("The monthly contribution amount. Defaults to 1"), | ||
) | ||
.arg( | ||
Arg::new("rate") | ||
.short('r') | ||
.long("rate") | ||
.value_name("RATE") | ||
.help("The annual interest rate (in %). Defaults to 5"), | ||
) | ||
.arg( | ||
Arg::new("years") | ||
.short('y') | ||
.long("years") | ||
.value_name("YEARS") | ||
.help("The number of years for contributions. Defaults to 5"), | ||
) | ||
.arg( | ||
Arg::new("json") | ||
.short('j') | ||
.long("json") | ||
.help("Output as JSON. Defaults to false") | ||
.action(clap::ArgAction::SetTrue), | ||
) | ||
.subcommand( | ||
Command::new("server") | ||
.about("Starts the server mode") | ||
.arg( | ||
Arg::new("port") | ||
.short('p') | ||
.long("port") | ||
.value_name("PORT") | ||
.help("The port to run the server on. Defaults to 8080"), | ||
), | ||
) | ||
} | ||
|
||
/// Retrieves the port number from the CLI matches. | ||
/// | ||
/// This function extracts and parses the port number from the subcommand matches. If no port is | ||
/// specified, it returns the default port 8080. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `matches` - The `ArgMatches` instance containing the parsed CLI arguments and subcommands. | ||
/// | ||
/// # Returns | ||
/// | ||
/// The port number as a `u16`. | ||
pub fn get_port(matches: &ArgMatches) -> u16 { | ||
matches | ||
.get_one::<String>("port") | ||
.and_then(|s| s.parse().ok()) | ||
.unwrap_or(8080) | ||
} |
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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod args; | ||
pub mod calculations; | ||
pub mod server; |
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 |
---|---|---|
@@ -1,72 +1,45 @@ | ||
mod args; | ||
mod calculations; | ||
mod server; | ||
|
||
use calculations::{plot_summary, Investment}; | ||
use clap::{Arg, Command}; | ||
use serde_json::to_string_pretty; | ||
use std::env; | ||
|
||
fn run() { | ||
let matches = Command::new("Compound Interest Calculator") | ||
.about("cis - Calculates Compound Interest.\nOutput the results of compound interest calculations as either a line graph image or JSON.") | ||
.arg( | ||
Arg::new("principal") | ||
.short('p') | ||
.long("principal") | ||
.value_name("PRINCIPAL") | ||
.help("The principal at the time you started investing. Defaults to 0"), | ||
) | ||
.arg( | ||
Arg::new("contribution") | ||
.short('c') | ||
.long("contribution") | ||
.value_name("CONTRIBUTION") | ||
.help("The monthly contribution amount. Defaults to 1"), | ||
) | ||
.arg( | ||
Arg::new("rate") | ||
.short('r') | ||
.long("rate") | ||
.value_name("RATE") | ||
.help("The annual interest rate (in %). Defaults to 5"), | ||
) | ||
.arg( | ||
Arg::new("years") | ||
.short('y') | ||
.long("years") | ||
.value_name("YEARS") | ||
.help("The number of years for contributions. Defaults to 5"), | ||
) | ||
.arg( | ||
Arg::new("json") | ||
.short('j') | ||
.long("json") | ||
.help("Output as JSON. Defaults to false") | ||
.action(clap::ArgAction::SetTrue), | ||
) | ||
.get_matches(); | ||
async fn run() -> std::io::Result<()> { | ||
let matches = args::build_cli().get_matches(); | ||
|
||
let args: Vec<String> = env::args().collect(); | ||
if args.len() == 1 { | ||
println!("`cls --help` for usage"); | ||
return; | ||
return Ok(()); | ||
} | ||
|
||
if let Some(matches) = matches.subcommand_matches("server") { | ||
let port = args::get_port(&matches); | ||
if let Err(e) = server::start_server(port).await { | ||
eprintln!("Failed to start server: {}", e); | ||
} | ||
return Ok(()); | ||
} | ||
|
||
let investment = Investment::from_matches(&matches); | ||
// Display the yearly summary | ||
let summary = investment.yearly_summary(); | ||
if matches.get_flag("json") { | ||
match to_string_pretty(&summary) { | ||
Ok(json) => println!("{}", json), | ||
Err(e) => eprintln!("Failed to serialize to JSON: {}", e), | ||
} | ||
} else { | ||
match plot_summary(&summary) { | ||
Ok(_) => (), | ||
Err(e) => eprintln!("Failed to plot summary: {}", e), | ||
} | ||
return Ok(()); | ||
} | ||
match plot_summary(&summary) { | ||
Ok(_) => (), | ||
Err(e) => eprintln!("Failed to plot summary: {}", e), | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
run(); | ||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
run().await | ||
} |
Oops, something went wrong.