| 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 | [`DataMarker`], which is the type-system-level data identifier. [`DataProvider`] has a single method, |
| 16 | [`DataProvider::load`], which transforms a [`DataRequest`] into a [`DataResponse`]. |
| 17 | |
| 18 | - [`DataRequest`] contains selectors to choose a specific variant of the marker, such as a locale. |
| 19 | - [`DataResponse`] contains the data if the request was successful. |
| 20 | |
| 21 | The most common types required for this crate are included via the prelude: |
| 22 | |
| 23 | ```rust |
| 24 | use icu_provider::prelude::*; |
| 25 | ``` |
| 26 | |
| 27 | ### Dynamic Data Providers |
| 28 | |
| 29 | If the type system cannot be leveraged to load data (such as when dynamically loading from I/O), |
| 30 | there's another form of the [`DataProvider`]: [`DynamicDataProvider`]. While [`DataProvider`] is parametrized |
| 31 | on the type-system level by a [`DataMarker`] (which are distinct types implementing this trait), |
| 32 | [`DynamicDataProvider`]s are parametrized at runtime by a [`DataMarkerInfo`] struct, which essentially is the runtime |
| 33 | representation of the [`DataMarker`] type. |
| 34 | |
| 35 | The [`DynamicDataProvider`] is still type-level parametrized by the type that it loads, and there are two |
| 36 | implementations that should be called out |
| 37 | |
| 38 | - [`DynamicDataProvider<BufferMarker>`], a.k.a. [`BufferProvider`](buf::BufferProvider) returns data as `[u8]` buffers. |
| 39 | |
| 40 | #### BufferProvider |
| 41 | |
| 42 | These providers are able to return unstructured data typically represented as |
| 43 | [`serde`]-serialized buffers. Users can call [`as_deserializing()`] to get an object |
| 44 | implementing [`DataProvider`] by invoking Serde Deserialize. |
| 45 | |
| 46 | Examples of BufferProviders: |
| 47 | |
| 48 | - [`FsDataProvider`] reads individual buffers from the filesystem. |
| 49 | - [`BlobDataProvider`] reads buffers from a large in-memory blob. |
| 50 | |
| 51 | ### Provider Adapters |
| 52 | |
| 53 | ICU4X offers several built-in modules to combine providers in interesting ways. |
| 54 | These can be found in the [`icu_provider_adapters`] crate. |
| 55 | |
| 56 | ### Testing Provider |
| 57 | |
| 58 | This crate also contains a concrete provider for demonstration purposes: |
| 59 | |
| 60 | - [`HelloWorldProvider`] returns "hello world" strings in several languages. |
| 61 | |
| 62 | ### Types and Lifetimes |
| 63 | |
| 64 | Types compatible with [`Yokeable`] can be passed through the data provider, so long as they are |
| 65 | associated with a marker type implementing [`DynamicDataMarker`]. |
| 66 | |
| 67 | Data structs should generally have one lifetime argument: `'data`. This lifetime allows data |
| 68 | structs to borrow zero-copy data. |
| 69 | |
| 70 | [`FixedProvider`]: https://docs.rs/icu_provider_adapters/latest/fixed/any_payload/struct.FixedProvider.html |
| 71 | [`HelloWorldProvider`]: hello_world::HelloWorldProvider |
| 72 | [`Yokeable`]: yoke::Yokeable |
| 73 | [`impl_dynamic_data_provider!`]: dynutil::impl_dynamic_data_provider |
| 74 | [`icu_provider_adapters`]: https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/index.html |
| 75 | [`SourceDataProvider`]: https://docs.rs/icu_provider_source/latest/icu_provider_source/struct.SourceDataProvider.html |
| 76 | [`as_deserializing()`]: buf::AsDeserializingBufferProvider::as_deserializing |
| 77 | [`FsDataProvider`]: https://docs.rs/icu_provider_fs/latest/icu_provider_fs/struct.FsDataProvider.html |
| 78 | [`BlobDataProvider`]: https://docs.rs/icu_provider_blob/latest/icu_provider_blob/struct.BlobDataProvider.html |
| 79 | |
| 80 | <!-- cargo-rdme end --> |
| 81 | |
| 82 | ## More Information |
| 83 | |
| 84 | For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |