rust_macios/natural_language/
nl_gazetteer.rs1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4 foundation::{NSArray, NSData, NSDictionary, NSError, NSString, NSURL},
5 object,
6 objective_c_runtime::{
7 macros::interface_impl,
8 nil,
9 traits::{FromId, PNSObject},
10 },
11 utils::{to_bool, to_optional},
12};
13
14use super::NLLanguage;
15
16object! {
17 unsafe pub struct NLGazetteer;
19}
20
21#[interface_impl(NSObject)]
22impl NLGazetteer {
23 #[method]
28 pub fn init_with_contents_of_url(url: &NSURL) -> Result<Self, NSError>
29 where
30 Self: Sized + FromId,
31 {
32 unsafe {
33 let mut error = NSError::m_alloc();
34
35 let ptr = Self::from_id(
36 msg_send![Self::m_class(), initWithContentsOfURL:url.m_self() error: &mut error],
37 );
38
39 if error.m_self() != nil {
40 Err(error)
41 } else {
42 Ok(ptr)
43 }
44 }
45 }
46
47 #[method]
49 pub fn init_with_data(&mut self, data: &NSData) -> Result<Self, NSError>
50 where
51 Self: Sized + FromId,
52 {
53 unsafe {
54 let mut error = NSError::m_alloc();
55
56 let ptr = Self::from_id(
57 msg_send![self.m_self(), initWithData: data.m_self() error: &mut error],
58 );
59
60 if error.m_self() != nil {
61 Err(error)
62 } else {
63 Ok(ptr)
64 }
65 }
66 }
67
68 #[method]
70 pub fn init_with_dictionary_language(
71 &mut self,
72 dictionary: &NSDictionary<NSString, NSArray<NSString>>,
73 language: Option<NLLanguage>,
74 ) -> Result<Self, NSError>
75 where
76 Self: Sized + FromId,
77 {
78 let language = match language {
79 Some(value) => value.m_self(),
80 None => nil,
81 };
82
83 let mut error = NSError::m_alloc();
84
85 unsafe {
86 let ptr = Self::from_id(
87 msg_send![self.m_self(), initWithDictionary:dictionary.m_self() language: language error: &mut error],
88 );
89
90 if error.m_self() != nil {
91 Err(error)
92 } else {
93 Ok(ptr)
94 }
95 }
96 }
97
98 #[method]
100 pub fn write_gazetteer_for_dictionary_language_to_url(
101 dictionary: &NSDictionary<NSString, NSArray<NSString>>,
102 language: Option<NLLanguage>,
103 url: &NSURL,
104 ) -> Result<bool, NSError> {
105 let mut error = NSError::m_alloc();
106
107 unsafe {
108 let ptr = to_bool(
109 msg_send![Self::m_class(), writeGazetteerForDictionary:dictionary.m_self() language: language toURL: url.m_self() error: &mut error],
110 );
111
112 if error.m_self() != nil {
113 Err(error)
114 } else {
115 Ok(ptr)
116 }
117 }
118 }
119
120 #[method]
125 pub fn label_for_string(&self, string: &NSString) -> Option<NSString> {
126 unsafe { to_optional(msg_send![self.m_self(), labelForString: string.m_self()]) }
127 }
128
129 #[property]
134 pub fn data(&self) -> NSData {
135 unsafe { NSData::from_id(msg_send![self.m_self(), data]) }
136 }
137
138 #[property]
140 pub fn set_data(&mut self, data: NSData) -> NSData {
141 unsafe { msg_send![self.m_self(), setData: data] }
142 }
143
144 #[property]
146 pub fn language(&self) -> Option<NLLanguage> {
147 unsafe { to_optional(msg_send![self.m_self(), language]) }
148 }
149}