rust_macios/foundation/
ns_error.rs1use 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::{Int, NSArray, NSDictionary, NSErrorDomain, NSErrorUserInfoKey, NSString, UInt};
13
14object! {
15 unsafe pub struct NSError;
18}
19
20#[interface_impl(NSObject)]
21impl NSError {
22 #[method]
27 pub fn error_with_domain_code_user_info(
28 domain: NSErrorDomain,
29 code: Int,
30 dict: NSDictionary<NSErrorUserInfoKey, id>,
31 ) -> Self
32 where
33 Self: Sized + FromId,
34 {
35 unsafe {
36 Self::from_id(
37 msg_send![Self::m_class(), errorWithDomain: domain code: code userInfo: dict],
38 )
39 }
40 }
41
42 #[method]
44 pub fn init_with_domain_code_user_info(
45 &mut self,
46 domain: NSErrorDomain,
47 code: Int,
48 dict: NSDictionary<NSErrorUserInfoKey, id>,
49 ) -> Self
50 where
51 Self: Sized + FromId,
52 {
53 unsafe {
54 Self::from_id(
55 msg_send![self.m_self(), initWithDomain: domain code: code userInfo: dict],
56 )
57 }
58 }
59
60 #[property]
65 pub fn code(&self) -> Int {
66 unsafe { msg_send![self.m_self(), code] }
67 }
68
69 #[property]
71 pub fn domain(&self) -> NSErrorDomain {
72 unsafe { msg_send![self.m_self(), domain] }
73 }
74
75 #[property]
77 pub fn user_info(&self) -> NSDictionary<NSErrorUserInfoKey, id> {
78 unsafe { NSDictionary::from_id(msg_send![self.m_self(), userInfo]) }
79 }
80
81 #[property]
85 pub fn localized_description(&self) -> NSString {
86 unsafe { NSString::from_id(msg_send![self.m_self(), localizedDescription]) }
87 }
88
89 #[property]
91 pub fn localized_recovery_options(&self) -> NSArray<NSString> {
92 unsafe { NSArray::from_id(msg_send![self.m_self(), localizedRecoveryOptions]) }
93 }
94
95 #[property]
97 pub fn localized_recovery_suggestion(&self) -> NSString {
98 unsafe { NSString::from_id(msg_send![self.m_self(), localizedRecoverySuggestion]) }
99 }
100
101 #[property]
103 pub fn localized_failure_reason(&self) -> NSString {
104 unsafe { NSString::from_id(msg_send![self.m_self(), localizedFailureReason]) }
105 }
106
107 #[property]
109 pub fn recovery_attempter(&self) -> id {
110 unsafe { msg_send![self.m_self(), recoveryAttempter] }
111 }
112
113 #[method]
115 pub fn attempt_recovery_from_error_option_index(
116 &self,
117 error: NSError,
118 recovery_index: UInt,
119 ) -> bool {
120 unsafe {
121 to_bool(
122 msg_send![self.m_self(), attemptRecoveryFromError: error optionIndex: recovery_index],
123 )
124 }
125 }
126
127 #[method]
129 pub fn help_anchor(&self) -> NSString {
130 unsafe { NSString::from_id(msg_send![self.m_self(), helpAnchor]) }
131 }
132
133 #[property]
135 pub fn underlying_errors(&self) -> NSArray<NSError> {
136 unsafe { NSArray::from_id(msg_send![self.m_self(), underlyingErrors]) }
137 }
138}