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