iban-ex/lib/iban_ex/country.ex

106 lines
3.4 KiB
Elixir
Raw Permalink Normal View History

2024-03-05 11:02:58 +00:00
defmodule IbanEx.Country do
@moduledoc false
2024-03-05 11:02:58 +00:00
import IbanEx.Commons, only: [normalize: 1]
@type country_code() :: <<_::16>> | atom()
@type error_tuple() :: {:error, atom()}
@supported_countries %{
2024-05-14 23:15:55 +00:00
"AD" => IbanEx.Country.AD,
"AE" => IbanEx.Country.AE,
"AL" => IbanEx.Country.AL,
2024-03-05 11:02:58 +00:00
"AT" => IbanEx.Country.AT,
2024-05-14 23:15:55 +00:00
"AZ" => IbanEx.Country.AZ,
"BA" => IbanEx.Country.BA,
2024-03-05 11:02:58 +00:00
"BE" => IbanEx.Country.BE,
"BG" => IbanEx.Country.BG,
2024-05-14 23:15:55 +00:00
"BH" => IbanEx.Country.BH,
"BR" => IbanEx.Country.BR,
2024-03-05 11:02:58 +00:00
"CH" => IbanEx.Country.CH,
2024-05-14 23:15:55 +00:00
"CR" => IbanEx.Country.CR,
2024-03-05 11:02:58 +00:00
"CY" => IbanEx.Country.CY,
"CZ" => IbanEx.Country.CZ,
"DE" => IbanEx.Country.DE,
2024-05-14 23:15:55 +00:00
"DO" => IbanEx.Country.DO,
2024-03-05 11:02:58 +00:00
"DK" => IbanEx.Country.DK,
"EE" => IbanEx.Country.EE,
"ES" => IbanEx.Country.ES,
2024-05-14 23:15:55 +00:00
"EG" => IbanEx.Country.EG,
2024-03-05 11:02:58 +00:00
"FI" => IbanEx.Country.FI,
"FR" => IbanEx.Country.FR,
2024-05-14 23:15:55 +00:00
"FO" => IbanEx.Country.FO,
2024-03-05 11:02:58 +00:00
"GB" => IbanEx.Country.GB,
2024-05-14 23:15:55 +00:00
"GE" => IbanEx.Country.GE,
"GI" => IbanEx.Country.GI,
2024-05-14 23:15:55 +00:00
"GL" => IbanEx.Country.GL,
"GR" => IbanEx.Country.GR,
2024-05-14 23:15:55 +00:00
"GT" => IbanEx.Country.GT,
2024-03-05 11:02:58 +00:00
"HR" => IbanEx.Country.HR,
"HU" => IbanEx.Country.HU,
"IE" => IbanEx.Country.IE,
2024-05-14 23:15:55 +00:00
"IL" => IbanEx.Country.IL,
"IT" => IbanEx.Country.IT,
"IS" => IbanEx.Country.IS,
2024-05-14 23:15:55 +00:00
"JO" => IbanEx.Country.JO,
"KZ" => IbanEx.Country.KZ,
"KW" => IbanEx.Country.KW,
"LB" => IbanEx.Country.LB,
"LI" => IbanEx.Country.LI,
2024-03-05 11:02:58 +00:00
"LT" => IbanEx.Country.LT,
"LU" => IbanEx.Country.LU,
"LV" => IbanEx.Country.LV,
"MC" => IbanEx.Country.MC,
2024-05-14 23:15:55 +00:00
"MD" => IbanEx.Country.MD,
"ME" => IbanEx.Country.ME,
"MK" => IbanEx.Country.MK,
"MR" => IbanEx.Country.MR,
2024-03-05 11:02:58 +00:00
"MT" => IbanEx.Country.MT,
"NL" => IbanEx.Country.NL,
"NO" => IbanEx.Country.NO,
2024-03-05 11:02:58 +00:00
"PL" => IbanEx.Country.PL,
"PT" => IbanEx.Country.PT,
2024-05-14 23:15:55 +00:00
"PK" => IbanEx.Country.PK,
"QA" => IbanEx.Country.QA,
"RO" => IbanEx.Country.RO,
2024-05-14 23:15:55 +00:00
"RS" => IbanEx.Country.RS,
"SA" => IbanEx.Country.SA,
"SE" => IbanEx.Country.SE,
"SI" => IbanEx.Country.SI,
"SK" => IbanEx.Country.SK,
2024-05-14 23:15:55 +00:00
"SM" => IbanEx.Country.SM,
"SV" => IbanEx.Country.SV,
"TL" => IbanEx.Country.TL,
"TR" => IbanEx.Country.TR,
"UA" => IbanEx.Country.UA,
2024-05-14 23:15:55 +00:00
"VA" => IbanEx.Country.VA,
"VG" => IbanEx.Country.VG,
"XK" => IbanEx.Country.XK
2024-03-05 11:02:58 +00:00
}
@supported_country_codes Map.keys(@supported_countries)
@supported_country_modules Map.values(@supported_countries)
2024-03-05 11:02:58 +00:00
@spec supported_countries() :: map()
defp supported_countries(), do: @supported_countries
@spec supported_country_codes() :: [country_code()] | []
def supported_country_codes(), do: @supported_country_codes
@spec supported_country_modules() :: [module()] | []
def supported_country_modules(), do: @supported_country_modules
2024-05-14 23:15:55 +00:00
@spec country_module(country_code) :: module() | error_tuple()
2024-03-05 11:02:58 +00:00
def country_module(country_code) when is_binary(country_code) or is_atom(country_code) do
normalized_country_code = normalize(country_code)
case is_country_code_supported?(normalized_country_code) do
true -> supported_countries()[normalized_country_code]
_ -> {:error, :unsupported_country_code}
end
end
@spec is_country_code_supported?(country_code()) :: boolean()
def is_country_code_supported?(country_code) when is_binary(country_code) or is_atom(country_code),
do: Enum.member?(@supported_country_codes, normalize(country_code))
end