use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CustomsContentType {
Merchandise,
Gift,
Documents,
Sample,
ReturnedGoods,
HumanitarianDonation,
Other,
}
impl std::fmt::Display for CustomsContentType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Merchandise => write!(f, "MERCHANDISE"),
Self::Gift => write!(f, "GIFT"),
Self::Documents => write!(f, "DOCUMENTS"),
Self::Sample => write!(f, "SAMPLE"),
Self::ReturnedGoods => write!(f, "RETURNED_GOODS"),
Self::HumanitarianDonation => write!(f, "HUMANITARIAN_DONATION"),
Self::Other => write!(f, "OTHER"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum NonDeliveryOption {
Return,
Abandon,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomsItem {
pub description: String,
pub quantity: u32,
pub value: f64,
pub weight_lbs: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub hs_tariff_number: Option<String>,
pub country_of_origin: String,
}
impl CustomsItem {
#[must_use]
pub fn new(
description: impl Into<String>,
quantity: u32,
value: f64,
weight_lbs: f64,
country_of_origin: impl Into<String>,
) -> Self {
Self {
description: description.into(),
quantity,
value,
weight_lbs,
hs_tariff_number: None,
country_of_origin: country_of_origin.into(),
}
}
#[must_use]
pub fn hs_tariff_number(mut self, hs: impl Into<String>) -> Self {
self.hs_tariff_number = Some(hs.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomsDeclaration {
pub content_type: CustomsContentType,
pub non_delivery_option: NonDeliveryOption,
#[serde(skip_serializing_if = "Option::is_none")]
pub aes_itn: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub importer_tax_id: Option<String>,
pub items: Vec<CustomsItem>,
}
impl CustomsDeclaration {
#[must_use]
pub fn new(
content_type: CustomsContentType,
non_delivery_option: NonDeliveryOption,
items: Vec<CustomsItem>,
) -> Self {
Self {
content_type,
non_delivery_option,
aes_itn: None,
importer_tax_id: None,
items,
}
}
#[must_use]
pub fn aes_itn(mut self, itn: impl Into<String>) -> Self {
self.aes_itn = Some(itn.into());
self
}
#[must_use]
pub fn importer_tax_id(mut self, tax_id: impl Into<String>) -> Self {
self.importer_tax_id = Some(tax_id.into());
self
}
#[must_use]
pub fn total_declared_value(&self) -> f64 {
self.items
.iter()
.map(|item| f64::from(item.quantity) * item.value)
.sum()
}
#[must_use]
pub fn total_weight_lbs(&self) -> f64 {
self.items.iter().map(|item| item.weight_lbs).sum()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn customs_declaration_builder_and_calculations() {
let item1 = CustomsItem::new("Men's Leather Shoes", 1, 120.0, 2.5, "US")
.hs_tariff_number("6403.59");
let item2 =
CustomsItem::new("Cotton Socks", 3, 10.0, 0.6, "US").hs_tariff_number("6115.95");
let decl = CustomsDeclaration::new(
CustomsContentType::Merchandise,
NonDeliveryOption::Return,
vec![item1, item2],
)
.aes_itn("NOEEI 30.37(a)")
.importer_tax_id("EU123456789");
assert_eq!(decl.content_type, CustomsContentType::Merchandise);
assert_eq!(decl.non_delivery_option, NonDeliveryOption::Return);
assert_eq!(decl.aes_itn.as_deref(), Some("NOEEI 30.37(a)"));
assert_eq!(decl.items.len(), 2);
assert_eq!(decl.total_declared_value(), 150.0);
assert!((decl.total_weight_lbs() - 3.1).abs() < 1e-6);
}
#[test]
fn customs_content_type_display() {
assert_eq!(CustomsContentType::Merchandise.to_string(), "MERCHANDISE");
assert_eq!(CustomsContentType::Gift.to_string(), "GIFT");
assert_eq!(CustomsContentType::Documents.to_string(), "DOCUMENTS");
assert_eq!(CustomsContentType::Sample.to_string(), "SAMPLE");
assert_eq!(
CustomsContentType::ReturnedGoods.to_string(),
"RETURNED_GOODS"
);
assert_eq!(
CustomsContentType::HumanitarianDonation.to_string(),
"HUMANITARIAN_DONATION"
);
assert_eq!(CustomsContentType::Other.to_string(), "OTHER");
}
}