2个版本

0.1.1 2022年8月26日
0.1.0 2022年8月24日

#263 in 财经

MIT/Apache

9KB
119

currencyapi_parse

一个解析currencyapi latest 响应的库。

这个库不解析响应JSON,只是利用API的规律性。它有一些对规律的检查,所以至少对于日常使用来说应该是可以的。

用法

使用 Scanner 遍历货币 Entry

示例

# 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(()) }

依赖项

~105KB