2 releases

0.1.1 Aug 26, 2022
0.1.0 Aug 24, 2022

#217 in Finance

21 downloads per month

MIT/Apache

9KB
119 lines

currencyapi_parse

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.

Usage

Use the Scanner to iterate over currency Entrys.

Example

# fn main() -> Result<(), Box<dyn std::error::Error>> {
// sample response from /latest USD & EUR: https://api.currencyapi.com/v3/latest?apikey=<apikey>&currencies=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(()) }

Dependencies

~115KB