Crates.io | currencyapi_parse |
lib.rs | currencyapi_parse |
version | 0.1.1 |
source | src |
created_at | 2022-08-24 19:05:09.125521 |
updated_at | 2022-08-26 08:34:25.506676 |
description | Quick & dirty currencyapi parsing |
homepage | https://github.com/yehuthi/currencyapi_parse |
repository | https://github.com/yehuthi/currencyapi_parse |
max_upload_size | |
id | 651763 |
size | 8,437 |
A library to parse currencyapi latest
response.
This library doesn't parse the response JSON, it just takes advantage of the regularity in the API. It has some sanity-checks for the regularity so it should be fine at least for casual use.
Use the [Scanner
] to iterate over currency [Entry
]s.
# fn main() -> Result<(), Box<dyn std::error::Error>> {
// sample response from /latest USD & EUR: https://api.currencyapi.com/v3/latest?apikey=<apikey>¤cies=USD%2CEUR
let response = r#"{"meta":{"last_updated_at":"2022-08-23T23:59:59Z"},"data":{"EUR":{"code":"EUR","value":1.003367},"USD":{"code":"USD","value":1}}}"#;
let currencies = currencyapi_parse::Scanner::try_from(response)?;
for entry in currencies {
println!("{} = {:.2}",
std::str::from_utf8(&entry.currency)?,
response[entry].parse::<f64>()? // `Entry`s can conveniently index the response they come from.
);
}
# Ok(()) }