| 1 |
# icu_provider [](https://crates.io/crates/icu_provider) |
| 2 |
|
| 3 |
<!-- cargo-rdme start --> |
| 4 |
|
| 5 |
`icu_provider` is one of the [`ICU4X`] components. |
| 6 |
|
| 7 |
Unicode's experience with ICU4X's parent projects, ICU4C and ICU4J, led the team to realize |
| 8 |
that data management is the most critical aspect of deploying internationalization, and that it requires |
| 9 |
a high level of customization for the needs of the platform it is embedded in. As a result |
| 10 |
ICU4X comes with a selection of providers that should allow for ICU4X to naturally fit into |
| 11 |
different business and technological needs of customers. |
| 12 |
|
| 13 |
`icu_provider` defines traits and structs for transmitting data through the ICU4X locale |
| 14 |
data pipeline. The primary trait is [`DataProvider`]. It is parameterized by a |
| 15 |
[`KeyedDataMarker`], which contains the data type and a [`DataKey`]. It has one method, |
| 16 |
[`DataProvider::load`], which transforms a [`DataRequest`] |
| 17 |
into a [`DataResponse`]. |
| 18 |
|
| 19 |
- [`DataKey`] is a fixed identifier for the data type, such as `"plurals/cardinal@1"`. |
| 20 |
- [`DataRequest`] contains additional annotations to choose a specific variant of the key, |
| 21 |
such as a locale. |
| 22 |
- [`DataResponse`] contains the data if the request was successful. |
| 23 |
|
| 24 |
In addition, there are three other traits which are widely implemented: |
| 25 |
|
| 26 |
- [`AnyProvider`] returns data as `dyn Any` trait objects. |
| 27 |
- [`BufferProvider`] returns data as `[u8]` buffers. |
| 28 |
- [`DynamicDataProvider`] returns structured data but is not specific to a key. |
| 29 |
|
| 30 |
The most common types required for this crate are included via the prelude: |
| 31 |
|
| 32 |
```rust |
| 33 |
use icu_provider::prelude::*; |
| 34 |
``` |
| 35 |
|
| 36 |
### Types of Data Providers |
| 37 |
|
| 38 |
All nontrivial data providers can fit into one of two classes. |
| 39 |
|
| 40 |
1. [`AnyProvider`]: Those whose data originates as structured Rust objects |
| 41 |
2. [`BufferProvider`]: Those whose data originates as unstructured `[u8]` buffers |
| 42 |
|
| 43 |
**✨ Key Insight:** A given data provider is generally *either* an [`AnyProvider`] *or* a |
| 44 |
[`BufferProvider`]. Which type depends on the data source, and it is not generally possible |
| 45 |
to convert one to the other. |
| 46 |
|
| 47 |
See also [crate::constructors]. |
| 48 |
|
| 49 |
#### AnyProvider |
| 50 |
|
| 51 |
These providers are able to return structured data cast into `dyn Any` trait objects. Users |
| 52 |
can call [`as_downcasting()`] to get an object implementing [`DataProvider`] by downcasting |
| 53 |
the trait objects. |
| 54 |
|
| 55 |
Examples of AnyProviders: |
| 56 |
|
| 57 |
- [`DatagenProvider`] reads structured data from CLDR source files and returns ICU4X data structs. |
| 58 |
- [`AnyPayloadProvider`] wraps a specific data struct and returns it. |
| 59 |
- The `BakedDataProvider` which encodes structured data directly in Rust source |
| 60 |
|
| 61 |
#### BufferProvider |
| 62 |
|
| 63 |
These providers are able to return unstructured data typically represented as |
| 64 |
[`serde`]-serialized buffers. Users can call [`as_deserializing()`] to get an object |
| 65 |
implementing [`DataProvider`] by invoking Serde Deserialize. |
| 66 |
|
| 67 |
Examples of BufferProviders: |
| 68 |
|
| 69 |
- [`FsDataProvider`] reads individual buffers from the filesystem. |
| 70 |
- [`BlobDataProvider`] reads buffers from a large in-memory blob. |
| 71 |
|
| 72 |
### Provider Adapters |
| 73 |
|
| 74 |
ICU4X offers several built-in modules to combine providers in interesting ways. |
| 75 |
These can be found in the [`icu_provider_adapters`] crate. |
| 76 |
|
| 77 |
### Testing Provider |
| 78 |
|
| 79 |
This crate also contains a concrete provider for demonstration purposes: |
| 80 |
|
| 81 |
- [`HelloWorldProvider`] returns "hello world" strings in several languages. |
| 82 |
|
| 83 |
### Types and Lifetimes |
| 84 |
|
| 85 |
Types compatible with [`Yokeable`] can be passed through the data provider, so long as they are |
| 86 |
associated with a marker type implementing [`DataMarker`]. |
| 87 |
|
| 88 |
Data structs should generally have one lifetime argument: `'data`. This lifetime allows data |
| 89 |
structs to borrow zero-copy data. |
| 90 |
|
| 91 |
### Data generation API |
| 92 |
|
| 93 |
*This functionality is enabled with the "datagen" Cargo feature* |
| 94 |
|
| 95 |
The [`datagen`] module contains several APIs for data generation. See [`icu_datagen`] for the reference |
| 96 |
data generation implementation. |
| 97 |
|
| 98 |
[`ICU4X`]: ../icu/index.html |
| 99 |
[`DataProvider`]: data_provider::DataProvider |
| 100 |
[`DataKey`]: key::DataKey |
| 101 |
[`DataLocale`]: request::DataLocale |
| 102 |
[`IterableDynamicDataProvider`]: datagen::IterableDynamicDataProvider |
| 103 |
[`IterableDataProvider`]: datagen::IterableDataProvider |
| 104 |
[`AnyPayloadProvider`]: ../icu_provider_adapters/any_payload/struct.AnyPayloadProvider.html |
| 105 |
[`HelloWorldProvider`]: hello_world::HelloWorldProvider |
| 106 |
[`AnyProvider`]: any::AnyProvider |
| 107 |
[`Yokeable`]: yoke::Yokeable |
| 108 |
[`impl_dynamic_data_provider!`]: impl_dynamic_data_provider |
| 109 |
[`icu_provider_adapters`]: ../icu_provider_adapters/index.html |
| 110 |
[`DatagenProvider`]: ../icu_datagen/struct.DatagenProvider.html |
| 111 |
[`as_downcasting()`]: AsDowncastingAnyProvider::as_downcasting |
| 112 |
[`as_deserializing()`]: AsDeserializingBufferProvider::as_deserializing |
| 113 |
[`CldrJsonDataProvider`]: ../icu_datagen/cldr/struct.CldrJsonDataProvider.html |
| 114 |
[`FsDataProvider`]: ../icu_provider_fs/struct.FsDataProvider.html |
| 115 |
[`BlobDataProvider`]: ../icu_provider_blob/struct.BlobDataProvider.html |
| 116 |
[`icu_datagen`]: ../icu_datagen/index.html |
| 117 |
|
| 118 |
<!-- cargo-rdme end --> |
| 119 |
|
| 120 |
## More Information |
| 121 |
|
| 122 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |