Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/iterator #25

Merged
merged 7 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.check.features": ["iterator", "with-serde"]
}
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ keywords = ["currency", "iso", "iso-4217", "iso4217"]
[features]
default = []
with-serde = ["serde"]
iterator = ["strum"]

[dependencies]
iso_country = "0.1.4"
serde = { version = "1.0.127", optional = true, features = ["derive"] }
strum = {version = "0.25.0", optional = true, features = ["derive"] }

[dev-dependencies]
serde_json = "1.0.66"
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ The `Country` enum is re-exported from the only dependency - the [iso_country](h

## Features

The crate has only one optional feature - `with-serde`. If you need serialization/deserialization support using `serde` you should include the feature in your dependency on `iso_currency`, for example like this:
The crate has only two optional features:

```toml
iso_currency = { version = "0.4.3", features = ["with-serde"] }
- `with-serde`
- `iterator`

### 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](https://crates.io/crates/strum) `EnumIter` trait on `Currency`, which provides an iterator over all variants of it. Here's an example usage:

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

## Examples
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ fn write_enum(file: &mut BufWriter<File>, data: &[IsoData]) {
"#[cfg_attr(feature = \"with-serde\", derive(Serialize, Deserialize))]"
)
.unwrap();
writeln!(
file,
"#[cfg_attr(feature = \"iterator\", derive(EnumIter))]"
)
.unwrap();
writeln!(
file,
"#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]"
Expand Down
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub use iso_country::Country;
#[cfg(feature = "with-serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "iterator")]
use strum::EnumIter;
#[cfg(feature = "iterator")]
pub use strum::IntoEnumIterator;

include!(concat!(env!("OUT_DIR"), "/isodata.rs"));

#[derive(PartialEq, Eq)]
Expand Down Expand Up @@ -105,8 +110,6 @@ impl std::str::FromStr for Currency {
mod tests {
use crate::{Country, Currency, ParseCurrencyError};

#[cfg(feature = "with-serde")]
use serde_json;
#[cfg(feature = "with-serde")]
use std::collections::HashMap;

Expand Down Expand Up @@ -216,4 +219,13 @@ mod tests {
assert_eq!(Currency::from_str("BGN"), Ok(Currency::BGN));
assert_eq!(Currency::from_str("AAA"), Err(ParseCurrencyError));
}

#[test]
#[cfg(feature = "iterator")]
fn test_iterator() {
use crate::IntoEnumIterator;
let mut iter = Currency::iter();
assert_eq!(iter.next(), Some(Currency::AED));
assert_eq!(iter.next(), Some(Currency::AFN));
}
}
Loading