-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
107 lines (90 loc) · 2.86 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::{io, num::ParseFloatError};
use tokio::time::error::Error;
use reqwest::header::*;
// api-key file should not have a new line
const API_KEY: &'static str = include_str!("../api-key");
fn amount_from() -> f64 {
loop {
println!("How many Euros are you exchanging?");
let mut amount_from = String::new();
io::stdin()
.read_line(&mut amount_from)
.expect("Failed to read line.");
let amount_from_trimmed = amount_from.trim().parse::<f64>();
if test_input(&amount_from_trimmed) { break amount_from_trimmed.unwrap(); }
}
}
// Deprecated function since the exchange rate is obtained via API
/*
fn exchange_rate() -> f64 {
loop {
println!("What is the exchange rate?");
let mut exchange_rate = String::new();
io::stdin()
.read_line(&mut exchange_rate)
.expect("Failed to read line.");
let exchange_rate_trimmed = exchange_rate.trim().parse::<f64>();
if test_input(&exchange_rate_trimmed) { break exchange_rate_trimmed.unwrap(); }
}
}
*/
fn test_input(input: &Result<f64,ParseFloatError>) -> bool {
match input {
Ok(ok) => if ok > &0.00 {
return true
} else {
println!("Number must be positive.");
return false;
},
Err(..) => {
println!("Input is not a number.");
return false;
}
}
}
// Not that robust, but it works
fn round_up(input: f64) -> f64 {
if input % 0.01 > 0.000 {
input - (input % 0.01) + 0.01
} else {
input
}
}
// A basic program like this isn't a challenge. Let's do the real challenge using a live API
/*
fn main() {
let amount_from = amount_from();
let rate = exchange_rate();
let amount_to = round_up(amount_from / rate);
println!("{} Euros at an exchange rate of {} is {:.2} US Dollars.", amount_from, rate, amount_to);
}
*/
// Async isn't appropriate for single requests like this, but this is just for the challenge.
#[tokio::main]
async fn main() -> Result<(), Error> {
let amount_from = amount_from();
let client = reqwest::Client::new();
let url = format!(
"https://api.apilayer.com/exchangerates_data/latest?base=EUR&symbols=USD");
let response = client
.get(url)
.header("apikey", API_KEY)
.header(CONTENT_TYPE, "application/json")
.header(ACCEPT, "application/json")
.send()
.await
.unwrap()
.json::<serde_json::Value>()
.await
.unwrap();
let symbol = "USD";
let rate = response.pointer("/rates")
.unwrap()
.get(symbol)
.unwrap()
.as_f64()
.unwrap();
let amount_to = round_up(amount_from / rate);
println!("{} Euros at an exchange rate of {} is {:.2} US Dollars.", amount_from, rate, amount_to);
Ok(())
}