rust_macios/foundation/
ns_error.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::{Int, NSArray, NSDictionary, NSErrorDomain, NSErrorUserInfoKey, NSString, UInt};
13
14object! {
15    /// Information about an error condition including a domain, a domain-specific
16    /// error code, and application-specific information.
17    unsafe pub struct NSError;
18}
19
20#[interface_impl(NSObject)]
21impl NSError {
22    /* Creating Error Objects
23     */
24
25    /// Creates and initializes an NSError object for a given domain and code with a given userInfo dictionary.
26    #[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    /// Returns an NSError object initialized for a given domain and code with a given userInfo dictionary.
43    #[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    /* Getting Error Properties
61     */
62
63    /// The error code.
64    #[property]
65    pub fn code(&self) -> Int {
66        unsafe { msg_send![self.m_self(), code] }
67    }
68
69    /// A string containing the error domain.
70    #[property]
71    pub fn domain(&self) -> NSErrorDomain {
72        unsafe { msg_send![self.m_self(), domain] }
73    }
74
75    /// The user info dictionary.
76    #[property]
77    pub fn user_info(&self) -> NSDictionary<NSErrorUserInfoKey, id> {
78        unsafe { NSDictionary::from_id(msg_send![self.m_self(), userInfo]) }
79    }
80
81    /* Getting a Localized Error Description */
82
83    /// A string containing the localized description of the error.
84    #[property]
85    pub fn localized_description(&self) -> NSString {
86        unsafe { NSString::from_id(msg_send![self.m_self(), localizedDescription]) }
87    }
88
89    /// An array containing the localized titles of buttons appropriate for displaying in an alert panel.
90    #[property]
91    pub fn localized_recovery_options(&self) -> NSArray<NSString> {
92        unsafe { NSArray::from_id(msg_send![self.m_self(), localizedRecoveryOptions]) }
93    }
94
95    /// A string containing the localized recovery suggestion for the error.
96    #[property]
97    pub fn localized_recovery_suggestion(&self) -> NSString {
98        unsafe { NSString::from_id(msg_send![self.m_self(), localizedRecoverySuggestion]) }
99    }
100
101    /// A string containing the localized explanation of the reason for the error.
102    #[property]
103    pub fn localized_failure_reason(&self) -> NSString {
104        unsafe { NSString::from_id(msg_send![self.m_self(), localizedFailureReason]) }
105    }
106
107    /// The object in the user info dictionary corresponding to the NSRecoveryAttempterErrorKey key.
108    #[property]
109    pub fn recovery_attempter(&self) -> id {
110        unsafe { msg_send![self.m_self(), recoveryAttempter] }
111    }
112
113    /// Implemented to attempt a recovery from an error noted in an application-modal dialog.
114    #[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    /// A string to display in response to an alert panel help anchor button being pressed.
128    #[method]
129    pub fn help_anchor(&self) -> NSString {
130        unsafe { NSString::from_id(msg_send![self.m_self(), helpAnchor]) }
131    }
132
133    ///
134    #[property]
135    pub fn underlying_errors(&self) -> NSArray<NSError> {
136        unsafe { NSArray::from_id(msg_send![self.m_self(), underlyingErrors]) }
137    }
138}