|
5 | 5 | //! # env_preferences
|
6 | 6 | //!
|
7 | 7 | //! `env_preferences` is a crate to retrieve system locale and preferences for
|
8 |
| -//! Apple, Linux & Windows systems |
| 8 | +//! Apple, Linux & Windows systems. |
9 | 9 | //!
|
10 |
| -//! It currently fetches locales for the operating system |
11 |
| -//! currently in `String` format. |
12 |
| -//! |
13 |
| -//! In the current setup, it is not ensured that the locale retrieved will be |
14 |
| -//! converted to [`ICU4X Locale`](https://crates.io/crates/icu_locale) |
| 10 | +//! It provides functionality to fetch preferred locales from the user's operating |
| 11 | +//! system and parse them lossily to an ICU4X [`Locale`](icu_locale_core::Locale). |
15 | 12 | //!
|
16 | 13 | //! It also retrieves preferences for [`Calendar`](https://crates.io/crates/icu_calendar)
|
17 | 14 | //! & [`TimeZone`](https://crates.io/crates/icu_time)
|
18 | 15 |
|
19 | 16 | mod error;
|
20 |
| -pub use error::RetrievalError; |
| 17 | +pub mod parse; |
21 | 18 |
|
22 |
| -#[cfg(target_os = "linux")] |
23 |
| -mod linux; |
24 |
| -#[cfg(target_os = "linux")] |
25 |
| -pub use linux::*; |
26 |
| -#[cfg(target_os = "macos")] |
27 |
| -mod apple; |
28 |
| -#[cfg(target_os = "macos")] |
29 |
| -pub use apple::*; |
30 |
| -#[cfg(target_os = "windows")] |
31 |
| -mod windows; |
32 |
| -#[cfg(target_os = "windows")] |
33 |
| -pub use windows::*; |
| 19 | +pub use error::{LocaleError, ParseError, RetrievalError}; |
| 20 | + |
| 21 | +#[cfg(any(doc, target_os = "macos"))] |
| 22 | +pub mod apple; |
| 23 | +#[cfg(any(doc, target_os = "linux"))] |
| 24 | +pub mod posix; |
| 25 | +#[cfg(any(doc, target_os = "windows"))] |
| 26 | +pub mod windows; |
34 | 27 | #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
|
35 | 28 | compile_error!(
|
36 | 29 | "Unsupported target OS. Supported operating systems are Apple, Linux & Windows as of now"
|
37 | 30 | );
|
| 31 | + |
| 32 | +#[cfg(target_os = "macos")] |
| 33 | +use apple as system; |
| 34 | +#[cfg(target_os = "linux")] |
| 35 | +use posix as system; |
| 36 | +#[cfg(target_os = "windows")] |
| 37 | +use windows as system; |
| 38 | + |
| 39 | +#[cfg(target_os = "macos")] |
| 40 | +use parse::apple::AppleLocale as SystemLocale; |
| 41 | +#[cfg(target_os = "linux")] |
| 42 | +use parse::posix::PosixLocale as SystemLocale; |
| 43 | +#[cfg(target_os = "windows")] |
| 44 | +use parse::windows::WindowsLocale as SystemLocale; |
| 45 | + |
| 46 | +/// List the user's available locales as the platform-provided [`String`]s, ordered by preference. |
| 47 | +/// |
| 48 | +/// <div class="warning"> |
| 49 | +/// |
| 50 | +/// The output of this function is platform-dependent and **is not guaranteed** to be a valid |
| 51 | +/// BCP-47 identifier. To get a list of parsed locales, see [`get_locales_lossy()`]. |
| 52 | +/// |
| 53 | +/// </div> |
| 54 | +/// |
| 55 | +/// Specific information can be found at the platform's implementation: |
| 56 | +/// - [`apple::get_raw_locales()`] |
| 57 | +/// - [`posix::get_raw_locales()`] |
| 58 | +/// - [`windows::get_raw_locales()`] |
| 59 | +pub fn get_raw_locales() -> Result<Vec<String>, RetrievalError> { |
| 60 | + system::get_raw_locales() |
| 61 | +} |
| 62 | + |
| 63 | +/// List the user's available locales as ICU4X [`Locale`](icu_locale_core::Locale)s, ordered by preference. |
| 64 | +/// |
| 65 | +/// This performs a best-effort conversion that may lose some (or all!) data in certain cases. |
| 66 | +/// For getting a list of raw system locales, see [`get_raw_locales()`]. |
| 67 | +/// |
| 68 | +/// Specific information can be found at the platform's implementation: |
| 69 | +/// - [`parse::apple::AppleLocale`] |
| 70 | +/// - [`parse::posix::PosixLocale`] |
| 71 | +/// - [`parse::windows::WindowsLocale`] |
| 72 | +pub fn get_locales_lossy() -> Result<Vec<icu_locale_core::Locale>, LocaleError> { |
| 73 | + let raw_locales = get_raw_locales()?; |
| 74 | + let system_locales = raw_locales |
| 75 | + .iter() |
| 76 | + .map(String::as_str) |
| 77 | + .map(SystemLocale::try_from_str) |
| 78 | + .collect::<Result<Vec<SystemLocale>, ParseError>>()?; |
| 79 | + |
| 80 | + system_locales |
| 81 | + .iter() |
| 82 | + .map(SystemLocale::try_convert_lossy) |
| 83 | + .map(|result| result.map_err(LocaleError::from)) |
| 84 | + .collect::<Result<Vec<icu_locale_core::Locale>, LocaleError>>() |
| 85 | +} |
0 commit comments