const APPAREL_AND_CLOTHING_ACCESSORIES_PREFIXES: &[&str] = &["4203", "61", "62", "6504", "6505"];
const FOOTWEAR_PREFIXES: &[&str] = &["6401", "6402", "6403", "6404", "6405"];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnnexViiHeading {
ApparelAndClothingAccessories,
Footwear,
}
#[must_use]
pub fn annex_vii_heading(commodity_code: &str) -> Option<AnnexViiHeading> {
if APPAREL_AND_CLOTHING_ACCESSORIES_PREFIXES
.iter()
.any(|prefix| commodity_code.starts_with(prefix))
{
Some(AnnexViiHeading::ApparelAndClothingAccessories)
} else if FOOTWEAR_PREFIXES
.iter()
.any(|prefix| commodity_code.starts_with(prefix))
{
Some(AnnexViiHeading::Footwear)
} else {
None
}
}
#[must_use]
pub fn is_within_annex_vii_scope(commodity_code: &str) -> bool {
annex_vii_heading(commodity_code).is_some()
}
#[must_use]
pub fn product_category_matches_heading(product_category: &str, heading: AnnexViiHeading) -> bool {
matches!(
(product_category, heading),
(
"apparel" | "accessories",
AnnexViiHeading::ApparelAndClothingAccessories
) | ("footwear", AnnexViiHeading::Footwear)
)
}
#[cfg(test)]
mod tests {
use super::{
AnnexViiHeading, annex_vii_heading, is_within_annex_vii_scope,
product_category_matches_heading,
};
#[test]
fn apparel_heading_prefixes_are_in_scope() {
assert!(is_within_annex_vii_scope("420321")); assert!(is_within_annex_vii_scope("610910")); assert!(is_within_annex_vii_scope("620342")); assert!(is_within_annex_vii_scope("650400")); assert!(is_within_annex_vii_scope("650500")); }
#[test]
fn footwear_heading_prefixes_are_in_scope() {
assert!(is_within_annex_vii_scope("64011000"));
assert!(is_within_annex_vii_scope("64029900"));
assert!(is_within_annex_vii_scope("64031900"));
assert!(is_within_annex_vii_scope("64041100"));
assert!(is_within_annex_vii_scope("64051000"));
}
#[test]
fn footwear_parts_are_out_of_scope() {
assert!(!is_within_annex_vii_scope("64062000"));
}
#[test]
fn unrelated_chapter_is_out_of_scope() {
assert!(!is_within_annex_vii_scope("851712")); }
#[test]
fn annex_vii_heading_reports_which_heading_matched() {
assert_eq!(
annex_vii_heading("620342"),
Some(AnnexViiHeading::ApparelAndClothingAccessories)
);
assert_eq!(
annex_vii_heading("64011000"),
Some(AnnexViiHeading::Footwear)
);
assert_eq!(annex_vii_heading("851712"), None);
}
#[test]
fn apparel_and_accessories_both_match_the_one_apparel_heading() {
assert!(product_category_matches_heading(
"apparel",
AnnexViiHeading::ApparelAndClothingAccessories
));
assert!(product_category_matches_heading(
"accessories",
AnnexViiHeading::ApparelAndClothingAccessories
));
}
#[test]
fn footwear_matches_only_the_footwear_heading() {
assert!(product_category_matches_heading(
"footwear",
AnnexViiHeading::Footwear
));
assert!(!product_category_matches_heading(
"footwear",
AnnexViiHeading::ApparelAndClothingAccessories
));
}
#[test]
fn crossed_categories_do_not_match() {
assert!(!product_category_matches_heading(
"apparel",
AnnexViiHeading::Footwear
));
assert!(!product_category_matches_heading(
"accessories",
AnnexViiHeading::Footwear
));
}
#[test]
fn home_textile_and_other_match_no_heading() {
for category in ["home-textile", "other"] {
assert!(!product_category_matches_heading(
category,
AnnexViiHeading::ApparelAndClothingAccessories
));
assert!(!product_category_matches_heading(
category,
AnnexViiHeading::Footwear
));
}
}
}