Skip to main content

ibridge_core/
contacts.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Contacts values and the contacts provider trait.
4
5use crate::error::CoreError;
6use crate::ids::{ETag, ResourcePath};
7
8/// A contact search.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct ContactQuery {
11    /// Name, email or phone text. Matching is done by the provider.
12    pub text: String,
13    /// Maximum hits. Callers cap this at 100.
14    pub limit: u32,
15    /// Also return groups, so a caller can find one to put a contact in.
16    pub include_groups: bool,
17}
18
19/// A group a contact is in.
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct GroupRef {
22    /// Path of the group card, for `set_group_membership`.
23    pub path: ResourcePath,
24    /// Group name.
25    pub name: String,
26}
27
28/// An email address or phone number with the label the card gives it.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct LabeledValue {
31    /// The address or number.
32    pub value: String,
33    /// `Home`, `Work`, `Cell` or a custom label.
34    pub label: Option<String>,
35}
36
37/// Contacts from a search, with a count of cards that were skipped.
38#[derive(Clone, Debug, PartialEq, Eq)]
39pub struct ContactList {
40    /// Matching contacts in name order.
41    pub contacts: Vec<Contact>,
42    /// Cards that could not be read and were left out.
43    pub warnings: u32,
44}
45
46/// One contact.
47#[derive(Clone, Debug, PartialEq, Eq)]
48pub struct Contact {
49    /// Path relative to the address book home.
50    pub path: ResourcePath,
51    /// `ETag` for a conditional write.
52    pub etag: ETag,
53    /// Full name.
54    pub full_name: String,
55    /// Email addresses.
56    pub emails: Vec<LabeledValue>,
57    /// Phone numbers.
58    pub phones: Vec<LabeledValue>,
59    /// Company.
60    pub organization: Option<String>,
61    /// Free-text note.
62    pub note: Option<String>,
63    /// The card is a group, not a person.
64    pub is_group: bool,
65    /// The groups the contact is in. Filled by `get` only.
66    pub groups: Vec<GroupRef>,
67}
68
69/// Fields written by create and update.
70#[derive(Clone, Debug, PartialEq, Eq)]
71pub struct ContactDraft {
72    /// Full name.
73    pub full_name: String,
74    /// Email addresses.
75    pub emails: Vec<String>,
76    /// Phone numbers.
77    pub phones: Vec<String>,
78}
79
80/// Changes to a contact. A field left as `None` stays as the card has it.
81#[derive(Clone, Debug, Default, PartialEq, Eq)]
82pub struct ContactUpdate {
83    /// New full name.
84    pub full_name: Option<String>,
85    /// New email addresses, replacing the current ones.
86    pub emails: Option<Vec<String>>,
87    /// New phone numbers, replacing the current ones.
88    pub phones: Option<Vec<String>>,
89}
90
91/// Add or remove a contact in an existing group.
92#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
93#[non_exhaustive]
94pub enum GroupChange {
95    /// Put the contact in the group.
96    Add,
97    /// Take the contact out of the group.
98    Remove,
99}
100
101/// Contacts read and write operations.
102#[trait_variant::make(Send)]
103pub trait ContactsProvider {
104    /// Searches contacts.
105    ///
106    /// # Errors
107    ///
108    /// Returns [`CoreError`] when the search cannot be run.
109    async fn search(&self, query: &ContactQuery) -> Result<ContactList, CoreError>;
110
111    /// Fetches one contact.
112    ///
113    /// # Errors
114    ///
115    /// Returns [`CoreError`] when the contact cannot be read.
116    async fn get(&self, path: &ResourcePath) -> Result<Contact, CoreError>;
117
118    /// Creates a contact.
119    ///
120    /// # Errors
121    ///
122    /// Returns [`CoreError`] when the contact cannot be written.
123    async fn create(&self, draft: &ContactDraft) -> Result<ResourcePath, CoreError>;
124
125    /// Replaces a contact when the `ETag` still matches.
126    ///
127    /// # Errors
128    ///
129    /// Returns [`CoreError::Conflict`] when the `ETag` does not match.
130    async fn update(
131        &self,
132        path: &ResourcePath,
133        etag: &ETag,
134        update: &ContactUpdate,
135    ) -> Result<ETag, CoreError>;
136
137    /// Deletes a contact.
138    ///
139    /// # Errors
140    ///
141    /// Returns [`CoreError`] when the contact cannot be deleted.
142    async fn delete(&self, path: &ResourcePath, etag: &ETag) -> Result<(), CoreError>;
143
144    /// Adds or removes a contact in an existing group.
145    ///
146    /// # Errors
147    ///
148    /// Returns [`CoreError`] when the group card cannot be rewritten.
149    async fn set_group_membership(
150        &self,
151        group: &ResourcePath,
152        contact: &ResourcePath,
153        change: GroupChange,
154    ) -> Result<(), CoreError>;
155}