1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! `icu_testdata` is a unit testing crate for [`ICU4X`].
//!
//! The crate exposes data providers with stable data useful for unit testing. The data is
//! based on a CLDR tag and a short list of locales that, together, cover a range of scenarios.
//!
//! The crate exposes three kinds of providers, corresponding to the three types of constructors
//! in ICU:
//! * [`unstable`], [`unstable_no_fallback`]
//! * [`any`], [`any_no_fallback`]
//! * [`buffer`], [`buffer_no_fallback`] (`buffer` Cargo feature)
//!
//!
//! Additionally, the `metadata` Cargo feature exposes the [`metadata`] module which contains
//! information such as the CLDR Gitref  and the list of included locales.
//!
//! # `bin` Cargo feature
//!
//! ## Downloading fresh CLDR data
//!
//! ```bash
//! $ cargo run --bin --features=bin icu4x-testdata-download-sources
//! ```
//!
//! ## Regenerating data
//!
//! ```bash
//! $ cargo run --bin --features=bin icu4x-testdata-datagen
//! ```
//!
//! # Examples
//!
//! ```
//! use icu::locid::locale;
//! use icu_provider::hello_world::*;
//! use icu_provider::prelude::*;
//!
//! let req = DataRequest {
//!     locale: &locale!("en").into(),
//!     metadata: Default::default(),
//! };
//!
//! assert_eq!(
//!     DataProvider::<HelloWorldV1Marker>::load(
//!         &icu_testdata::unstable(),
//!         req
//!     )
//!     .and_then(DataResponse::take_payload)
//!     .unwrap()
//!     .get()
//!     .message,
//!     "Hello World"
//! );
//!
//! assert_eq!(
//!     BufferProvider::load_buffer(
//!         &icu_testdata::buffer(),
//!         HelloWorldV1Marker::KEY,
//!         req
//!     )
//!     .and_then(DataResponse::take_payload)
//!     .unwrap()
//!     .get(),
//!     &b"\x0bHello World"
//! );
//!
//! assert_eq!(
//!     AnyProvider::load_any(
//!         &icu_testdata::any(),
//!         HelloWorldV1Marker::KEY,
//!         req
//!     )
//!     .and_then(AnyResponse::downcast::<HelloWorldV1Marker>)
//!     .and_then(DataResponse::take_payload)
//!     .unwrap()
//!     .get()
//!     .message,
//!     "Hello World"
//! );
//! ```
//!
//! [`ICU4X`]: ../icu/index.html

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(
    not(test),
    deny(
        clippy::indexing_slicing,
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::exhaustive_structs,
        clippy::exhaustive_enums
    )
)]
#![warn(missing_docs)]
#![allow(unused_imports)] // too many feature combinations too keep track of

extern crate alloc;

// If you want this to be made public stable, file an issue on the ICU4X repository.
#[cfg(feature = "metadata")]
#[doc(hidden)]
pub mod metadata;

#[cfg(feature = "metadata")]
pub mod versions {
    //! Functions to access version info of the ICU test data.

    /// Gets the CLDR tag used as the test data source (for formatters, likely subtags, ...)
    ///
    /// Enabled with the "metadata" Cargo feature.
    ///
    /// # Panics
    ///
    /// Panics if the package metadata cannot be loaded.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!("42.0.0", icu_testdata::versions::cldr_tag());
    /// ```
    #[allow(clippy::unwrap_used)] // documented
    pub fn cldr_tag() -> String {
        crate::metadata::load()
            .unwrap()
            .package_metadata
            .cldr_json_gitref
    }

    /// Gets the ICU tag used as the test data source (for properties, collator, ...)
    ///
    /// Enabled with the "metadata" Cargo feature.
    ///
    /// # Panics
    ///
    /// Panics if the package metadata cannot be loaded.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!("release-72-1", icu_testdata::versions::icu_tag());
    /// ```
    #[allow(clippy::unwrap_used)] // documented
    pub fn icu_tag() -> String {
        crate::metadata::load()
            .unwrap()
            .package_metadata
            .icuexportdata_gitref
    }
}

#[cfg(feature = "std")]
pub mod paths;

use icu_provider::prelude::*;
use icu_provider_adapters::fallback::LocaleFallbackProvider;

/// A data provider that is compatible with all ICU `_unstable` constructors.
///
/// The return type of this method is not considered stable, mirroring the unstable trait
/// bounds of the constructors. For matching versions of `icu` and `icu_testdata`, however,
/// these are guaranteed to match.
#[cfg(any(feature = "all_features_hack", not(feature = "bin")))] // allow regenerating testdata even if databake doesn't compile
pub fn unstable() -> LocaleFallbackProvider<UnstableDataProvider> {
    // The statically compiled data file is valid.
    #[allow(clippy::unwrap_used)]
    LocaleFallbackProvider::try_new_unstable(unstable_no_fallback()).unwrap()
}

/// A data provider that is compatible with all ICU `_unstable` constructors.
///
/// The return type of this method is not considered stable, mirroring the unstable trait
/// bounds of the constructors. For matching versions of `icu` and `icu_testdata`, however,
/// these are guaranteed to match.
pub fn unstable_no_fallback() -> UnstableDataProvider {
    UnstableDataProvider
}

/// An [`AnyProvider`] backed by baked data.
#[cfg(any(feature = "all_features_hack", not(feature = "bin")))] // allow regenerating testdata even if databake doesn't compile
pub fn any() -> impl AnyProvider {
    // The baked data is valid.
    #[allow(clippy::unwrap_used)]
    LocaleFallbackProvider::try_new_with_any_provider(any_no_fallback()).unwrap()
}

/// An [`AnyProvider`] backed by baked data.
#[cfg(any(feature = "all_features_hack", not(feature = "bin")))] // allow regenerating testdata even if databake doesn't compile
pub fn any_no_fallback() -> impl AnyProvider {
    UnstableDataProvider
}

/// A [`BufferProvider`] backed by a Postcard blob.
#[cfg(feature = "buffer")]
pub fn buffer() -> impl BufferProvider {
    // The statically compiled data file is valid.
    #[allow(clippy::unwrap_used)]
    LocaleFallbackProvider::try_new_with_buffer_provider(buffer_no_fallback()).unwrap()
}

/// A [`BufferProvider`] backed by a Postcard blob.
#[cfg(feature = "buffer")]
pub fn buffer_no_fallback() -> impl BufferProvider {
    lazy_static::lazy_static! {
        static ref POSTCARD: icu_provider_blob::BlobDataProvider = {
            // The statically compiled data file is valid.
            #[allow(clippy::unwrap_used)]
            icu_provider_blob::BlobDataProvider::try_new_from_static_blob(include_bytes!(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/data/testdata.postcard"
            )))
            .unwrap()
        };
    }
    POSTCARD.clone()
}

#[doc(hidden)]
#[non_exhaustive]
pub struct UnstableDataProvider;

#[cfg(any(feature = "all_features_hack", not(feature = "bin")))] // allow regenerating testdata even if databake doesn't compile
mod baked {
    include!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/baked/mod.rs"));
    impl_data_provider!(super::UnstableDataProvider);
    impl_any_provider!(super::UnstableDataProvider);
}