jacs 0.9.5

JACS JSON AI Communication Standard
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// CRUD operations for future public API - will be exposed in upcoming releases
#![allow(dead_code)]

use serde_json::{Value, json};
use validator::ValidateEmail;

/// Creates a minimal contact with optional fields.
///
/// # Arguments
///
/// * `first_name` - The first name of the person.
/// * `last_name` - The last name of the person.
/// * `address_name` - The location name of the address.
/// * `phone` - The contact phone number.
/// * `email` - The contact email address.
/// * `mail_name` - The name to reach at the address.
/// * `mail_address` - The street and street address.
/// * `mail_address_two` - The second part of the mailing address.
/// * `mail_state` - The state or province.
/// * `mail_zip` - The zipcode.
/// * `mail_country` - The country.
/// * `is_primary` - Indicates if this is the primary way to contact the agent.
///
/// # Returns
///
/// A `serde_json::Value` representing the created contact.
///
/// # Errors
///
/// Returns an error if:
/// - `email` is provided but is not a valid email address.
#[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)
}

/// Updates the first name of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_first_name` - The new first name for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact first name was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact first name.
fn update_contact_first_name(contact: &mut Value, new_first_name: &str) -> Result<(), String> {
    contact["firstName"] = json!(new_first_name);
    Ok(())
}

/// Updates the last name of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_last_name` - The new last name for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact last name was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact last name.
fn update_contact_last_name(contact: &mut Value, new_last_name: &str) -> Result<(), String> {
    contact["lastName"] = json!(new_last_name);
    Ok(())
}

/// Updates the email of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_email` - The new email for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact email was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact email.
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(())
}

/// Removes the email from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact email was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact email.
fn remove_contact_email(contact: &mut Value) -> Result<(), String> {
    contact
        .as_object_mut()
        .ok_or_else(|| "Invalid contact format".to_string())?
        .remove("email");
    Ok(())
}

/// Updates the phone of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_phone` - The new phone for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact phone was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact phone.
fn update_contact_phone(contact: &mut Value, new_phone: &str) -> Result<(), String> {
    contact["phone"] = json!(new_phone);
    Ok(())
}

/// Removes the phone from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact phone was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact phone.
fn remove_contact_phone(contact: &mut Value) -> Result<(), String> {
    contact
        .as_object_mut()
        .ok_or_else(|| "Invalid contact format".to_string())?
        .remove("phone");
    Ok(())
}

/// Updates the address name of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_address_name` - The new address name for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact address name was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact address name.
fn update_contact_address_name(contact: &mut Value, new_address_name: &str) -> Result<(), String> {
    contact["addressName"] = json!(new_address_name);
    Ok(())
}

/// Removes the address name from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact address name was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact address name.
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(())
}

/// Updates the mail address of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_mail_address` - The new mail address for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail address was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact mail address.
fn update_contact_mail_address(contact: &mut Value, new_mail_address: &str) -> Result<(), String> {
    contact["mailAddress"] = json!(new_mail_address);
    Ok(())
}

/// Removes the mail address from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail address was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact mail address.
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(())
}

/// Updates the mail address two of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_mail_address_two` - The new mail address two for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail address two was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact mail address two.
fn update_contact_mail_address_two(
    contact: &mut Value,
    new_mail_address_two: &str,
) -> Result<(), String> {
    contact["mailAddressTwo"] = json!(new_mail_address_two);
    Ok(())
}

/// Removes the mail address two from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail address two was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact mail address two.
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(())
}

/// Updates the mail state of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_mail_state` - The new mail state for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail state was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact mail state.
fn update_contact_mail_state(contact: &mut Value, new_mail_state: &str) -> Result<(), String> {
    contact["mailState"] = json!(new_mail_state);
    Ok(())
}

/// Removes the mail state from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail state was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact mail state.
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(())
}

/// Updates the mail zip of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_mail_zip` - The new mail zip for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail zip was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact mail zip.
fn update_contact_mail_zip(contact: &mut Value, new_mail_zip: &str) -> Result<(), String> {
    contact["mailZip"] = json!(new_mail_zip);
    Ok(())
}

/// Removes the mail zip from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail zip was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact mail zip.
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(())
}

/// Updates the mail country of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `new_mail_country` - The new mail country for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail country was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact mail country.
fn update_contact_mail_country(contact: &mut Value, new_mail_country: &str) -> Result<(), String> {
    contact["mailCountry"] = json!(new_mail_country);
    Ok(())
}

/// Removes the mail country from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact mail country was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact mail country.
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(())
}

/// Updates the primary status of a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
/// * `is_primary` - The new primary status for the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact primary status was updated successfully.
/// * `Err(String)` - If an error occurred while updating the contact primary status.
fn update_contact_is_primary(contact: &mut Value, is_primary: bool) -> Result<(), String> {
    contact["isPrimary"] = json!(is_primary);
    Ok(())
}

/// Removes the primary status from a contact.
///
/// # Arguments
///
/// * `contact` - A mutable reference to the contact.
///
/// # Returns
///
/// * `Ok(())` - If the contact primary status was removed successfully.
/// * `Err(String)` - If an error occurred while removing the contact primary status.
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"));
    }
}