1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use core::fmt;
use core::panic::{RefUnwindSafe, UnwindSafe};
use super::{
NSCopying, NSDictionary, NSMutableAttributedString, NSMutableCopying, NSObject, NSString,
};
use crate::rc::{DefaultId, Id, Shared};
use crate::runtime::Object;
use crate::{extern_class, extern_methods, msg_send_id, ClassType};
extern_class!(
#[derive(PartialEq, Eq, Hash)]
pub struct NSAttributedString;
unsafe impl ClassType for NSAttributedString {
type Super = NSObject;
}
);
unsafe impl Sync for NSAttributedString {}
unsafe impl Send for NSAttributedString {}
impl UnwindSafe for NSAttributedString {}
impl RefUnwindSafe for NSAttributedString {}
pub type NSAttributedStringKey = NSString;
extern_methods!(
unsafe impl NSAttributedString {
pub fn new() -> Id<Self, Shared> {
unsafe { msg_send_id![Self::class(), new] }
}
#[doc(alias = "initWithString:")]
pub fn new_with_attributes(
string: &NSString,
attributes: &NSDictionary<NSAttributedStringKey, Object>,
) -> Id<Self, Shared> {
unsafe {
let obj = msg_send_id![Self::class(), alloc];
msg_send_id![obj, initWithString: string, attributes: attributes]
}
}
#[doc(alias = "initWithString:")]
pub fn from_nsstring(string: &NSString) -> Id<Self, Shared> {
unsafe {
let obj = msg_send_id![Self::class(), alloc];
msg_send_id![obj, initWithString: string]
}
}
}
unsafe impl NSAttributedString {
pub fn string(&self) -> Id<NSString, Shared> {
unsafe { msg_send_id![self, string] }
}
#[doc(alias = "length")]
#[sel(length)]
pub fn len_utf16(&self) -> usize;
}
);
impl DefaultId for NSAttributedString {
type Ownership = Shared;
#[inline]
fn default_id() -> Id<Self, Self::Ownership> {
Self::new()
}
}
unsafe impl NSCopying for NSAttributedString {
type Ownership = Shared;
type Output = NSAttributedString;
}
unsafe impl NSMutableCopying for NSAttributedString {
type Output = NSMutableAttributedString;
}
impl alloc::borrow::ToOwned for NSAttributedString {
type Owned = Id<NSAttributedString, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}
impl fmt::Debug for NSAttributedString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let obj: &NSObject = self;
fmt::Debug::fmt(obj, f)
}
}
#[cfg(test)]
mod tests {
use alloc::string::ToString;
use alloc::{format, vec};
use super::*;
use crate::rc::{autoreleasepool, Owned};
#[test]
fn test_new() {
let s = NSAttributedString::new();
assert_eq!(&s.string().to_string(), "");
}
#[test]
fn test_string_bound_to_attributed() {
let attr_s = {
let source = NSString::from_str("Hello world!");
NSAttributedString::from_nsstring(&source)
};
let s = autoreleasepool(|_| attr_s.string());
assert_eq!(s.len(), 12);
}
#[test]
fn test_from_nsstring() {
let s = NSAttributedString::from_nsstring(&NSString::from_str("abc"));
assert_eq!(&s.string().to_string(), "abc");
}
#[test]
fn test_copy() {
let s1 = NSAttributedString::from_nsstring(&NSString::from_str("abc"));
let s2 = s1.copy();
assert!(s2.is_kind_of::<NSAttributedString>());
let s3 = s1.mutable_copy();
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3).cast());
assert!(s3.is_kind_of::<NSMutableAttributedString>());
}
#[test]
fn test_debug() {
let s = NSAttributedString::from_nsstring(&NSString::from_str("abc"));
let expected = if cfg!(feature = "gnustep-1-7") {
"abc{}"
} else {
"abc{\n}"
};
assert_eq!(format!("{:?}", s), expected);
let obj: Id<Object, Owned> = unsafe { Id::cast(NSObject::new()) };
let ptr: *const Object = &*obj;
let s = NSAttributedString::new_with_attributes(
&NSString::from_str("abc"),
&NSDictionary::from_keys_and_objects(&[&*NSString::from_str("test")], vec![obj]),
);
let expected = if cfg!(feature = "gnustep-1-7") {
format!("abc{{test = \"<NSObject: {:?}>\"; }}", ptr)
} else {
format!("abc{{\n test = \"<NSObject: {:?}>\";\n}}", ptr)
};
assert_eq!(format!("{:?}", s), expected);
}
}