13 releases

new 0.5.1 Nov 18, 2024
0.4.4 Aug 26, 2023
0.4.3 Jun 7, 2022
0.4.2 Aug 9, 2021
0.2.0 Oct 13, 2019

#1 in Finance

Download history 4590/week @ 2024-07-30 5824/week @ 2024-08-06 3741/week @ 2024-08-13 5025/week @ 2024-08-20 5122/week @ 2024-08-27 4299/week @ 2024-09-03 5918/week @ 2024-09-10 4904/week @ 2024-09-17 4906/week @ 2024-09-24 5601/week @ 2024-10-01 5657/week @ 2024-10-08 5068/week @ 2024-10-15 4796/week @ 2024-10-22 3659/week @ 2024-10-29 4647/week @ 2024-11-05 5093/week @ 2024-11-12

19,297 downloads per month
Used in 20 crates (17 directly)

MIT license

42KB
823 lines

ISO 4217 currency codes

docs badge build badge crates badge license badge

This crate provides an enum that represents all ISO 4217 currencies and has simple methods to convert between numeric and character code, list of territories where each currency is used, the symbol, and the English name of the currency.

The data for this is taken from https://en.wikipedia.org/wiki/ISO_4217

The Country enum is re-exported from the only dependency - the iso_country crate.

Features

The crate has some optional features:

  • with-serde
  • iterator
  • with-schemars
  • with-sqlx-sqlite
  • with-sqlx-postgres
  • with-sqlx-mysql

with-serde

If you need serialization/deserialization support using serde you should include the feature in your dependency on iso_currency.

This would derive serde's Serialize and Deserialize on Currency.

iterator

If you specify the iterator feature on iso_currency, it will derive strum's EnumIter trait on Currency, which provides an iterator over all variants of it. Here's an example usage:

use iso_currency::IntoEnumIterator;
let mut iter = Currency::iter();

with-schemars

If you need to generate a JSON schema for your project, you can use the with-schemars feature. This will derive schemars's JsonSchema trait on Currency.

NOTE: This feature enables with-serde as well.

with-sqlx-sqlite

Implements the Type and Decode traits from sqlx version >0.7 for SQLite on the Currency struct.

with-sqlx-postgres

Implements the Type and Decode traits from sqlx version >0.7 for PostgreSQL on the Currency struct.

with-sqlx-mysql

Implements the Type and Decode traits from sqlx version >0.7 for MySQL on the Currency struct.

Examples

use iso_currency::{Currency, Country};

assert_eq!(Currency::EUR.name(), "Euro");
assert_eq!(Currency::EUR.numeric(), 978);
assert_eq!(Currency::from_numeric(978), Some(Currency::EUR));
assert_eq!(Currency::from_code("EUR"), Some(Currency::EUR));
assert_eq!(Currency::CHF.used_by(), vec![Country::LI, Country::CH]);
assert_eq!(format!("{}", Currency::EUR.symbol()), "");
assert_eq!(Currency::EUR.subunit_fraction(), Some(100));
assert_eq!(Currency::JPY.exponent(), Some(0));

Want to help improve the data?

The Currency enum and its implementations are generated from the isodata.tsv file. It is a table of <tab> separated values. If you wanna correct some value or add some missing values you just need to make a pull request editing that table.

One thing to watch out for is to have always the same amount of fields on a row, even if an optional field is missing. This means on each row you should have 6 tabs.

The used_by_alpha2 column is a bit different. It can be empty but if not it includes a list, separated by a semicolon (without a trailing semicolon), of ISO 3166-1 2-letter country codes in all caps.

Dependencies

~0–14MB
~189K SLoC