retiretui_engine 0.4.1

Plan model, tax engine, and projection for RetireTui.
Documentation
//! The places a residency may name: the closed sets of country and U.S.
//! state codes, each with the name a surface shows for it.

/// A place's code as a plan file spells it, and its name.
pub type Place = (&'static str, &'static str);

/// The country whose residencies name a state.
pub const US: &str = "us";

/// The fifty states and the District of Columbia, by name. A territory is
/// a country of its own in [`COUNTRIES`].
pub const US_STATES: &[Place] = &[
    ("al", "Alabama"),
    ("ak", "Alaska"),
    ("az", "Arizona"),
    ("ar", "Arkansas"),
    ("ca", "California"),
    ("co", "Colorado"),
    ("ct", "Connecticut"),
    ("de", "Delaware"),
    ("dc", "District of Columbia"),
    ("fl", "Florida"),
    ("ga", "Georgia"),
    ("hi", "Hawaii"),
    ("id", "Idaho"),
    ("il", "Illinois"),
    ("in", "Indiana"),
    ("ia", "Iowa"),
    ("ks", "Kansas"),
    ("ky", "Kentucky"),
    ("la", "Louisiana"),
    ("me", "Maine"),
    ("md", "Maryland"),
    ("ma", "Massachusetts"),
    ("mi", "Michigan"),
    ("mn", "Minnesota"),
    ("ms", "Mississippi"),
    ("mo", "Missouri"),
    ("mt", "Montana"),
    ("ne", "Nebraska"),
    ("nv", "Nevada"),
    ("nh", "New Hampshire"),
    ("nj", "New Jersey"),
    ("nm", "New Mexico"),
    ("ny", "New York"),
    ("nc", "North Carolina"),
    ("nd", "North Dakota"),
    ("oh", "Ohio"),
    ("ok", "Oklahoma"),
    ("or", "Oregon"),
    ("pa", "Pennsylvania"),
    ("ri", "Rhode Island"),
    ("sc", "South Carolina"),
    ("sd", "South Dakota"),
    ("tn", "Tennessee"),
    ("tx", "Texas"),
    ("ut", "Utah"),
    ("vt", "Vermont"),
    ("va", "Virginia"),
    ("wa", "Washington"),
    ("wv", "West Virginia"),
    ("wi", "Wisconsin"),
    ("wy", "Wyoming"),
];

