Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic version of NSError #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
doc/
target/
Cargo.lock
.idea
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding this to your ~/.gitignore_global instead? Unless IntelliJ works differently than how I think it does.

27 changes: 27 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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> {
NSString::from_str(&"NSLocalizedDescription")
}
Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to use the lazy_static! macro, but I think your approach is fine.

In the future something like fruity's nsstring! could be really nice, but it is very difficult to ensure the correctness of, similar to SSheldon/rust-objc#49.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this actually has value "NSLocalizedDescriptionKey" on GNUStep


pub trait INSError: INSObject {
fn new<T>(code: NSInteger, domain: Id<NSString>, user_info: Id<NSDictionary<NSErrorUserInfoKey, T>>) -> Id<Self> {
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 {}

4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -28,6 +30,8 @@ mod array;
mod data;
mod dictionary;
mod enumerator;
mod error;
mod object;
mod runtime;
mod string;
mod value;
2 changes: 2 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub type NSInteger = isize;
pub type NSUInteger = usize;
Comment on lines +1 to +2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these could be useful to have in objc, the reason I haven't added them myself is that I'm yet not completely sure they will always be isize/usize on more exotic platforms like Windows using GNUStep