Skip to content

Commit d191c28

Browse files
Add missing Language docs + doc tests. (#233)
1 parent f68701a commit d191c28

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

harfbuzz/src/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl Buffer {
198198
///
199199
/// Finally, if buffer language is not set (ie. is `HB_LANGUAGE_INVALID`),
200200
/// it will be set to the process's default language as returned by
201-
/// `hb_language_get_default()`. This may change in the future by
201+
/// [`Language::get_process_default()`]. This may change in the future by
202202
/// taking buffer script into consideration when choosing a language.
203203
///
204204
/// ```

harfbuzz/src/language.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
#![allow(missing_docs)]
11-
1210
use crate::sys;
1311

12+
/// A language tag.
13+
///
14+
/// This corresponds to a [BCP 47] language tag.
15+
///
16+
/// This is a wrapper around the [`hb_language_t`] type from the
17+
/// [`harfbuzz-sys`](crate::sys) crate.
18+
///
19+
/// [`hb_language_t`]: crate::sys::hb_language_t
20+
/// [BCP 47]: https://tools.ietf.org/html/bcp47
1421
#[derive(Copy, Clone, PartialEq, PartialOrd)]
1522
pub struct Language {
1623
/// The underlying `hb_language_t` from the `harfbuzz-sys` crate.
@@ -22,6 +29,26 @@ pub struct Language {
2229
}
2330

2431
impl Language {
32+
/// Construct a `Language` from a string.
33+
///
34+
/// The string should be a [BCP 47] language tag.
35+
///
36+
/// Example:
37+
///
38+
/// ```
39+
/// let lang = harfbuzz::Language::from_string("en-US");
40+
/// assert!(lang.is_valid());
41+
/// let lang = harfbuzz::Language::from_string("ja");
42+
/// assert!(lang.is_valid());
43+
/// let lang = harfbuzz::Language::from_string("zh-Hant");
44+
/// assert!(lang.is_valid());
45+
/// let lang = harfbuzz::Language::from_string("sr-Latn-RS");
46+
/// assert!(lang.is_valid());
47+
/// let lang = harfbuzz::Language::from_string("");
48+
/// assert!(!lang.is_valid());
49+
/// ```
50+
///
51+
/// [BCP 47]: https://tools.ietf.org/html/bcp47
2552
pub fn from_string(lang: &str) -> Self {
2653
Language {
2754
raw: unsafe {
@@ -33,6 +60,13 @@ impl Language {
3360
}
3461
}
3562

63+
/// Converts the language to a string.
64+
///
65+
/// Example:
66+
/// ```
67+
/// let lang = harfbuzz::Language::from_string("en-US");
68+
/// assert_eq!(lang.to_string(), "en-us");
69+
/// ```
3670
pub fn to_string(&self) -> &str {
3771
unsafe { std::ffi::CStr::from_ptr(sys::hb_language_to_string(self.raw)) }
3872
.to_str()
@@ -48,16 +82,36 @@ impl Language {
4882
Language { raw }
4983
}
5084

85+
/// Convert the `Language` to a raw pointer.
86+
///
87+
/// This is useful for interfacing with functions from the
88+
/// [`harfbuzz-sys`](crate::sys) crate that haven't been safely exposed.
5189
pub fn as_raw(self) -> sys::hb_language_t {
5290
self.raw
5391
}
5492

93+
/// Returns the default language for the process locale.
94+
///
95+
/// See [`hb_language_get_default()`] for more information.
96+
///
97+
/// Example:
98+
///
99+
/// ```
100+
/// let lang = harfbuzz::Language::get_process_default();
101+
/// assert!(lang.is_valid());
102+
/// ```
103+
///
104+
/// [`hb_language_get_default()`]: https://harfbuzz.github.io/harfbuzz-hb-common.html#hb-language-get-default
55105
pub fn get_process_default() -> Self {
56106
Language {
57107
raw: unsafe { sys::hb_language_get_default() },
58108
}
59109
}
60110

111+
/// Returns whether or not the language is valid.
112+
///
113+
/// TODO: This should go away and the constructor should
114+
/// return an `Option<Language>`.
61115
pub fn is_valid(self) -> bool {
62116
!self.raw.is_null()
63117
}

0 commit comments

Comments
 (0)