rust_macios/contacts/
cn_labeled_value.rs

1use std::marker::{PhantomData, Sized};
2
3use objc::{msg_send, sel, sel_impl};
4
5use crate::{
6    foundation::NSString,
7    object,
8    objective_c_runtime::traits::{FromId, PNSObject},
9};
10
11object! {
12    /// An immutable object that combines a contact property value with a label that describes that property.
13    unsafe pub struct CNLabeledValue<ValueType> {
14        marker: PhantomData<ValueType>,
15    }
16}
17
18/// A trait containing all the methods for [`ICNLabeledValue`]"
19pub trait ICNLabeledValue<ValueType>: PNSObject
20where
21    ValueType: PNSObject + FromId,
22{
23    /// Returns a new labeled value identifier.
24    fn m_init_with_label_value(&mut self, label: &NSString, value: ValueType) -> Self
25    where
26        Self: Sized + FromId,
27    {
28        unsafe {
29            Self::from_id(msg_send![
30                self.m_self(),
31                initWithLabel:label.m_self()
32                value:value
33            ])
34        }
35    }
36
37    /// Returns a new labeled value identifier.
38    fn m_labeled_value_with_label_value(label: &NSString, value: ValueType) -> Self
39    where
40        Self: Sized + FromId,
41    {
42        unsafe {
43            Self::from_id(
44                msg_send![Self::m_class(), labeledValueWithLabel:label.m_self() value:value],
45            )
46        }
47    }
48
49    /* Getting the Label and Value
50     */
51
52    /// The label for a contact property value.
53    fn p_label(&self) -> NSString {
54        unsafe { NSString::from_id(msg_send![self.m_self(), label]) }
55    }
56
57    /// A contact property value.
58    fn p_value(&self) -> ValueType {
59        unsafe { ValueType::from_id(msg_send![self.m_self(), value]) }
60    }
61
62    /* Setting Labels and Values
63     */
64
65    /// Returns a labeled value object with an existing value and identifier.
66    fn m_labeled_value_by_setting_label(&self, label: &NSString) -> Self
67    where
68        Self: Sized + FromId,
69    {
70        unsafe {
71            Self::from_id(msg_send![self.m_self(), labeledValueBySettingLabel: label.m_self()])
72        }
73    }
74
75    /// Returns a labeled value object with the specified label and value with the existing identifier.
76    fn m_labeled_value_by_setting_label_value(&self, label: &NSString, value: ValueType) -> Self
77    where
78        Self: Sized + FromId,
79    {
80        unsafe {
81            Self::from_id(msg_send![
82                self.m_self(),
83                labeledValueBySettingLabel:label.m_self()
84                value:value
85            ])
86        }
87    }
88
89    /// Returns a new value for an existing label and identifier.
90    fn m_labeled_value_by_setting_value(&self, value: ValueType) -> Self
91    where
92        Self: Sized + FromId,
93    {
94        unsafe { Self::from_id(msg_send![self.m_self(), valueBySettingValue: value]) }
95    }
96
97    /* Localizing the Label and Value
98     */
99
100    /// Returns a localized string for the specified label.
101    fn m_localized_string_for_label(label: &NSString) -> NSString {
102        unsafe {
103            NSString::from_id(msg_send![Self::m_class(), localizedStringForLabel: label.m_self()])
104        }
105    }
106
107    /// A unique identifier for the labeled value object.
108    fn p_identifier(&self) -> NSString {
109        unsafe { NSString::from_id(msg_send![self.m_self(), identifier]) }
110    }
111}
112
113impl<ValueType> ICNLabeledValue<ValueType> for CNLabeledValue<ValueType> where
114    ValueType: PNSObject + FromId
115{
116}
117
118impl<ValueType> CNLabeledValue<ValueType>
119where
120    ValueType: PNSObject + FromId,
121{
122    /// Returns a new labeled value identifier.
123    pub fn init_with_label_value(&mut self, label: &NSString, value: ValueType) -> Self {
124        self.m_init_with_label_value(label, value)
125    }
126
127    /// Returns a new labeled value identifier.
128    pub fn labeled_value_with_label_value(label: &NSString, value: ValueType) -> Self {
129        Self::m_labeled_value_with_label_value(label, value)
130    }
131
132    /* Getting the Label and Value
133     */
134
135    /// The label for a contact property value.
136    pub fn label(&self) -> NSString {
137        self.p_label()
138    }
139
140    /// A contact property value.
141    pub fn value(&self) -> ValueType {
142        self.p_value()
143    }
144
145    /* Setting Labels and Values
146     */
147
148    /// Returns a labeled value object with an existing value and identifier.
149    pub fn labeled_value_by_setting_label(&self, label: &NSString) -> Self {
150        self.m_labeled_value_by_setting_label(label)
151    }
152
153    /// Returns a labeled value object with the specified label and value with the existing identifier.
154    pub fn labeled_value_by_setting_label_value(&self, label: &NSString, value: ValueType) -> Self {
155        self.m_labeled_value_by_setting_label_value(label, value)
156    }
157
158    /// Returns a new value for an existing label and identifier.
159    pub fn labeled_value_by_setting_value(&self, value: ValueType) -> Self {
160        self.m_labeled_value_by_setting_value(value)
161    }
162
163    /* Localizing the Label and Value
164     */
165
166    /// Returns a localized string for the specified label.
167    pub fn localized_string_for_label(label: &NSString) -> NSString {
168        Self::m_localized_string_for_label(label)
169    }
170
171    /// A unique identifier for the labeled value object.
172    pub fn identifier(&self) -> NSString {
173        self.p_identifier()
174    }
175}
176
177extern "C" {
178    /* Getting Common Labels
179     */
180
181    /// The label for identifying home information.
182    pub static CNLabelHome: NSString;
183
184    /// The label for identifying work information.
185    pub static CNLabelWork: NSString;
186
187    /// The label for the contact’s school.
188    pub static CNLabelSchool: NSString;
189
190    /// The label for identifying other information.
191    pub static CNLabelOther: NSString;
192
193    /// The label for identifying the contact's iCloud email information.
194    pub static CNLabelEmailiCloud: NSString;
195
196    /// The label for identifying URL information.
197    pub static CNLabelURLAddressHomePage: NSString;
198
199    /// The label for identifying the contact's anniversary date.
200    pub static CNLabelDateAnniversary: NSString;
201}