Skip to content

Commit

Permalink
fix(utils): add to_state_code to router crate (#7293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakilmostak authored Feb 17, 2025
1 parent 1606eb6 commit 7691108
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,8 @@ pub trait AddressDetailsData {
fn get_zip(&self) -> Result<&Secret<String>, Error>;
fn get_country(&self) -> Result<&api_models::enums::CountryAlpha2, Error>;
fn get_combined_address_line(&self) -> Result<Secret<String>, Error>;
fn to_state_code(&self) -> Result<Secret<String>, Error>;
fn to_state_code_as_optional(&self) -> Result<Option<Secret<String>>, Error>;
fn get_optional_line2(&self) -> Option<Secret<String>>;
fn get_optional_country(&self) -> Option<api_models::enums::CountryAlpha2>;
}
Expand Down Expand Up @@ -1894,6 +1896,31 @@ impl AddressDetailsData for hyperswitch_domain_models::address::AddressDetails {
self.get_line2()?.peek()
)))
}
fn to_state_code(&self) -> Result<Secret<String>, Error> {
let country = self.get_country()?;
let state = self.get_state()?;
match country {
api_models::enums::CountryAlpha2::US => Ok(Secret::new(
UsStatesAbbreviation::foreign_try_from(state.peek().to_string())?.to_string(),
)),
api_models::enums::CountryAlpha2::CA => Ok(Secret::new(
CanadaStatesAbbreviation::foreign_try_from(state.peek().to_string())?.to_string(),
)),
_ => Ok(state.clone()),
}
}
fn to_state_code_as_optional(&self) -> Result<Option<Secret<String>>, Error> {
self.state
.as_ref()
.map(|state| {
if state.peek().len() == 2 {
Ok(state.to_owned())
} else {
self.to_state_code()
}
})
.transpose()
}

fn get_optional_line2(&self) -> Option<Secret<String>> {
self.line2.clone()
Expand Down

0 comments on commit 7691108

Please sign in to comment.