use crate::{CountryCode, PhoneNumber};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Customer {
email: Option<String>,
phone: Option<PhoneNumber>,
country: Option<CountryCode>,
name: Option<String>,
}
impl Customer {
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
#[must_use]
pub fn with_phone(mut self, phone: PhoneNumber) -> Self {
self.phone = Some(phone);
self
}
#[must_use]
pub fn with_country(mut self, country: CountryCode) -> Self {
self.country = Some(country);
self
}
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
#[inline]
#[must_use]
pub fn email(&self) -> Option<&str> {
self.email.as_deref()
}
#[inline]
#[must_use]
pub const fn phone(&self) -> Option<&PhoneNumber> {
self.phone.as_ref()
}
#[inline]
#[must_use]
pub const fn country(&self) -> Option<&CountryCode> {
self.country.as_ref()
}
#[inline]
#[must_use]
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_methods_set_customer_fields() {
let phone = PhoneNumber::new("260971234567").expect("phone should be valid");
let country = CountryCode::new("ZM").expect("country should be valid");
let customer = Customer::new()
.with_email("customer@example.com")
.with_phone(phone)
.with_country(country)
.with_name("Ada Buyer");
assert_eq!(customer.email(), Some("customer@example.com"));
assert_eq!(
customer.phone().expect("phone should be present").as_e164(),
"+260971234567"
);
assert_eq!(
customer
.country()
.expect("country should be present")
.as_str(),
"ZM"
);
assert_eq!(customer.name(), Some("Ada Buyer"));
}
}