/// The ISO 3166-1 alpha-2 countries, by name.
pub const COUNTRIES: &[Place] = &[
    ("af", "Afghanistan"),
    ("ax", "Ã…land Islands"),
    ("al", "Albania"),
    ("dz", "Algeria"),
    ("ad", "Andorra"),
    ("ao", "Angola"),
    ("ai", "Anguilla"),
    ("aq", "Antarctica"),
    ("ag", "Antigua & Barbuda"),
    ("ar", "Argentina"),
    ("am", "Armenia"),
    ("aw", "Aruba"),
    ("au", "Australia"),
    ("at", "Austria"),
    ("az", "Azerbaijan"),
    ("bs", "Bahamas"),
    ("bh", "Bahrain"),
    ("bd", "Bangladesh"),
    ("bb", "Barbados"),
    ("by", "Belarus"),
    ("be", "Belgium"),
    ("bz", "Belize"),
    ("bj", "Benin"),
    ("bm", "Bermuda"),
    ("bt", "Bhutan"),
    ("bo", "Bolivia"),
    ("ba", "Bosnia & Herzegovina"),
    ("bw", "Botswana"),
    ("bv", "Bouvet Island"),
    ("br", "Brazil"),
    ("gb", "Britain (UK)"),
    ("io", "British Indian Ocean Territory"),
    ("bn", "Brunei"),
    ("bg", "Bulgaria"),
    ("bf", "Burkina Faso"),
    ("bi", "Burundi"),
    ("kh", "Cambodia"),
    ("cm", "Cameroon"),
    ("ca", "Canada"),
    ("cv", "Cape Verde"),
    ("bq", "Caribbean NL"),
    ("ky", "Cayman Islands"),
    ("cf", "Central African Rep."),
    ("td", "Chad"),
    ("cl", "Chile"),
    ("cn", "China"),
    ("cx", "Christmas Island"),
    ("cc", "Cocos (Keeling) Islands"),
    ("co", "Colombia"),
    ("km", "Comoros"),
    ("cd", "Congo (Dem. Rep.)"),
    ("cg", "Congo (Rep.)"),
    ("ck", "Cook Islands"),
    ("cr", "Costa Rica"),
    ("ci", "Côte d’Ivoire"),
    ("hr", "Croatia"),
    ("cu", "Cuba"),
    ("cw", "Curaçao"),
    ("cy", "Cyprus"),
    ("cz", "Czech Republic"),
    ("dk", "Denmark"),
    ("dj", "Djibouti"),
    ("dm", "Dominica"),
    ("do", "Dominican Republic"),
    ("tl", "East Timor"),
    ("ec", "Ecuador"),
    ("eg", "Egypt"),
    ("sv", "El Salvador"),
    ("gq", "Equatorial Guinea"),
    ("er", "Eritrea"),
    ("ee", "Estonia"),
    ("sz", "Eswatini (Swaziland)"),
    ("et", "Ethiopia"),
    ("fk", "Falkland Islands"),
    ("fo", "Faroe Islands"),
    ("fj", "Fiji"),
    ("fi", "Finland"),
    ("fr", "France"),
    ("gf", "French Guiana"),
    ("pf", "French Polynesia"),
    ("tf", "French S. Terr."),
    ("ga", "Gabon"),
    ("gm", "Gambia"),
    ("ge", "Georgia"),
    ("de", "Germany"),
    ("gh", "Ghana"),
    ("gi", "Gibraltar"),
    ("gr", "Greece"),
    ("gl", "Greenland"),
    ("gd", "Grenada"),
    ("gp", "Guadeloupe"),
    ("gu", "Guam"),
    ("gt", "Guatemala"),
    ("gg", "Guernsey"),
    ("gn", "Guinea"),
    ("gw", "Guinea-Bissau"),
    ("gy", "Guyana"),
    ("ht", "Haiti"),
    ("hm", "Heard Island & McDonald Islands"),
    ("hn", "Honduras"),
    ("hk", "Hong Kong"),
    ("hu", "Hungary"),
    ("is", "Iceland"),
    ("in", "India"),
    ("id", "Indonesia"),
    ("ir", "Iran"),
    ("iq", "Iraq"),
    ("ie", "Ireland"),
    ("im", "Isle of Man"),
    ("il", "Israel"),
    ("it", "Italy"),
    ("jm", "Jamaica"),
    ("jp", "Japan"),
    ("je", "Jersey"),
    ("jo", "Jordan"),
    ("kz", "Kazakhstan"),
    ("ke", "Kenya"),
    ("ki", "Kiribati"),
    ("kp", "Korea (North)"),
    ("kr", "Korea (South)"),
    ("kw", "Kuwait"),
    ("kg", "Kyrgyzstan"),
    ("la", "Laos"),
    ("lv", "Latvia"),
    ("lb", "Lebanon"),
    ("ls", "Lesotho"),
    ("lr", "Liberia"),
    ("ly", "Libya"),
    ("li", "Liechtenstein"),
    ("lt", "Lithuania"),
    ("lu", "Luxembourg"),
    ("mo", "Macau"),
    ("mg", "Madagascar"),
    ("mw", "Malawi"),
    ("my", "Malaysia"),
    ("mv", "Maldives"),
    ("ml", "Mali"),
    ("mt", "Malta"),
    ("mh", "Marshall Islands"),
    ("mq", "Martinique"),
    ("mr", "Mauritania"),
    ("mu", "Mauritius"),
    ("yt", "Mayotte"),
    ("mx", "Mexico"),
    ("fm", "Micronesia"),
    ("md", "Moldova"),
    ("mc", "Monaco"),
    ("mn", "Mongolia"),
    ("me", "Montenegro"),
    ("ms", "Montserrat"),
    ("ma", "Morocco"),
    ("mz", "Mozambique"),
    ("mm", "Myanmar (Burma)"),
    ("na", "Namibia"),
    ("nr", "Nauru"),
    ("np", "Nepal"),
    ("nl", "Netherlands"),
    ("nc", "New Caledonia"),
    ("nz", "New Zealand"),
    ("ni", "Nicaragua"),
    ("ne", "Niger"),
    ("ng", "Nigeria"),
    ("nu", "Niue"),
    ("nf", "Norfolk Island"),
    ("mk", "North Macedonia"),
    ("mp", "Northern Mariana Islands"),
    ("no", "Norway"),
    ("om", "Oman"),
    ("pk", "Pakistan"),
    ("pw", "Palau"),
    ("ps", "Palestine"),
    ("pa", "Panama"),
    ("pg", "Papua New Guinea"),
    ("py", "Paraguay"),
    ("pe", "Peru"),
    ("ph", "Philippines"),
    ("pn", "Pitcairn"),
    ("pl", "Poland"),
    ("pt", "Portugal"),
    ("pr", "Puerto Rico"),
    ("qa", "Qatar"),
    ("re", "Réunion"),
    ("ro", "Romania"),
    ("ru", "Russia"),
    ("rw", "Rwanda"),
    ("as", "Samoa (American)"),
    ("ws", "Samoa (western)"),
    ("sm", "San Marino"),
    ("st", "Sao Tome & Principe"),
    ("sa", "Saudi Arabia"),
    ("sn", "Senegal"),
    ("rs", "Serbia"),
    ("sc", "Seychelles"),
    ("sl", "Sierra Leone"),
    ("sg", "Singapore"),
    ("sk", "Slovakia"),
    ("si", "Slovenia"),
    ("sb", "Solomon Islands"),
    ("so", "Somalia"),
    ("za", "South Africa"),
    ("gs", "South Georgia & the South Sandwich Islands"),
    ("ss", "South Sudan"),
    ("es", "Spain"),
    ("lk", "Sri Lanka"),
    ("bl", "St Barthelemy"),
    ("sh", "St Helena"),
    ("kn", "St Kitts & Nevis"),
    ("lc", "St Lucia"),
    ("sx", "St Maarten (Dutch)"),
    ("mf", "St Martin (French)"),
    ("pm", "St Pierre & Miquelon"),
    ("vc", "St Vincent"),
    ("sd", "Sudan"),
    ("sr", "Suriname"),
    ("sj", "Svalbard & Jan Mayen"),
    ("se", "Sweden"),
    ("ch", "Switzerland"),
    ("sy", "Syria"),
    ("tw", "Taiwan"),
    ("tj", "Tajikistan"),
    ("tz", "Tanzania"),
    ("th", "Thailand"),
    ("tg", "Togo"),
    ("tk", "Tokelau"),
    ("to", "Tonga"),
    ("tt", "Trinidad & Tobago"),
    ("tn", "Tunisia"),
    ("tr", "Turkey"),
    ("tm", "Turkmenistan"),
    ("tc", "Turks & Caicos Is"),
    ("tv", "Tuvalu"),
    ("ug", "Uganda"),
    ("ua", "Ukraine"),
    ("ae", "United Arab Emirates"),
    ("us", "United States"),
    ("uy", "Uruguay"),
    ("um", "US minor outlying islands"),
    ("uz", "Uzbekistan"),
    ("vu", "Vanuatu"),
    ("va", "Vatican City"),
    ("ve", "Venezuela"),
    ("vn", "Vietnam"),
    ("vg", "Virgin Islands (UK)"),
    ("vi", "Virgin Islands (US)"),
    ("wf", "Wallis & Futuna"),
    ("eh", "Western Sahara"),
    ("ye", "Yemen"),
    ("zm", "Zambia"),
    ("zw", "Zimbabwe"),
];

/// The name of the place `code` is among `places`, if it is one.
#[must_use]
pub fn place_name(places: &[Place], code: &str) -> Option<&'static str> {
    places
        .iter()
        .find(|(known, _)| *known == code)
        .map(|(_, name)| *name)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use super::*;

    #[test]
    fn codes_are_unique_lowercase_pairs_of_letters() {
        for places in [US_STATES, COUNTRIES] {
            let codes: BTreeSet<&str> = places.iter().map(|(code, _)| *code).collect();
            assert_eq!(codes.len(), places.len());
            for code in codes {
                assert!(code.len() == 2 && code.bytes().all(|byte| byte.is_ascii_lowercase()));
            }
        }
        assert_eq!(US_STATES.len(), 51);
        assert_eq!(place_name(COUNTRIES, US), Some("United States"));
        assert_eq!(
            place_name(US_STATES, "pr"),
            None,
            "a territory is a country"
        );
        assert_eq!(place_name(COUNTRIES, "pr"), Some("Puerto Rico"));
    }
}