#![allow(dead_code)]
use serde_json::{Value, json};
use validator::ValidateEmail;
#[allow(clippy::too_many_arguments)]
pub fn create_minimal_contact(
first_name: Option<&str>,
last_name: Option<&str>,
address_name: Option<&str>,
phone: Option<&str>,
email: Option<&str>,
mail_name: Option<&str>,
mail_address: Option<&str>,
mail_address_two: Option<&str>,
mail_state: Option<&str>,
mail_zip: Option<&str>,
mail_country: Option<&str>,
is_primary: Option<bool>,
) -> Result<Value, String> {
let mut contact = json!({});
if let Some(first_name) = first_name {
contact["firstName"] = json!(first_name);
}
if let Some(last_name) = last_name {
contact["lastName"] = json!(last_name);
}
if let Some(address_name) = address_name {
contact["addressName"] = json!(address_name);
}
if let Some(phone) = phone {
contact["phone"] = json!(phone);
}
if let Some(email) = email {
let email_valid: bool = ValidateEmail::validate_email(&email);
if !email_valid {
return Err(format!("Invalid email address: {}", email));
}
contact["email"] = json!(email);
}
if let Some(mail_name) = mail_name {
contact["mailName"] = json!(mail_name);
}
if let Some(mail_address) = mail_address {
contact["mailAddress"] = json!(mail_address);
}
if let Some(mail_address_two) = mail_address_two {
contact["mailAddressTwo"] = json!(mail_address_two);
}
if let Some(mail_state) = mail_state {
contact["mailState"] = json!(mail_state);
}
if let Some(mail_zip) = mail_zip {
contact["mailZip"] = json!(mail_zip);
}
if let Some(mail_country) = mail_country {
contact["mailCountry"] = json!(mail_country);
}
if let Some(is_primary) = is_primary {
contact["isPrimary"] = json!(is_primary);
}
Ok(contact)
}
fn update_contact_first_name(contact: &mut Value, new_first_name: &str) -> Result<(), String> {
contact["firstName"] = json!(new_first_name);
Ok(())
}
fn update_contact_last_name(contact: &mut Value, new_last_name: &str) -> Result<(), String> {
contact["lastName"] = json!(new_last_name);
Ok(())
}
fn update_contact_email(contact: &mut Value, new_email: &str) -> Result<(), String> {
let email_valid: bool = ValidateEmail::validate_email(&new_email);
if !email_valid {
return Err(format!("Invalid email address: {}", new_email));
}
contact["email"] = json!(new_email);
Ok(())
}
fn remove_contact_email(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("email");
Ok(())
}
fn update_contact_phone(contact: &mut Value, new_phone: &str) -> Result<(), String> {
contact["phone"] = json!(new_phone);
Ok(())
}
fn remove_contact_phone(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("phone");
Ok(())
}
fn update_contact_address_name(contact: &mut Value, new_address_name: &str) -> Result<(), String> {
contact["addressName"] = json!(new_address_name);
Ok(())
}
fn remove_contact_address_name(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("addressName");
Ok(())
}
fn update_contact_mail_address(contact: &mut Value, new_mail_address: &str) -> Result<(), String> {
contact["mailAddress"] = json!(new_mail_address);
Ok(())
}
fn remove_contact_mail_address(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("mailAddress");
Ok(())
}
fn update_contact_mail_address_two(
contact: &mut Value,
new_mail_address_two: &str,
) -> Result<(), String> {
contact["mailAddressTwo"] = json!(new_mail_address_two);
Ok(())
}
fn remove_contact_mail_address_two(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("mailAddressTwo");
Ok(())
}
fn update_contact_mail_state(contact: &mut Value, new_mail_state: &str) -> Result<(), String> {
contact["mailState"] = json!(new_mail_state);
Ok(())
}
fn remove_contact_mail_state(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("mailState");
Ok(())
}
fn update_contact_mail_zip(contact: &mut Value, new_mail_zip: &str) -> Result<(), String> {
contact["mailZip"] = json!(new_mail_zip);
Ok(())
}
fn remove_contact_mail_zip(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("mailZip");
Ok(())
}
fn update_contact_mail_country(contact: &mut Value, new_mail_country: &str) -> Result<(), String> {
contact["mailCountry"] = json!(new_mail_country);
Ok(())
}
fn remove_contact_mail_country(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("mailCountry");
Ok(())
}
fn update_contact_is_primary(contact: &mut Value, is_primary: bool) -> Result<(), String> {
contact["isPrimary"] = json!(is_primary);
Ok(())
}
fn remove_contact_is_primary(contact: &mut Value) -> Result<(), String> {
contact
.as_object_mut()
.ok_or_else(|| "Invalid contact format".to_string())?
.remove("isPrimary");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_minimal_contact_accepts_valid_email_and_sets_fields() {
let contact = create_minimal_contact(
Some("Ada"),
Some("Lovelace"),
None,
Some("+1-555-0100"),
Some("ada@example.com"),
None,
None,
None,
None,
None,
Some("UK"),
Some(true),
)
.expect("contact should be created");
assert_eq!(contact["firstName"], json!("Ada"));
assert_eq!(contact["lastName"], json!("Lovelace"));
assert_eq!(contact["email"], json!("ada@example.com"));
assert_eq!(contact["isPrimary"], json!(true));
assert_eq!(contact["mailCountry"], json!("UK"));
}
#[test]
fn contact_helpers_update_and_remove_fields() {
let mut contact = create_minimal_contact(
Some("Ada"),
None,
None,
Some("+1-555-0100"),
Some("ada@example.com"),
None,
None,
None,
None,
None,
None,
None,
)
.expect("contact should be created");
update_contact_first_name(&mut contact, "Grace").unwrap();
update_contact_email(&mut contact, "grace@example.com").unwrap();
update_contact_is_primary(&mut contact, true).unwrap();
remove_contact_phone(&mut contact).unwrap();
remove_contact_email(&mut contact).unwrap();
remove_contact_is_primary(&mut contact).unwrap();
assert_eq!(contact["firstName"], json!("Grace"));
assert!(contact.get("phone").is_none());
assert!(contact.get("email").is_none());
assert!(contact.get("isPrimary").is_none());
}
#[test]
fn create_minimal_contact_rejects_invalid_email() {
let error = create_minimal_contact(
Some("Ada"),
None,
None,
None,
Some("not-an-email"),
None,
None,
None,
None,
None,
None,
None,
)
.expect_err("invalid email should be rejected");
assert!(error.contains("Invalid email address"));
}
}