-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inspiration from: - SSheldon/rust-objc-foundation#16 - https://github.com/nvzqz/fruity/tree/v0.3.0/src/foundation/ns_error - https://github.com/ryanmcgrath/cacao/blob/02946476837e45ee381e37dde2946ed63b44dc31/src/error.rs
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use core::fmt; | ||
use core::panic::{RefUnwindSafe, UnwindSafe}; | ||
|
||
use objc2::ffi::NSInteger; | ||
use objc2::rc::{Id, Shared}; | ||
use objc2::{msg_send, msg_send_id}; | ||
|
||
use crate::{extern_class, NSDictionary, NSObject, NSString}; | ||
|
||
extern_class! { | ||
/// Information about an error condition including a domain, a | ||
/// domain-specific error code, and application-specific information. | ||
/// | ||
/// See also [Apple's documentation][doc]. | ||
/// | ||
/// [doc]: https://developer.apple.com/documentation/foundation/nserror?language=objc | ||
#[derive(PartialEq, Eq, Hash)] | ||
unsafe pub struct NSError: NSObject; | ||
} | ||
|
||
// SAFETY: Error objects are immutable data containers. | ||
unsafe impl Sync for NSError {} | ||
unsafe impl Send for NSError {} | ||
|
||
impl UnwindSafe for NSError {} | ||
impl RefUnwindSafe for NSError {} | ||
|
||
pub type NSErrorUserInfoKey = NSString; | ||
pub type NSErrorDomain = NSString; | ||
|
||
/// Creation methods. | ||
impl NSError { | ||
/// Construct a new [`NSError`] with the given code in the given domain. | ||
pub fn new(code: NSInteger, domain: &NSString) -> Id<Self, Shared> { | ||
unsafe { Self::with_user_info(code, domain, None) } | ||
} | ||
|
||
// TODO: Figure out safety of `user_info` dict! | ||
unsafe fn with_user_info( | ||
code: NSInteger, | ||
domain: &NSString, | ||
user_info: Option<&NSDictionary<NSErrorUserInfoKey, NSObject>>, | ||
) -> Id<Self, Shared> { | ||
// SAFETY: `domain` and `user_info` are copied to the error object, so | ||
// even if the `&NSString` came from a `&mut NSMutableString`, we're | ||
// still good! | ||
unsafe { | ||
msg_send_id![ | ||
msg_send_id![Self::class(), alloc], | ||
initWithDomain: domain, | ||
code: code, | ||
userInfo: user_info, | ||
] | ||
.expect("unexpected NULL NSError") | ||
} | ||
} | ||
} | ||
|
||
/// Accessor methods. | ||
impl NSError { | ||
pub fn domain(&self) -> Id<NSString, Shared> { | ||
unsafe { msg_send_id![self, domain].expect("unexpected NULL NSError domain") } | ||
} | ||
|
||
pub fn code(&self) -> NSInteger { | ||
unsafe { msg_send![self, code] } | ||
} | ||
|
||
pub fn user_info(&self) -> Option<Id<NSDictionary<NSErrorUserInfoKey, NSObject>, Shared>> { | ||
unsafe { msg_send_id![self, userInfo] } | ||
} | ||
|
||
pub fn localized_description(&self) -> Id<NSString, Shared> { | ||
unsafe { | ||
msg_send_id![self, localizedDescription].expect( | ||
"unexpected NULL localized description; a default should have been generated!", | ||
) | ||
} | ||
} | ||
|
||
// TODO: localizedRecoveryOptions | ||
// TODO: localizedRecoverySuggestion | ||
// TODO: localizedFailureReason | ||
// TODO: helpAnchor | ||
// TODO: +setUserInfoValueProviderForDomain:provider: | ||
// TODO: +userInfoValueProviderForDomain: | ||
|
||
// TODO: recoveryAttempter | ||
// TODO: attemptRecoveryFromError:... | ||
|
||
// TODO: Figure out if this is a good design, or if we should do something | ||
// differently (like a Rusty name for the function, or putting a bunch of | ||
// statics in a module instead)? | ||
#[allow(non_snake_case)] | ||
pub fn NSLocalizedDescriptionKey() -> &'static NSErrorUserInfoKey { | ||
extern "C" { | ||
#[link_name = "NSLocalizedDescriptionKey"] | ||
static VALUE: &'static NSErrorUserInfoKey; | ||
} | ||
unsafe { VALUE } | ||
} | ||
|
||
// TODO: Other NSErrorUserInfoKey values | ||
// TODO: NSErrorDomain values | ||
} | ||
|
||
impl fmt::Debug for NSError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("NSError") | ||
.field("domain", &self.domain()) | ||
.field("code", &self.code()) | ||
.field("user_info", &self.user_info()) | ||
.finish() | ||
} | ||
} | ||
|
||
impl fmt::Display for NSError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.localized_description()) | ||
} | ||
} | ||
|
||
impl std::error::Error for NSError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters