Skip to content

Commit

Permalink
added serde feature flag to enable serde support for Iban type
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPeel committed Oct 26, 2023
1 parent 7003ed5 commit 77affba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust-version = "1.60.0"
default = ["std"]
std = []
rand = ["dep:rand"]
serde = ["dep:serde", "arrayvec/serde"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
Expand All @@ -21,6 +22,7 @@ all-features = true
arrayvec = { version = "0.7", default-features = false }
phf = { version = "0.11", default-features = false }
rand = { version = "0.8", default-features = false, optional = true }
serde = { version = "1.0.7", default-features = false, features = ["derive"], optional = true }

[build-dependencies]
csv = "1"
Expand All @@ -30,6 +32,7 @@ regex = "1"
serde = { version = "1.0.7", features = ["derive"] }

[dev-dependencies]
serde_json = "1"
test-case = "3"
rand = "0.8"

Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const IBAN_MAX_LENGTH: usize = 34;
/// Electronic formatting can be obtained from the [`Debug`](std::fmt::Debug), [`Deref`](std::ops::Deref),
/// or [`AsRef`](std::convert::AsRef) implementations.
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Iban(ArrayString<IBAN_MAX_LENGTH>);

/// Represents the Basic Bank Account Number (BBAN) portion of an International Bank Account Number (IBAN).
Expand Down Expand Up @@ -637,6 +638,18 @@ mod tests {
assert_eq!(iban.to_string().as_str(), "AE07 0331 2345 6789 0123 456");
}

#[cfg(feature = "serde")]
#[test]
fn iban_serde() {
let iban = Iban::parse("AD1200012030200359100100").unwrap();
let json = serde_json::to_string(&iban).unwrap();

assert_eq!(json, r#""AD1200012030200359100100""#);

let new_iban: Iban = serde_json::from_str(&json).unwrap();
assert_eq!(iban, new_iban);
}

#[test]
fn bban_display_impl() {
let iban = Iban::parse("AD1200012030200359100100").unwrap();
Expand Down

0 comments on commit 77affba

Please sign in to comment.