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
// 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)
//!
//! # Examples
//!
//! ```
//! use icu::locid::locale;
//! use icu_provider::hello_world::HelloWorldFormatter;
//!
//! // Unstable constructor
//! HelloWorldFormatter::try_new_unstable(
//!     &icu_testdata::unstable(),
//!     &locale!("en-CH").into(),
//! ).unwrap();
//!
//! // AnyProvider constructor
//! HelloWorldFormatter::try_new_with_any_provider(
//!     &icu_testdata::any(),
//!     &locale!("en-CH").into(),
//! ).unwrap();
//!
//! // BufferProvider constructor (`icu` with `serde` feature, `icu_testdata` with `buffer` feature)
//! HelloWorldFormatter::try_new_with_buffer_provider(
//!     &icu_testdata::buffer(),
//!     &locale!("en-CH").into(),
//! ).unwrap();
//!
//! // Without fallback the locale match needs to be exact
//! HelloWorldFormatter::try_new_unstable(
//!     &icu_testdata::unstable_no_fallback(),
//!     &locale!("en-CH").into(),
//! ).is_err();
//!
//! HelloWorldFormatter::try_new_unstable(
//!     &icu_testdata::unstable_no_fallback(),
//!     &locale!("en").into(),
//! ).unwrap();
//! ```
//!
//! [`ICU4X`]: https://docs.rs/icu/latest/icu/

// 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,
        missing_debug_implementations,
    )
)]
#![warn(missing_docs)]
#![allow(unused_imports)] // too many feature combinations too keep track of
#![allow(deprecated)]
#![deprecated(since = "1.3.0", note = "this crate has been superseded by `ICU4X`'s `compiled_data` feature. Data for new components will not be added, and it will not be updated for `ICU4X` 2.0.")]

extern crate alloc;

#[path = "../data/metadata.rs.data"]
mod metadata;

#[deprecated(since = "1.3.0")]
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, ...)
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!("44.0.0", icu_testdata::versions::cldr_tag());
    /// ```
    #[deprecated(since = "1.3.0")]
    pub fn cldr_tag() -> alloc::string::String {
        alloc::string::String::from(super::metadata::CLDR_TAG)
    }

    /// Gets the ICU tag used as the test data source (for properties, collator, ...)
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!("release-74-1", icu_testdata::versions::icu_tag());
    /// ```
    #[deprecated(since = "1.3.0")]
    pub fn icu_tag() -> alloc::string::String {
        alloc::string::String::from(super::metadata::ICUEXPORT_TAG)
    }
}

/// Gets the locales supported by the test data.
///
/// # Examples
///
/// ```
/// # use icu_locid::langid;
/// assert!(icu_testdata::locales().contains(&langid!("es-AR")));
/// assert!(icu_testdata::locales().len() > 10);
/// ```
#[deprecated(since = "1.3.0")]
pub fn locales() -> alloc::vec::Vec<icu_locid::LanguageIdentifier> {
    alloc::vec::Vec::from(metadata::LOCALES)
}

#[cfg(feature = "std")]
#[deprecated(since = "1.3.0")]
/// Get paths to the test data directories. Some of these paths do not
/// exist anymore, and data should only be accessed through the functions
/// provided by this crate.
pub mod paths {
    use std::path::PathBuf;

    #[deprecated(since = "1.3.0")]
    /// Returns the absolute path to the top-level data directory.
    pub fn data_root() -> PathBuf {
        PathBuf::from(std::env!("CARGO_MANIFEST_DIR")).join("data")
    }

    #[deprecated(since = "1.3.0")]
    /// Returns the absolute path to the CLDR JSON root directory.
    pub fn cldr_json_root() -> PathBuf {
        data_root().join("cldr")
    }

    #[deprecated(since = "1.3.0")]
    /// Returns the absolute path to the icuexport TOML root directory.
    pub fn icuexport_toml_root() -> PathBuf {
        data_root().join("icuexport")
    }

    #[deprecated(since = "1.3.0")]
    /// Returns the absolute path to the collation tailoring TOML root directory.
    pub fn coll_toml_root() -> PathBuf {
        data_root().join("coll")
    }
}

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(feature = "icu_locid_transform")]
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
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.
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
pub fn unstable_no_fallback() -> UnstableDataProvider {
    UnstableDataProvider
}

/// An [`AnyProvider`] backed by baked data.
#[cfg(feature = "icu_locid_transform")]
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
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.
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
pub fn any_no_fallback() -> impl AnyProvider {
    UnstableDataProvider
}

/// A [`BufferProvider`] backed by a Postcard blob.
///
/// This deserializes a large data blob from static memory, please cache the result if you
/// are calling this repeatedly and care about performance
#[cfg(feature = "buffer")]
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
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.
///
/// This deserializes a large data blob from static memory, please cache the result if you
/// are calling this repeatedly and care about performance
#[cfg(feature = "buffer")]
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
pub fn buffer_no_fallback() -> impl BufferProvider {
    #[allow(clippy::unwrap_used)] // The statically compiled data file is valid.
    icu_provider_blob::BlobDataProvider::try_new_from_static_blob(include_bytes!(
        "../data/testdata.postcard"
    ))
    .unwrap()
}

#[doc(hidden)]
#[non_exhaustive]
#[derive(Debug)]
#[deprecated(since = "1.3.0", note = "use `compiled_data`")]
pub struct UnstableDataProvider;

mod baked {
    include!("../data/baked/mod.rs");
    impl_data_provider!(super::UnstableDataProvider);
    impl_any_provider!(super::UnstableDataProvider);
}