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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // The message format C api is variadic. #![feature(c_variadic)] //! # Locale-aware message formatting. //! //! Implementation of the text formatting code from the ICU4C //! [`umsg.h`](https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/umsg_8h.html) header. //! Skip to the section ["Example use"](#example-use) below if you want to see it in action. //! //! The library inherits all pattern and formatting specifics from the corresponding [ICU C++ //! API](https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1MessageFormat.html). //! //! This is the support for [MessageFormat](http://userguide.icu-project.org/formatparse/messages) //! message formatting. The `MessageFormat` uses ICU data to format text properly based on the //! locale selected at formatter initialization. This includes formatting dates, times, //! currencies, and other text. //! //! > **Note:** The `MessageFormat` library does not handle loading the format patterns in the //! > appropriate language. This task is left to the application author. //! //! # Example use //! //! The example below shows how to format values into an English text. For more detail about //! formatting specifics see [message_format!]. //! //! ``` //! use rust_icu_sys as sys; //! use rust_icu_common as common; //! use rust_icu_ustring as ustring; //! use rust_icu_uloc as uloc; //! use rust_icu_umsg::{self as umsg, message_format}; //! # use rust_icu_ucal as ucal; //! # use std::convert::TryFrom; //! # //! # struct TzSave(String); //! # impl Drop for TzSave { //! # fn drop(&mut self) { //! # ucal::set_default_time_zone(&self.0); //! # } //! # } //! //! fn testfn() -> Result<(), common::Error> { //! # let _ = TzSave(ucal::get_default_time_zone()?); //! # ucal::set_default_time_zone("Europe/Amsterdam")?; //! let loc = uloc::ULoc::try_from("en-US-u-tz-uslax")?; //! let msg = ustring::UChar::try_from( //! r"Formatted double: {0,number,##.#}, //! Formatted integer: {1,number,integer}, //! Formatted string: {2}, //! Date: {3,date,full}", //! )?; //! //! let fmt = umsg::UMessageFormat::try_from(&msg, &loc)?; //! let hello = ustring::UChar::try_from("Hello! Добар дан!")?; //! let result = umsg::message_format!( //! fmt, //! { 43.4 => Double }, //! { 31337 => Integer}, //! { hello => String}, //! { 0.0 => Date } //! )?; //! //! assert_eq!( //! r"Formatted double: 43.4, //! Formatted integer: 31,337, //! Formatted string: Hello! Добар дан!, //! Date: Thursday, January 1, 1970", //! result //! ); //! Ok(()) //! } //! # fn main() -> Result<(), common::Error> { //! # testfn() //! # } //! ``` use { rust_icu_common as common, rust_icu_sys as sys, rust_icu_sys::*, rust_icu_uloc as uloc, rust_icu_ustring as ustring, std::convert::TryFrom, }; /// The implementation of the ICU `UMessageFormat*`. /// /// Use the [UMessageFormat::try_from] to create a message formatter for a given message pattern in /// the [Messageformat](http://userguide.icu-project.org/formatparse/messages) and a specified /// locale. Use the macro [message_format!] to actually format the arguments. /// /// [UMessageFormat] supports very few methods when compared to the wealth of functions that one /// can see in /// [`umsg.h`](https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/umsg_8h.html). It is /// not clear that other functions available there offer significantly more functionality than is /// given here. /// /// If, however, you find that the set of methods implemented at the moment are not adequate, feel /// free to provide a [pull request](https://github.com/google/rust_icu/pulls) implementing what /// you need. /// /// Implements `UMessageFormat`. #[derive(Debug)] pub struct UMessageFormat { rep: std::rc::Rc<Rep>, } // An internal representation of the message formatter, used to allow cloning. #[derive(Debug)] struct Rep { rep: *mut sys::UMessageFormat, } impl Drop for Rep { /// Drops the content of [sys::UMessageFormat] and releases its memory. /// /// Implements `umsg_close`. fn drop(&mut self) { unsafe { versioned_function!(umsg_close)(self.rep); } } } impl Clone for UMessageFormat { /// Implements `umsg_clone`. fn clone(&self) -> Self { // Note this is not OK if UMessageFormat ever grows mutable methods. UMessageFormat { rep: self.rep.clone(), } } } impl UMessageFormat { /// Creates a new message formatter. /// /// A single message formatter is created per each pattern-locale combination. Mutable methods /// from [`umsg`](https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/umsg_8h.html) /// are not implemented, and for now requires that all formatting be separate. /// /// Implements `umsg_open`. pub fn try_from( pattern: &ustring::UChar, locale: &uloc::ULoc, ) -> Result<UMessageFormat, common::Error> { let pstr = pattern.as_c_ptr(); let loc = locale.as_c_str(); let mut status = common::Error::OK_CODE; let mut parse_status = common::NO_PARSE_ERROR.clone(); let rep = unsafe { assert!(common::Error::is_ok(status)); versioned_function!(umsg_open)( pstr, pattern.len() as i32, loc.as_ptr(), &mut parse_status, &mut status, ) }; common::Error::ok_or_warning(status)?; common::parse_ok(parse_status)?; Ok(UMessageFormat { rep: std::rc::Rc::new(Rep { rep }), }) } } /// Given a formatter, formats the passed arguments into the formatter's message. /// /// The general usage pattern for the formatter is as follows, assuming that `formatter` /// is an appropriately initialized [UMessageFormat]: /// /// ``` ignore /// use rust_icu_umsg as umsg; /// // let result = umsg::message_format!( /// // formatter, [{ value => <type_assertion> }, ...]); /// let result = umsg::message_format!(formatter, { 31337 => Integer }); /// ``` /// /// Each fragment `{ value => <type_assertion> }` represents a single positional parameter binding /// for the pattern in `formatter`. The first fragment corresponds to the positional parameter `0` /// (which, if an integer, would be referred to as `{0,number,integer}` in a MessageFormat /// pattern). Since the original C API that this rust library is generated for uses variadic /// functions for parameter passing, it is very important that the programmer matches the actual /// parameter types to the types that are expected in the pattern. /// /// > **Note:** If the types of parameter bindings do not match the expectations in the pattern, /// > memory corruption may occur, so tread lightly here. /// /// In general this is very brittle, and an API in a more modern lanugage, or a contemporary C++ /// flavor would probably take a different route were the library to be written today. The rust /// binding tries to make the API use a bit more palatable by requiring that the programmer /// explicitly specifies a type for each of the parameters to be passed into the formatter. /// /// The supported types are not those of a full rust system, but rather a very restricted subset /// of types that MessageFormat supports: /// /// | Type | Description | /// | ---- | ----------- | /// | Double | This is effectively rust type `f64`. Any numeric parameter not specifically designated as different type, is always a double. See section below on Doubles. | /// | String | This is effectively [rust_icu_ustring::UChar].| /// | Integer | This is effectively `i32`, and is used | /// | Date | This is effectively [rust_icu_sys::UDate], and is used to format dates. Depending on the date format requested in the pattern used in [UMessageFormat], the end result of date formatting could be one of a wide variety of [date formats](http://userguide.icu-project.org/formatparse/datetime).| /// /// ## Double as numeric parameter /// /// According to the [ICU documentation for /// `umsg_format`](https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/umsg_8h.html#a90a4b5fe778754e5da52f7c2e5fd6048): /// /// > for all numeric arguments double is assumed unless the type is explicitly /// > integer (long). All choice format arguments must be of type double. /// /// ## Strings /// /// We determined by code inspection that the string format must be `rust_icu_ustring::UChar`. /// /// # Example use /// /// ``` /// use rust_icu_sys as sys; /// use rust_icu_common as common; /// use rust_icu_ustring as ustring; /// use rust_icu_uloc as uloc; /// use rust_icu_umsg::{self as umsg, message_format}; /// # use rust_icu_ucal as ucal; /// # use std::convert::TryFrom; /// # /// # struct TzSave(String); /// # impl Drop for TzSave { /// # // Restore the system time zone upon exit. /// # fn drop(&mut self) { /// # ucal::set_default_time_zone(&self.0); /// # } /// # } /// /// fn testfn() -> Result<(), common::Error> { /// # let _ = TzSave(ucal::get_default_time_zone()?); /// # ucal::set_default_time_zone("Europe/Amsterdam")?; /// let loc = uloc::ULoc::try_from("en-US")?; /// let msg = ustring::UChar::try_from( /// r"Formatted double: {0,number,##.#}, /// Formatted integer: {1,number,integer}, /// Formatted string: {2}, /// Date: {3,date,full}", /// )?; /// /// let fmt = umsg::UMessageFormat::try_from(&msg, &loc)?; /// let hello = ustring::UChar::try_from("Hello! Добар дан!")?; /// let result = umsg::message_format!( /// fmt, /// { 43.4 => Double }, /// { 31337 => Integer}, /// { hello => String}, /// { 0.0 => Date } /// )?; /// /// assert_eq!( /// r"Formatted double: 43.4, /// Formatted integer: 31,337, /// Formatted string: Hello! Добар дан!, /// Date: Thursday, January 1, 1970", /// result /// ); /// Ok(()) /// } /// # fn main() -> Result<(), common::Error> { /// # testfn() /// # } /// ``` /// /// Implements `umsg_format`. /// Implements `umsg_vformat`. #[macro_export] macro_rules! message_format { ($dest:expr) => { panic!("you should not format a message without parameters") }; ($dest:expr, $( {$arg:expr => $t:ident}),* ) => { unsafe { $crate::format_varargs( &$dest, $( $crate::checkarg!($arg, $t) ),* ) } }; } #[doc(hidden)] #[macro_export] macro_rules! checkarg { ($e:expr, Double) => {{ { let _: f64 = $e; $e } }}; ($e:expr, String) => {{ { let _: &ustring::UChar = &$e; $e.as_c_ptr() } }}; ($e:expr, Integer) => {{ { let _: i32 = $e; $e } }}; ($e:expr, Long) => {{ { let _: i64 = $e; $e } }}; ($e:expr, Date) => {{ { let _: sys::UDate = $e; $e } }}; } // TODO: is there a way to *not* expose this function? // Has to be declared as extern "C" to allow for variadic args; but since the // return type is not C-clean, then we need to suppress the improper type // definition. #[allow(improper_ctypes_definitions)] #[no_mangle] #[doc(hidden)] pub unsafe extern "C" fn format_varargs( fmt: &UMessageFormat, args: ... ) -> Result<String, common::Error> { const CAP: usize = 1024; let mut status = common::Error::OK_CODE; let mut result = ustring::UChar::new_with_capacity(CAP); let total_size = { args.with_copy(|va_list| { // Overlay a __va_list_tag on top of our va_list. // // This is terribly unsafe, and hinges on the assumption that the memory layout of // `va_list` type above (which is a rust type) is exactly the same as the memory layout // of `__va_list_tag` (which is a type generated by bindgen). This *should* be true for // each architecture that the rust compiler supports, but is apparently not tested well // today. let valist: *mut __va_list_tag = std::mem::transmute(va_list); assert!(common::Error::is_ok(status)); versioned_function!(umsg_vformat)( fmt.rep.rep, result.as_mut_c_ptr(), CAP as i32, valist, &mut status, ) }) } as usize; common::Error::ok_or_warning(status)?; result.resize(total_size as usize); if total_size > CAP { args.with_copy(|va_list| { // See the safety note in the similar call above. let valist: *mut __va_list_tag = std::mem::transmute(va_list); assert!(common::Error::is_ok(status)); versioned_function!(umsg_vformat)( fmt.rep.rep, result.as_mut_c_ptr(), total_size as i32, valist, &mut status, ); }); common::Error::ok_or_warning(status)?; } String::try_from(&result) } #[cfg(test)] mod tests { use super::*; use rust_icu_ucal as ucal; struct TzSave(String); impl Drop for TzSave { // Restore the system time zone upon exit. fn drop(&mut self) { ucal::set_default_time_zone(&self.0).expect("timezone set success"); } } #[test] fn tzsave() -> Result<(), common::Error> { let _ = TzSave(ucal::get_default_time_zone()?); ucal::set_default_time_zone("Europe/Amsterdam")?; Ok(()) } #[test] fn basic() -> Result<(), common::Error> { let _ = TzSave(ucal::get_default_time_zone()?); ucal::set_default_time_zone("Europe/Amsterdam")?; let loc = uloc::ULoc::try_from("en-US")?; let msg = ustring::UChar::try_from( r"Formatted double: {0,number,##.#}, Formatted integer: {1,number,integer}, Formatted string: {2}, Date: {3,date,full}", )?; let fmt = crate::UMessageFormat::try_from(&msg, &loc)?; let hello = ustring::UChar::try_from("Hello! Добар дан!")?; let value: i32 = 31337; let result = message_format!( fmt, { 43.4 => Double }, { value => Integer}, { hello => String}, { 0.0 => Date } )?; assert_eq!( r"Formatted double: 43.4, Formatted integer: 31,337, Formatted string: Hello! Добар дан!, Date: Thursday, January 1, 1970", result ); Ok(()) } #[test] #[should_panic] fn empty_args_in_format() { let _ = TzSave(ucal::get_default_time_zone().unwrap()); ucal::set_default_time_zone("Europe/Amsterdam").unwrap(); let loc = uloc::ULoc::try_from("en-US").unwrap(); let msg = ustring::UChar::try_from( r"Formatted double: {0,number,##.#}, Formatted integer: {1,number,integer}, Formatted string: {2}, Date: {3,date,full}", ).unwrap(); let _fmt = crate::UMessageFormat::try_from(&msg, &loc).unwrap(); // This is not allowed! let _ = message_format!(&_fmt); } #[test] fn clone() -> Result<(), common::Error> { let loc = uloc::ULoc::try_from("en-US-u-tz-uslax")?; let msg = ustring::UChar::try_from(r"Formatted double: {0,number,##.#}")?; let fmt = crate::UMessageFormat::try_from(&msg, &loc)?; let result = message_format!(fmt.clone(), { 43.43 => Double })?; assert_eq!(r"Formatted double: 43.4", result); Ok(()) } }