7
7
// option. This file may not be copied, modified, or distributed
8
8
// except according to those terms.
9
9
10
- #![ allow( missing_docs) ]
11
-
12
10
use crate :: sys;
13
11
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
14
21
#[ derive( Copy , Clone , PartialEq , PartialOrd ) ]
15
22
pub struct Language {
16
23
/// The underlying `hb_language_t` from the `harfbuzz-sys` crate.
@@ -22,6 +29,26 @@ pub struct Language {
22
29
}
23
30
24
31
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
25
52
pub fn from_string ( lang : & str ) -> Self {
26
53
Language {
27
54
raw : unsafe {
@@ -33,6 +60,13 @@ impl Language {
33
60
}
34
61
}
35
62
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
+ /// ```
36
70
pub fn to_string ( & self ) -> & str {
37
71
unsafe { std:: ffi:: CStr :: from_ptr ( sys:: hb_language_to_string ( self . raw ) ) }
38
72
. to_str ( )
@@ -48,16 +82,36 @@ impl Language {
48
82
Language { raw }
49
83
}
50
84
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.
51
89
pub fn as_raw ( self ) -> sys:: hb_language_t {
52
90
self . raw
53
91
}
54
92
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
55
105
pub fn get_process_default ( ) -> Self {
56
106
Language {
57
107
raw : unsafe { sys:: hb_language_get_default ( ) } ,
58
108
}
59
109
}
60
110
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>`.
61
115
pub fn is_valid ( self ) -> bool {
62
116
!self . raw . is_null ( )
63
117
}
0 commit comments