From 65b5a3112a4d1bfc9c3775b3cf241ff87b49e858 Mon Sep 17 00:00:00 2001 From: Rob Napier Date: Tue, 23 Nov 2021 18:01:39 -0500 Subject: [PATCH] Basic version of NSError --- .gitignore | 1 + src/error.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 4 ++++ src/runtime.rs | 2 ++ 4 files changed, 34 insertions(+) create mode 100644 src/error.rs create mode 100644 src/runtime.rs diff --git a/.gitignore b/.gitignore index 1689f08..685e1bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ doc/ target/ Cargo.lock +.idea diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..23f7f56 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,27 @@ +use objc_id::Id; +use {INSObject, NSInteger, NSDictionary, NSString, INSString}; + +pub type NSErrorUserInfoKey = NSString; + +#[allow(non_snake_case)] +pub fn NSLocalizedDescriptionKey() -> Id { + NSString::from_str(&"NSLocalizedDescription") +} + +pub trait INSError: INSObject { + fn new(code: NSInteger, domain: Id, user_info: Id>) -> Id { + let cls = Self::class(); + unsafe { + let obj: *mut Self = msg_send![cls, alloc]; + let obj: *mut Self = msg_send![obj, initWithDomain:domain + code:code + userInfo:user_info]; + Id::from_retained_ptr(obj) + } + } +} + +object_struct!(NSError); + +impl INSError for NSError {} + diff --git a/src/lib.rs b/src/lib.rs index 65bbd01..64121cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,9 @@ pub use self::array::{ pub use self::data::{INSData, INSMutableData, NSData, NSMutableData}; pub use self::dictionary::{INSDictionary, NSDictionary}; pub use self::enumerator::{INSFastEnumeration, NSEnumerator, NSFastEnumerator}; +pub use self::error::{INSError, NSError, NSLocalizedDescriptionKey}; pub use self::object::{INSObject, NSObject}; +pub use self::runtime::{NSInteger, NSUInteger}; pub use self::string::{INSCopying, INSMutableCopying, INSString, NSString}; pub use self::value::{INSValue, NSValue}; @@ -28,6 +30,8 @@ mod array; mod data; mod dictionary; mod enumerator; +mod error; mod object; +mod runtime; mod string; mod value; diff --git a/src/runtime.rs b/src/runtime.rs new file mode 100644 index 0000000..b36fdba --- /dev/null +++ b/src/runtime.rs @@ -0,0 +1,2 @@ +pub type NSInteger = isize; +pub type NSUInteger = usize;