Browse Source

added error catching - fixes #2

tags/0.2.0
theycallmemac 3 years ago
parent
commit
baa29646d6
5 changed files with 48 additions and 22 deletions
  1. +3
    -0
      .travis.yml
  2. +13
    -13
      Cargo.lock
  3. +1
    -1
      Cargo.toml
  4. +19
    -2
      README.md
  5. +12
    -6
      src/main.rs

+ 3
- 0
.travis.yml View File

@@ -4,3 +4,6 @@ rust:
- beta
- nightly
cache: cargo

notification:
email: false

+ 13
- 13
Cargo.lock View File

@@ -71,6 +71,19 @@ dependencies = [
"vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "convert"
version = "0.1.1"
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)",
"env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"json 0.11.13 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "curl"
version = "0.2.19"
@@ -321,19 +334,6 @@ name = "pkg-config"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "convert"
version = "0.1.1"
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)",
"env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"json 0.11.13 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "pnacl-build-helper"
version = "1.4.11"


+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
[package]
name = "convert"
version = "0.1.1"
version = "0.2.0"
authors = ["theycallmemac <james.mcdermott89@gmail.com>"]
description = "A command line tool written in rust which converts between currencies."
categories = ["command-line-interface","command-line-utilities","parsing","algorithms"]


+ 19
- 2
README.md View File

@@ -1,11 +1,28 @@
# convert

[![Build Status](https://travis-ci.com/theycallmemac/convert.svg?token=LXpTJmtqBSjEDxSB5vRf&branch=master)](https://travis-ci.com/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)
[![Build Status](https://travis-ci.org/theycallmemac/convert.svg?token=LXpTJmtqBSjEDxSB5vRf&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

#### Version:
To check the current version of __convert__:
```convert -V```

#### Conversions:
To carry out any given conversion between currencies try something like:

```convert 180.50 EUR USD```

The above command will convert EUR180.50 to USD using realtime information.
This will convert EUR 180.50 to it's USD equivalent using real-time information.

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

```convert -l```

#### Help

To see how __convert__ works:
```convert -h```

+ 12
- 6
src/main.rs View File

@@ -47,7 +47,7 @@ fn main() {
fn run() -> Result<(), Error> {
env_logger::init()?;
let matches = App::new("convert")
.version("0.1.0")
.version("0.2.0")
.author("James McDermott <james.mcdermott89@gmail.com>")
.about("A command line tool written in rust which converts between currencies.")
.arg(Arg::with_name("list")
@@ -55,7 +55,7 @@ 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("amount")
.required(false)
.takes_value(true)
@@ -80,11 +80,15 @@ fn run() -> Result<(), Error> {
.exec()
.unwrap();
let body = std::str::from_utf8(response.get_body()).unwrap();
let json: Value = serde_json::from_str(body).unwrap();
let obj = json.as_object().and_then(|object| object.get("rates")).and_then(|links| links.as_object()).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");
});
let mut count = 0;
for (curr, val) in obj.iter() {
if (count == 4) {
if count == 4 {
println!("");
count = 0;
}
@@ -105,7 +109,9 @@ fn run() -> Result<(), Error> {
.exec()
.unwrap();
let body = std::str::from_utf8(response.get_body()).unwrap();
let data = json::parse(body).unwrap();
let data = json::parse(body).unwrap_or_else(|e| {
panic!("Failed to parse json; error is {}", e);
});
let rate = data["rates"][desired].to_string().parse::<f32>()?;
let conversion = Conversion::new(amount, rate);
println!("{} {} is worth {:.2} {}", conversion.amount, base, conversion.amount * conversion.rate, desired);


Loading…
Cancel
Save