Skip to main content

icu/
lib.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6#![cfg_attr(not(any(test, doc)), no_std)]
7#![cfg_attr(
8    not(test),
9    deny(
10        clippy::indexing_slicing,
11        clippy::unwrap_used,
12        clippy::expect_used,
13        clippy::panic,
14    )
15)]
16#![warn(missing_docs)]
17
18//! `icu` is the main meta-crate of the `ICU4X` project.
19//!
20//! It provides a comprehensive selection of functionality found in
21//! [International Components for Unicode](https://icu.unicode.org/)
22//! in their canonical configurations intended to enable software
23//! internationalization capabilities.
24//!
25//! This crate exists to collect the most important functionality for users
26//! together in one place.
27//! It does not bring any unique functionality, but rather,
28//! it re-exports the relevant crates as modules.
29//! The exported crate corresponding to each module is also
30//! available in a stand-alone manner, i.e. `icu::list` as `icu::list`.
31//!
32//! # Data Management
33//!
34//! Most internationalization algorithms are data-driven based on surveys of locale experts.
35//! ICU4X offers multiple ways to manage locale data: many clients can start by using the
36//! extensive data compiled into the library, while users with additional requirements can
37//! provide data explicitly using [`DataProvider`]s.
38//!
39//! ## Compiled data
40//!
41//! Compiled data is exposed through idiomatic Rust constructors like `new` or `try_new`:
42//!
43//! ```
44//! use icu::datetime::{fieldsets::YMD, DateTimeFormatter};
45//! use icu::locale::locale;
46//!
47//! let dtf =
48//!     DateTimeFormatter::try_new(locale!("es-US").into(), YMD::medium())
49//!         .expect("compiled data should include 'es-US'");
50//! ```
51//!
52//! Clients using compiled data benefit from simple code and optimal zero-cost data loading. Additionally,
53//! ICU4X's APIs are designed such that dead-code elimination can optimize away unused compiled data.
54//!
55//! By default, most of the data available in [CLDR] is included. Users can customize data by using
56//! the `icu4x-datagen` tool (with the `-⁠-format mod` flag) to, for example, select a smaller set of
57//! locales, and then compiling with the `ICU4X_DATA_DIR` variable.
58//!
59//! ## Explicit data
60//!
61//! Powerful data management is possible with [`DataProvider`]s, which are passed to ICU4X APIs via
62//! special constructors:
63//!
64//! ```no_run
65//! use icu::datetime::{fieldsets::YMD, DateTimeFormatter};
66//! use icu::locale::fallback::LocaleFallbacker;
67//! use icu::locale::locale;
68//! use icu_provider_adapters::fallback::LocaleFallbackProvider;
69//! use icu_provider_blob::BlobDataProvider;
70//!
71//! let data: Box<[u8]> = todo!();
72//!
73//! let provider = BlobDataProvider::try_new_from_blob(data)
74//!     .expect("data should be valid");
75//!
76//! let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&provider)
77//!     .expect("provider should include fallback data");
78//!
79//! let provider = LocaleFallbackProvider::new(provider, fallbacker);
80//!
81//! let dtf = DateTimeFormatter::try_new_with_buffer_provider(
82//!     &provider,
83//!     locale!("es-US").into(),
84//!     YMD::medium(),
85//! )
86//! .expect("data should include 'es-US', 'es', or 'und'");
87//! ```
88//!
89//! Explicit data management can be used if the compiled-data constructors are too limiting. It allows:
90//! * Accessing data without fallback
91//! * Custom [`DataProvider`]s backed by sources like the operating system
92//! * Lazily loading or updating data from I/O
93//! * Composing data providers from different sources
94//! * Manually including/excluding data
95//! * ... and more. See our [data management tutorial]
96//!
97//! The following [`DataProvider`]s are available in separate crates:
98//! * [`BlobDataProvider`]: deserializes data from an in-memory blob, which can be updated at runtime.
99//! * `BakedDataProvider`: a code-generated provider that contains the data directly in Rust code. This is the
100//!   same provider that is used internally by compiled data.
101//! * [`FsDataProvider`]: uses a file system tree of Serde files. This is mostly useful for development and
102//!   not recommended in production for performance reasons.
103//! * [`icu_provider_adapters`]: this crate contains provider adapters to combine providers,
104//!   provide additional functionality such as locale fallback, and more.
105//!
106//! The data that is required by these providers (in `BakedDataProvider`'s case, the provider itself) can be
107//! generated and customized using the [`icu4x-datagen`] tool.
108//!
109//! # Features
110//!
111//! ICU4X components share a set of common Cargo features that control whether core pieces of
112//! functionality are compiled. These features are:
113//!
114//! - `compiled_data` (default): Whether to include compiled data. Without this flag, only constructors with
115//!   explicit `provider` arguments are available.
116//! - `datagen`: Whether to implement functionality that is only required during data generation.
117//! - `logging`: Enables logging through the `log` crate.
118//! - `serde`: Activates `serde` implementations for core library types, such as [`Locale`], as well
119//!   as `*_with_buffer_provider` constructors for runtime data management.
120//! - `sync`: makes most ICU4X objects implement `Send + Sync`. Has a small performance impact when used with runtime data.
121//!
122//! # Experimental modules
123//!
124//! Experimental, unstable functionality can be found in the `icu::experimental` crate. The modules in that crate
125//! are on track to be eventually stabilized into this crate.
126//!
127//!
128//! [CLDR]: https://cldr.unicode.org/
129//! [`DataProvider`]: icu_provider::DataProvider
130//! [`FsDataProvider`]: https://docs.rs/icu_provider_fs/latest/icu_provider_fs/struct.FsDataProvider.html
131//! [`BlobDataProvider`]: https://docs.rs/icu_provider_blob/latest/icu_provider_blob/struct.BlobDataProvider.html
132//! [`icu_provider_adapters`]: https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/
133//! [`icu4x-datagen`]: https://crates.io/crates/icu4x-datagen
134//! [`Locale`]: crate::locale::Locale
135//! [data management tutorial]: https://github.com/unicode-org/icu4x/blob/main/tutorials/data-provider-runtime.md#loading-additional-data-at-runtime
136
137// Needed for intra-doc link to work, since icu_provider is otherwise never mentioned in this crate
138use icu_provider as _;
139
140#[doc(inline)]
141pub use icu_calendar as calendar;
142
143#[doc(inline)]
144pub use icu_casemap as casemap;
145
146#[doc(inline)]
147pub use icu_collator as collator;
148
149#[doc(inline)]
150pub use icu_datetime as datetime;
151
152#[doc(inline)]
153pub use icu_decimal as decimal;
154
155#[doc(inline)]
156pub use icu_list as list;
157
158#[doc(inline)]
159pub use icu_locale as locale;
160
161#[doc(inline)]
162pub use icu_normalizer as normalizer;
163
164#[doc(inline)]
165pub use icu_plurals as plurals;
166
167#[doc(inline)]
168pub use icu_properties as properties;
169
170#[doc(inline)]
171pub use icu_collections as collections;
172
173#[doc(inline)]
174pub use icu_segmenter as segmenter;
175
176#[doc(inline)]
177pub use icu_time as time;
178
179#[doc(inline)]
180#[cfg(feature = "unstable")]
181pub use icu_experimental as experimental;
182
183#[doc(inline)]
184#[cfg(feature = "unstable")]
185pub use icu_pattern as pattern;
186
187#[cfg(feature = "datagen")]
188mod datagen;
189#[cfg(feature = "datagen")]
190pub use datagen::*;