rust_macios/foundation/ns_formatter.rs
1use objc::{msg_send, sel, sel_impl};
2
3use crate::{object,
4 objective_c_runtime::{
5 id,
6 macros::{interface_impl},
7 traits::{FromId, PNSObject},
8 },
9 utils::to_bool,
10};
11
12use super::{
13 NSAttributedString, NSAttributedStringKey, NSDictionary, NSRange, NSRangePointer, NSString,
14};
15
16object! {
17 /// An abstract class that declares an interface for objects that create,
18 /// interpret, and validate the textual representation of values.
19 unsafe pub struct NSFormatter;
20}
21
22#[interface_impl(NSObject)]
23impl NSFormatter {
24 /* Getting Textual Representations of Object Values
25 */
26
27 /// The default implementation of this method raises an exception.
28 ///
29 /// # Arguments
30 ///
31 /// * `obj` - The object for which a textual representation is returned.
32 #[method]
33 pub fn string_for_object_value(&self, obj: id) -> NSString {
34 unsafe { NSString::from_id(msg_send![self.m_self(), stringForObjectValue: obj]) }
35 }
36
37 /// The default implementation returns nil to indicate that the formatter object does not provide an attributed string.
38 ///
39 /// # Arguments
40 ///
41 /// * `obj` - The object for which a textual representation is returned.
42 /// * `attrs` - The default attributes to use for the returned attributed string.
43 #[method]
44 pub fn attributed_string_for_object_value_with_default_attributes(
45 &self,
46 obj: id,
47 attrs: NSDictionary<NSAttributedStringKey, id>,
48 ) -> NSAttributedString {
49 unsafe {
50 NSAttributedString::from_id(
51 msg_send![self.m_self(), attributedStringForObjectValue: obj withDefaultAttributes: attrs],
52 )
53 }
54 }
55
56 /// The default implementation of this method invokes string_for_object_value.
57 ///
58 /// # Arguments
59 ///
60 /// * `obj` - The object for which to return an editing string.
61 #[method]
62 pub fn editing_string_for_object_value(&self, obj: id) -> NSString {
63 unsafe { NSString::from_id(msg_send![self.m_self(), editingStringForObjectValue: obj]) }
64 }
65
66 /* Getting Object Values for Textual Representations
67 */
68
69 /// The default implementation of this method raises an exception.
70 ///
71 /// # Arguments
72 ///
73 /// * `obj` - If conversion is successful, upon return contains the object created from string.
74 /// * `string` - The string to parse.
75 /// * `error` - If non-nil, if there is a error during the conversion, upon return contains an NSString object that describes the problem.
76 #[method]
77 pub fn get_object_value_for_string_error_description(
78 &self,
79 obj: &mut id,
80 string: NSString,
81 error: &mut NSString,
82 ) -> bool {
83 unsafe {
84 to_bool(
85 msg_send![self.m_self(), getObjectValue:obj forString:string errorDescription: error ],
86 )
87 }
88 }
89
90 /// Returns a Boolean value that indicates whether a partial string is valid.
91 ///
92 /// # Arguments
93 ///
94 /// * `partial_string` - The text currently in a cell.
95 /// * `new_string` - If partial_string needs to be modified, upon return contains the replacement string.
96 /// * `error` - If non-nil, if validation fails contains an [`NSString`] object that describes the problem.
97 #[method]
98 pub fn is_partial_string_valid_new_editing_string_error_description(
99 &self,
100 partial_string: NSString,
101 new_string: &mut NSString,
102 error: &mut NSString,
103 ) -> bool {
104 unsafe {
105 to_bool(
106 msg_send![self.m_self(), isPartialStringValid: partial_string newEditingString: new_string errorDescription: error],
107 )
108 }
109 }
110
111 /// This method should be implemented in subclasses that want to validate user changes to a string in a
112 /// field, where the user changes are not necessarily at the end of the string, and preserve the selection
113 /// (or set a different one, such as selecting the erroneous part of the string the user has typed).
114 #[method]
115 pub fn is_partial_string_valid_proposed_selected_range_original_string_original_selected_range_error_description(
116 &self,
117 partial_string_ptr: NSString,
118 proposed_sel_range_ptr: NSRangePointer,
119 orig_string: NSString,
120 orig_sel_range: NSRange,
121 error: &mut NSString,
122 ) -> bool {
123 unsafe {
124 to_bool(
125 msg_send![self.m_self(), isPartialStringValid: partial_string_ptr proposedSelectedRange: proposed_sel_range_ptr originalString: orig_string originalSelectedRange: orig_sel_range errorDescription: error],
126 )
127 }
128 }
129}