Browse Source

closes #4

master
theycallmemac 3 years ago
parent
commit
635f9fa011
4 changed files with 52 additions and 17 deletions
  1. +1
    -1
      Cargo.lock
  2. +13
    -4
      README.md
  3. BIN
      src/.main.rs.swp
  4. +38
    -12
      src/main.rs

+ 1
- 1
Cargo.lock View File

@@ -73,7 +73,7 @@ dependencies = [

[[package]]
name = "convert"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)",
"curl 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",


+ 13
- 4
README.md View File

@@ -2,12 +2,11 @@

[![Build Status](https://travis-ci.org/theycallmemac/convert.svg?branch=master)](https://travis-ci.org/theycallmemac/convert) [![Crates.io](https://img.shields.io/crates/v/convert.svg)](https://crates.io/crates/convert) [![GitHub license](https://img.shields.io/github/license/theycallmemac/dcurooms.svg)](https://github.com/theycallmemac/convert/blob/master/LICENSE)

A command line tool written in rust which converts between currencies.

### Example Usage
A command line tool written in Rust which converts between 33 different currencies using real-time information.

#### Version:
To check the current version of __convert__:

```convert -V```

#### Conversions:
@@ -17,12 +16,22 @@ To carry out any given conversion between currencies try something like:

This will convert EUR 180.50 to it's USD equivalent using real-time information.

#### Get Currency Rate:
To get the current rate between to currencies use:

```convert -r JPY ZAR```

In the above example, JPY is acting as teh base currency to get the current rate of ZAR from.

#### List Supported Currencies:
To view the currencies supported by __convert__:

```convert -l```

This returns the a list of currency codes in the form of USD, EUR, JPY... etc.

#### Help

To see how __convert__ works:
To see how __convert__ works use:

```convert -h```

BIN
src/.main.rs.swp View File


+ 38
- 12
src/main.rs View File

@@ -3,19 +3,14 @@ extern crate curl;
extern crate env_logger;
extern crate serde_json;
extern crate failure;
#[macro_use]
extern crate log;
extern crate json;
#[macro_use]

use clap::{App, Arg};
use failure::Error;
use curl::http;
use json::parse;
use std::f32;
use serde_json::Value;

#[derive(Debug)]
use std::env;
struct Conversion {
amount: f32,
rate: f32,
@@ -35,7 +30,6 @@ fn main() {
use std::process::exit;

if let Err(err) = run() {
debug!("{:?}", err);
eprintln!("{}", err);
for cause in err.causes().skip(1) {
eprintln!("Caused by: {}", cause);
@@ -45,7 +39,8 @@ fn main() {
}

fn run() -> Result<(), Error> {
env_logger::init()?;
env_logger::init()?;
let args: Vec<String> = env::args().collect();
let matches = App::new("convert")
.version("0.2.0")
.author("James McDermott <james.mcdermott89@gmail.com>")
@@ -55,11 +50,19 @@ fn run() -> Result<(), Error> {
.long("list")
.value_name("list")
.takes_value(false)
.help("displays all supported currencies."))
.help("displays all supported currencies."))
.arg(Arg::with_name("rate")
.short("r")
.long("rate")
.value_name("rate")
.takes_value(false)
.help("displays the rate between two currencies."))
.arg(Arg::with_name("amount")
.required(false)
.takes_value(true)
.index(1)
.requires("amount")
.requires("desired")
.help("amount to convert from base currency to desired currency."))
.arg(Arg::with_name("base")
.required(false)
@@ -70,6 +73,7 @@ fn run() -> Result<(), Error> {
.required(false)
.takes_value(true)
.index(3)
.multiple(true)
.help("desired currency, i.e, EUR, USD, etc"))
.get_matches();
if matches.is_present("list") {
@@ -87,15 +91,37 @@ fn run() -> Result<(), Error> {
panic!("Failed to get '_links' value from json");
});
let mut count = 0;
for (curr, val) in obj.iter() {
for (curr, _val) in obj.iter() {
if count == 4 {
println!("");
println!();
count = 0;
}
print!(" - {}", curr);
count += 1;
}
println!(" - ZAR\n");
};
if matches.is_present("rate") {
let _args: Vec<String> = env::args().collect();
let (base_rate, desired_rate) = (&args[2], &args[3]);
let url = "https://api.fixer.io/latest?base=".to_string();
let url = url + base_rate;
let response = http::handle()
.get(url)
.exec()
.unwrap();
let body = std::str::from_utf8(response.get_body()).unwrap();
let json: Value = serde_json::from_str(body).unwrap_or_else(|e| {
panic!("Failed to parse json; error is {}", e);
});
let obj = json.as_object().and_then(|object| object.get("rates")).and_then(|links| links.as_object()).unwrap_or_else(|| {
panic!("Failed to get '_links' value from json");
});
for (curr, val) in obj.iter() {
if curr == desired_rate {
println!("The current rate for the {} is {:?}", curr, val);
}
}
}
else {
let amount = matches.value_of("amount").unwrap().to_string().parse::<f32>()?;
@@ -103,7 +129,7 @@ fn run() -> Result<(), Error> {
let desired = matches.value_of("desired").unwrap();
let base_link = "https://api.fixer.io/latest?base=".to_string();
let curr = &base;
let url = base_link + &curr;
let url = base_link + curr;
let response = http::handle()
.get(url)
.exec()


Loading…
Cancel
Save