use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::geo_data::{DIVISIONS, DISTRICTS, UPAZILAS, UNIONS};
#[derive(Debug, Error)]
pub enum AddressError {
#[error("Division not found: {0}")]
DivisionNotFound(String),
#[error("District not found: {0}")]
DistrictNotFound(String),
#[error("Upazila not found: {0}")]
UpazilaNotFound(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BanglaAddress {
pub division: AddressComponent,
pub district: AddressComponent,
#[serde(skip_serializing_if = "Option::is_none")]
pub upazila: Option<AddressComponent>,
#[serde(skip_serializing_if = "Option::is_none")]
pub union: Option<AddressComponent>,
#[serde(skip_serializing_if = "Option::is_none")]
pub area: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub house: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub postal_code: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AddressComponent {
#[serde(rename = "geoCode")]
pub geo_code: String,
pub name: String,
}
impl BanglaAddress {
pub fn new(
division_code: &str,
district_code: &str,
) -> Self {
let division = DIVISIONS
.iter()
.find(|(code, _)| *code == division_code)
.map(|(code, name)| AddressComponent {
geo_code: code.to_string(),
name: name.to_string(),
})
.unwrap_or_else(|| AddressComponent {
geo_code: division_code.to_string(),
name: division_code.to_string(),
});
let district = DISTRICTS
.iter()
.find(|(div, dc, _)| *div == division_code && *dc == district_code)
.map(|(_, dc, bn)| AddressComponent {
geo_code: dc.to_string(),
name: bn.to_string(),
})
.unwrap_or_else(|| AddressComponent {
geo_code: district_code.to_string(),
name: district_code.to_string(),
});
Self {
division,
district,
upazila: None,
union: None,
area: None,
house: None,
postal_code: None,
}
}
pub fn with_upazila(mut self, query: impl Into<String>) -> Self {
let q = query.into();
let district_code = &self.district.geo_code;
let found = UPAZILAS.iter()
.find(|(dist, code, name)| dist == district_code && (*code == q || *name == q));
if let Some((_, code, name)) = found {
self.upazila = Some(AddressComponent {
geo_code: code.to_string(),
name: name.to_string(),
});
} else {
self.upazila = Some(AddressComponent {
geo_code: q.clone(),
name: q,
});
}
self
}
pub fn with_union(mut self, query: impl Into<String>) -> Self {
let q = query.into();
if let Some(upz) = &self.upazila {
let upz_code = &upz.geo_code;
let found = UNIONS.iter()
.find(|(u_code, code, name)| u_code == upz_code && (*code == q || *name == q));
if let Some((_, code, name)) = found {
self.union = Some(AddressComponent {
geo_code: code.to_string(),
name: name.to_string(),
});
return self;
}
}
self.union = Some(AddressComponent {
geo_code: q.clone(),
name: q,
});
self
}
pub fn with_area(mut self, area: impl Into<String>) -> Self {
self.area = Some(area.into());
self
}
pub fn with_house(mut self, house: impl Into<String>) -> Self {
self.house = Some(house.into());
self
}
pub fn with_postal_code(mut self, code: impl Into<String>) -> Self {
self.postal_code = Some(code.into());
self
}
pub fn to_json(&self) -> String {
serde_json::to_string_pretty(self).unwrap_or_default()
}
pub fn format(&self) -> String {
let mut parts = Vec::new();
if let Some(ref h) = self.house { parts.push(h.clone()); }
if let Some(ref a) = self.area { parts.push(a.clone()); }
if let Some(ref u) = self.union { parts.push(u.name.clone()); }
if let Some(ref upz) = self.upazila { parts.push(upz.name.clone()); }
parts.push(self.district.name.clone());
parts.push(self.division.name.clone());
if let Some(ref p) = self.postal_code { parts.push(p.clone()); }
parts.push("বাংলাদেশ".to_string());
parts.join(", ")
}
}
pub fn find_division(query: &str) -> Option<AddressComponent> {
DIVISIONS.iter().find(|(code, name)| *code == query || *name == query)
.map(|(code, name)| AddressComponent {
geo_code: code.to_string(),
name: name.to_string(),
})
}
pub fn districts_in_division(division_code: &str) -> Vec<AddressComponent> {
DISTRICTS
.iter()
.filter(|(div, _, _)| *div == division_code)
.map(|(_, dc, bn)| AddressComponent {
geo_code: dc.to_string(),
name: bn.to_string(),
})
.collect()
}
pub fn find_district(query: &str) -> Option<AddressComponent> {
DISTRICTS.iter().find(|(_, dc, bn)| *dc == query || *bn == query)
.map(|(_, dc, bn)| AddressComponent {
geo_code: dc.to_string(),
name: bn.to_string(),
})
}
pub fn upazilas_in_district(district_code: &str) -> Vec<AddressComponent> {
UPAZILAS
.iter()
.filter(|(dist, _, _)| *dist == district_code)
.map(|(_, code, name)| AddressComponent {
geo_code: code.to_string(),
name: name.to_string(),
})
.collect()
}
pub fn unions_in_upazila(upazila_code: &str) -> Vec<AddressComponent> {
UNIONS
.iter()
.filter(|(upz, _, _)| *upz == upazila_code)
.map(|(_, code, name)| AddressComponent {
geo_code: code.to_string(),
name: name.to_string(),
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_address_builder() {
let addr = BanglaAddress::new("3", "10")
.with_upazila("মিরপুর")
.with_union("পল্লবী")
.with_area("সেকশন-১১")
.with_postal_code("১২১৬");
assert_eq!(addr.division.name, "ঢাকা");
assert_eq!(addr.district.name, "ঢাকা");
assert_eq!(addr.upazila.as_ref().unwrap().name, "মিরপুর");
assert_eq!(addr.postal_code.as_deref(), Some("১২১৬"));
}
#[test]
fn test_format() {
let addr = BanglaAddress::new("3", "10")
.with_upazila("মিরপুর")
.with_union("পল্লবী");
let s = addr.format();
assert!(s.contains("ঢাকা"));
assert!(s.contains("বাংলাদেশ"));
}
#[test]
fn test_json_output() {
let addr = BanglaAddress::new("3", "10")
.with_upazila("মিরপুর")
.with_postal_code("1216");
let json = addr.to_json();
assert!(json.contains("\"geoCode\""));
assert!(json.contains("1216"));
}
#[test]
fn test_find_division() {
let d = find_division("ঢাকা").unwrap();
assert_eq!(d.geo_code, "3");
let d2 = find_division("3").unwrap();
assert_eq!(d2.geo_code, "3");
}
#[test]
fn test_districts_in_division() {
let dists = districts_in_division("3");
assert!(dists.iter().any(|d| d.name == "ঢাকা"));
assert!(dists.iter().any(|d| d.name == "গাজীপুর"));
}
#[test]
fn test_find_district() {
let d = find_district("নরসিংদী").unwrap();
assert_eq!(d.geo_code, "08");
}
}