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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use {
rust_icu_common as common, rust_icu_sys as sys,
rust_icu_sys::versioned_function,
rust_icu_sys::*,
rust_icu_ustring as ustring,
std::{convert::TryFrom, ffi, os::raw, ptr},
};
#[derive(Debug)]
pub struct UFormattable<'a> {
rep: ptr::NonNull<sys::UFormattable>,
owner: Option<&'a Self>,
}
impl<'a> Drop for crate::UFormattable<'a> {
fn drop(&mut self) {
if let None = self.owner {
unsafe { versioned_function!(ufmt_close)(self.rep.as_ptr()) };
}
}
}
macro_rules! simple_getter {
($method_name:ident, $impl_function_name:ident, $return_type:ty) => {
pub fn $method_name(&self) -> Result<$return_type, common::Error> {
let mut status = common::Error::OK_CODE;
let ret = unsafe {
assert!(common::Error::is_ok(status));
versioned_function!($impl_function_name)(self.rep.as_ptr(), &mut status)
};
common::Error::ok_or_warning(status)?;
Ok(ret)
}
};
}
impl<'a> crate::UFormattable<'a> {
pub fn try_new<'b>() -> Result<crate::UFormattable<'b>, common::Error> {
let mut status = common::Error::OK_CODE;
let rep = unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(ufmt_open)(&mut status)
};
common::Error::ok_or_warning(status)?;
Ok(UFormattable {
rep: ptr::NonNull::new(rep).unwrap(),
owner: None,
})
}
#[doc(hidden)]
pub fn as_mut_ptr(&mut self) -> *mut sys::UFormattable {
self.rep.as_ptr()
}
pub fn as_ptr(&self) -> *const sys::UFormattable {
self.rep.as_ptr()
}
pub fn is_numeric(&self) -> bool {
let ubool = unsafe { versioned_function!(ufmt_isNumeric)(self.rep.as_ptr()) };
match ubool {
0i8 => false,
_ => true,
}
}
simple_getter!(get_type, ufmt_getType, sys::UFormattableType);
simple_getter!(get_date, ufmt_getDate, sys::UDate);
simple_getter!(get_double, ufmt_getDouble, f64);
simple_getter!(get_i32, ufmt_getLong, i32);
simple_getter!(get_i64, ufmt_getInt64, i64);
simple_getter!(get_array_length, ufmt_getArrayLength, i32);
pub fn get_ustring(&self) -> Result<ustring::UChar, common::Error> {
let mut status = common::Error::OK_CODE;
let mut ustrlen = 0i32;
let raw = unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(ufmt_getUChars)(self.rep.as_ptr(), &mut ustrlen, &mut status)
} as *mut sys::UChar;
common::Error::ok_or_warning(status)?;
let ret = unsafe {
assert_ne!(raw, 0 as *mut sys::UChar);
assert!(ustrlen >= 0);
ustring::UChar::clone_from_raw_parts(raw, ustrlen)
};
Ok(ret)
}
pub fn get_str(&self) -> Result<String, common::Error> {
let ustr = self.get_ustring()?;
String::try_from(&ustr)
}
pub fn get_array_item_by_index(
&'a self,
index: i32,
) -> Result<crate::UFormattable<'a>, common::Error> {
let mut status = common::Error::OK_CODE;
let raw: *mut sys::UFormattable = unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(ufmt_getArrayItemByIndex)(self.rep.as_ptr(), index, &mut status)
};
common::Error::ok_or_warning(status)?;
assert_ne!(raw, 0 as *mut sys::UFormattable);
Ok(UFormattable {
rep: ptr::NonNull::new(raw).unwrap(),
owner: Some(&self),
})
}
pub fn get_dec_num_chars(&self) -> Result<String, common::Error> {
let mut status = common::Error::OK_CODE;
let mut cstrlen = 0i32;
let raw: *const raw::c_char = unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(ufmt_getDecNumChars)(self.rep.as_ptr(), &mut cstrlen, &mut status)
};
common::Error::ok_or_warning(status)?;
let ret = unsafe {
assert_ne!(raw, 0 as *const raw::c_char);
assert!(cstrlen >= 0);
ffi::CStr::from_ptr(raw)
};
Ok(ret
.to_str()
.map_err(|e: std::str::Utf8Error| common::Error::from(e))?
.to_string())
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn basic() {
let n = crate::UFormattable::try_new().expect("try_new");
assert_eq!(
sys::UFormattableType::UFMT_LONG,
n.get_type().expect("get_type")
);
assert_eq!(0, n.get_i32().expect("get_i32"));
assert_eq!("0", n.get_dec_num_chars().expect("get_dec_num_chars"));
}
}