Skip to content

Commit

Permalink
feat: Implement uloc_openAvailableByType()
Browse files Browse the repository at this point in the history
- Change the signature of ULoc::get_available() to more closely match the ICU4C uloc_getAvailable() function signature.
- Add the function ULoc::get_available_locales() to return an (uncached) collection of available locales.
- Add the function ULoc::get_available_locales_by_type() to return a collection of available locales for the specified type.
- Correct some configuration tags that used the plural 'features' incorrectly and were not functioning correctly as a result.
  • Loading branch information
clydegerber authored and filmil committed Nov 13, 2023
1 parent 54d771c commit 1858980
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rust_icu_uloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ rust_icu_common = { path = "../rust_icu_common", version = "4.2.3", default-feat
rust_icu_sys = { path = "../rust_icu_sys", version = "4.2.3", default-features = false }
rust_icu_uenum = { path = "../rust_icu_uenum", version = "4.2.3", default-features = false }
rust_icu_ustring = { path = "../rust_icu_ustring", version = "4.2.3", default-features = false }
anyhow = "1.0.25"

[dev-dependencies]
anyhow = "1.0.25"

# See the feature description in ../rust_icu_sys/Cargo.toml for details.
[features]
Expand Down
13 changes: 7 additions & 6 deletions rust_icu_uloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use {
anyhow::anyhow,
rust_icu_common as common,
rust_icu_common::buffered_string_method_with_retry,
rust_icu_sys as sys,
Expand Down Expand Up @@ -370,10 +371,10 @@ impl ULoc {
/// Implements `uloc_getAvailable`.
pub fn get_available(n: i32) -> Result<ULoc, common::Error> {
if (0 > n) || (n >= Self::count_available()) {
panic!("{n} is negative or greater than or equal to the value returned from count_available()");
panic!("{} is negative or greater than or equal to the value returned from count_available()", n);
}
let ptr = unsafe { versioned_function!(uloc_getAvailable)(n) };
if ptr == std::prt::null() {
if ptr == std::ptr::null() {
return Err(common::Error::Wrapper(anyhow!("uloc_getAvailable() returned a null pointer")));
}
let label = unsafe { ffi::CStr::from_ptr(ptr).to_str().unwrap() };
Expand All @@ -386,7 +387,7 @@ impl ULoc {
let mut vec = Vec::with_capacity(count as usize);
let mut index: i32 = 0;
while index < count {
let locale = get_available(index).unwrap();
let locale = Self::get_available(index).unwrap();
vec.push(locale);
index += 1;
}
Expand Down Expand Up @@ -1314,16 +1315,16 @@ mod tests {
}

#[test]
#[should_panic(expected = "n is negative or greater than or equal to the value returned from count_available()")]
#[should_panic(expected = "-1 is negative or greater than or equal to the value returned from count_available()")]
fn test_get_available_negative() {
let _ = ULoc::get_available(-1);
}

#[test]
#[should_panic(expected = "n is negative or greater than or equal to the value returned from count_available()")]
fn test_get_available_overrun() {
let index = ULoc::count_available();
let _ = ULoc::get_available(index);
let result = std::panic::catch_unwind(|| ULoc::get_available(index));
assert!(result.is_err());
}

#[test]
Expand Down

0 comments on commit 1858980

Please sign in to comment.