Skip to content

Commit

Permalink
Merge pull request #52 from ThomasdenH/edition-and-docs
Browse files Browse the repository at this point in the history
Update Cargo.toml and add `AsRef` and `AsMut`
  • Loading branch information
ThomasdenH authored Oct 31, 2024
2 parents 32ab706 + 6c73acb commit 5a6efec
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 5.1.0
- Add implementations of `AsRef` and `AsMut`
- Enable all features on `docs.rs`
- Remove now default `intra_rustdoc_links` feature flag

# 5.0.1
- Update registry to latest version: Release 98.
- Update country specific generation code. Inconsistencies are now resolved manually when generating the country specific code. Incorrect examples are now fixed before testing where they were previously ignored.
Expand Down
8 changes: 5 additions & 3 deletions iban_validate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Thomas den Hollander <denhollander.thomas@gmail.com>"]
description = "A small crate to verify IBAN account numbers."
repository = "https://github.com/ThomasdenH/iban_check"
license = "MIT OR Apache-2.0"
readme = "../README.md"
readme = "README.md"
keywords = ["iban", "iban-validator"]
categories = ["parsing"]
edition = "2021"
Expand All @@ -21,8 +21,10 @@ path = "src/lib.rs"

[features]
default = []
# Builds rustdoc links, but requires nightly
intra_rustdoc_links = []

# Enables all features when building documentation
[package.metadata.docs.rs]
features = ["serde"]

[dependencies.serde]
version = "1"
Expand Down
24 changes: 24 additions & 0 deletions iban_validate/src/base_iban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ impl fmt::Display for ParseBaseIbanError {

impl Error for ParseBaseIbanError {}

impl AsRef<ParseBaseIbanError> for ParseBaseIbanError {
fn as_ref(&self) -> &ParseBaseIbanError {
self
}
}

impl AsMut<ParseBaseIbanError> for ParseBaseIbanError {
fn as_mut(&mut self) -> &mut ParseBaseIbanError {
self
}
}

impl BaseIban {
/// Compute the checksum for the address. The code that the string contains
/// only valid characters: `'0'..='9'` and `'A'..='Z'`.
Expand Down Expand Up @@ -372,3 +384,15 @@ impl<'a> TryFrom<&'a str> for BaseIban {
value.parse()
}
}

impl AsRef<BaseIban> for BaseIban {
fn as_ref(&self) -> &BaseIban {
self
}
}

impl AsMut<BaseIban> for BaseIban {
fn as_mut(&mut self) -> &mut BaseIban {
self
}
}
30 changes: 30 additions & 0 deletions iban_validate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ impl Error for ParseIbanError {
}
}

impl AsRef<ParseIbanError> for ParseIbanError {
fn as_ref(&self) -> &ParseIbanError {
self
}
}

impl AsMut<ParseIbanError> for ParseIbanError {
fn as_mut(&mut self) -> &mut ParseIbanError {
self
}
}

impl<'a> TryFrom<&'a str> for Iban {
type Error = ParseIbanError;
/// Parse an IBAN without taking the BBAN into consideration.
Expand Down Expand Up @@ -365,6 +377,24 @@ impl str::FromStr for Iban {
}
}

impl AsRef<Iban> for Iban {
fn as_ref(&self) -> &Iban {
self
}
}

impl AsMut<Iban> for Iban {
fn as_mut(&mut self) -> &mut Iban {
self
}
}

impl AsRef<BaseIban> for Iban {
fn as_ref(&self) -> &BaseIban {
&self.base_iban
}
}

#[cfg(feature = "serde")]
impl Serialize for Iban {
#[inline]
Expand Down
21 changes: 21 additions & 0 deletions iban_validate/tests/as_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use core::error::Error;

use iban::{BaseIban, Iban};

/// [`Iban`] implements [`AsRef`] to [`BaseIban`]. Test it here and see if it
/// displays the same.
#[test]
fn test_as_ref() -> Result<(), Box<dyn Error>> {
let iban: Iban = "KW81CBKU0000000000001234560101".parse()?;

fn pretty_format(base_iban: impl AsRef<BaseIban>) -> String {
let base_iban: &BaseIban = base_iban.as_ref();
base_iban.to_string()
}

let s = pretty_format(iban);
assert_eq!(s.as_str(), "KW81 CBKU 0000 0000 0000 1234 5601 01");
assert_eq!(iban.to_string(), s);

Ok(())
}
19 changes: 15 additions & 4 deletions iban_validate/tests/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ assert_impl_all!(
TryFrom<&'static str>,
Send,
Sync,
From<Iban>
From<Iban>,
AsRef<BaseIban>,
AsMut<BaseIban>
);
assert_impl_all!(
Iban: Copy,
Expand All @@ -33,7 +35,12 @@ assert_impl_all!(
TryFrom<&'static str>,
Send,
Sync,
Into<BaseIban>
Into<BaseIban>,
// We can convert between references. We cannot convert between mutable
// references, since a changed BaseIban may not be a valid Iban anymore.
AsRef<BaseIban>,
AsRef<Iban>,
AsMut<Iban>
);
assert_impl_all!(
ParseBaseIbanError: Copy,
Expand All @@ -45,7 +52,9 @@ assert_impl_all!(
Send,
Sync,
Display,
Into<ParseIbanError>
Into<ParseIbanError>,
AsRef<ParseBaseIbanError>,
AsMut<ParseBaseIbanError>
);
assert_impl_all!(
ParseIbanError: Copy,
Expand All @@ -57,7 +66,9 @@ assert_impl_all!(
Send,
Sync,
Display,
From<ParseBaseIbanError>
From<ParseBaseIbanError>,
AsRef<ParseIbanError>,
AsMut<ParseIbanError>
);

assert_impl_all!(ParseBaseIbanError: core::error::Error);
Expand Down

0 comments on commit 5a6efec

Please sign in to comment.