rust_macios/contacts/
cn_contact_formatter.rs

1use objc::{msg_send, sel, sel_impl};
2use rust_macios_objective_c_runtime_proc_macros::interface_impl;
3
4use crate::{
5    foundation::{INSFormatter, NSAttributedString, NSDictionary, NSString},
6    object,
7    objective_c_runtime::id,
8    utils::to_optional,
9};
10
11use super::CNContact;
12
13#[repr(i64)]
14pub enum CNContactFormatterStyle {
15    /// Combine the contact name components into a displayable full name.
16    FullName,
17    /// Combine the contact phonetic name components into a displayable phonetic full name.
18    PhoneticFullName,
19}
20
21#[repr(i64)]
22pub enum CNContactDisplayNameOrder {
23    CNContactDisplayNameOrderUserDefault,
24    CNContactDisplayNameOrderGivenNameFirst,
25    CNContactDisplayNameOrderFamilyNameFirst,
26}
27
28object! {
29    /// An object that you use to format contact information before displaying it to the user.
30    unsafe pub struct CNContactFormatter;
31}
32
33impl INSFormatter for CNContactFormatter {}
34
35#[interface_impl(NSFormatter)]
36impl CNContactFormatter {
37    /* Creating a Formatted Attributed String
38     */
39
40    /// Formats the contact name as an attributed string.
41    #[method]
42    pub fn attributed_string_from_contact_default_attributes<K, V>(
43        &self,
44        contact: CNContact,
45        attributes: NSDictionary<K, V>,
46    ) -> Option<NSAttributedString> {
47        unsafe {
48            to_optional(
49                msg_send![self.m_self(), attributedStringFromContact: contact defaultAttributes: attributes],
50            )
51        }
52    }
53
54    /// Formats the contact name as an attributed string.
55    #[method]
56    pub fn attributed_string_from_contact_style_default_attributes<K, V>(
57        contact: CNContact,
58        style: CNContactFormatterStyle,
59        attributes: NSDictionary<K, V>,
60    ) -> Option<NSAttributedString> {
61        unsafe {
62            to_optional(
63                msg_send![Self::m_class(), attributedStringFromContact: contact style: style defaultAttributes: attributes],
64            )
65        }
66    }
67
68    /// Formats the contact name.
69    #[method]
70    pub fn string_from_contact(&self, contact: CNContact) -> Option<NSString> {
71        unsafe { to_optional(msg_send![self.m_self(), stringFromContact: contact]) }
72    }
73
74    /// Returns the contact name, formatted with the specified formatter.
75    #[method]
76    pub fn string_from_contact_style(
77        contact: CNContact,
78        style: CNContactFormatterStyle,
79    ) -> Option<NSString> {
80        unsafe { to_optional(msg_send![Self::m_class(), stringFromContact: contact style: style]) }
81    }
82
83    /* Specifying the Formatting Style
84     */
85
86    /// The formatting style for the contact name.
87    #[property]
88    pub fn style(&self) -> CNContactFormatterStyle {
89        unsafe { msg_send![self.m_self(), style] }
90    }
91
92    /// Sets the formatting style for the contact name.
93    #[property]
94    pub fn set_style(&mut self) -> CNContactFormatterStyle {
95        unsafe { msg_send![self.m_self(), style] }
96    }
97
98    /* Getting a Descriptor
99     */
100
101    /// Returns the required key descriptor for the specified formatting style of the contact.
102    #[method]
103    pub fn descriptor_for_required_keys_for_style(style: CNContactFormatterStyle) -> id {
104        unsafe { msg_send![Self::m_class(), descriptorForRequiredKeysForStyle: style] }
105    }
106
107    /* Getting Format Information
108     */
109
110    /// Returns the delimiter to use between name components.
111    #[method]
112    pub fn delimiter_for_contact(contact: CNContact) -> NSString {
113        unsafe { msg_send![Self::m_class(), delimiterForContact: contact] }
114    }
115
116    /// Returns the display name order.
117    #[method]
118    pub fn name_order_for_contact(contact: CNContact) -> CNContactDisplayNameOrder {
119        unsafe { msg_send![Self::m_class(), nameOrderForContact: contact] }
120    }
121
122    /* Type Properties
123     */
124
125    #[property]
126    pub fn descriptor_for_required_keys_for_delimiter() -> id {
127        unsafe { msg_send![Self::m_class(), descriptorForRequiredKeysForDelimiter] }
128    }
129
130    #[property]
131    pub fn descriptor_for_required_keys_for_name_order() -> id {
132        unsafe { msg_send![Self::m_class(), descriptorForRequiredKeysForNameOrder] }
133    }
134}