forked from unicode-org/icu4x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
696 lines (624 loc) · 27.1 KB
/
main.rs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// 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 ).
//! The command line interface for ICU4X datagen.
//!
//! ```bash
//! $ cargo install icu4x-datagen
//! $ icu4x-datagen --markers all --locales de en-AU --format blob --out data.postcard
//! ```
//!
//! More details can be found by running `--help`, or by consulting the [`icu_provider_export`] documentation.
// If no exporter feature is enabled this all doesn't make sense
#![cfg_attr(
not(any(
feature = "baked_exporter",
feature = "blob_exporter",
feature = "fs_exporter",
)),
allow(unused_assignments, unreachable_code, unused_variables)
)]
// If no source feature is enabled this all doesn't make sense
#![cfg_attr(
not(any(feature = "provider", feature = "blob_input",)),
allow(unused_assignments, unreachable_code, unused_variables)
)]
use clap::{Parser, ValueEnum};
use eyre::WrapErr;
use icu_provider::export::ExportableProvider;
use icu_provider::hello_world::HelloWorldV1;
use icu_provider_export::prelude::*;
#[cfg(feature = "provider")]
use icu_provider_source::SourceDataProvider;
use simple_logger::SimpleLogger;
use std::collections::HashMap;
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "icu4x-datagen")]
#[command(author = "The ICU4X Project Developers", version = option_env!("CARGO_PKG_VERSION"))]
#[command(about = format!("Learn more at: https://docs.rs/icu_provider_export/{}", option_env!("CARGO_PKG_VERSION").unwrap_or("")), long_about = None)]
struct Cli {
#[arg(short, long)]
#[arg(help = "Requests verbose output")]
verbose: bool,
#[arg(long, value_enum)]
#[arg(
help = "Select the output format: a directory tree of files (fs), a single blob (blob), or a Rust module (baked)."
)]
format: Format,
#[arg(short = 'W', long)]
#[arg(help = "Delete the output before writing data.")]
overwrite: bool,
#[arg(short, long, value_enum, default_value_t = Syntax::Json)]
#[arg(help = "--format=fs only: serde serialization format.")]
syntax: Syntax,
#[arg(short, long)]
#[arg(help = "--format=baked, --format=fs only: pretty-print the Rust or JSON output files.")]
pretty: bool,
#[arg(short = 't', long, value_name = "TAG", default_value = "latest")]
#[arg(
help = "Download CLDR JSON data from this GitHub tag (https://github.com/unicode-org/cldr-json/tags)\n\
Use 'latest' for the latest version verified to work with this version of the binary.\n\
Ignored if '--cldr-root' is present. Requires binary to be built with `networking` Cargo feature (enabled by default).\n\
Note that some markers do not support versions before 41.0.0."
)]
#[cfg(feature = "provider")]
#[cfg_attr(not(feature = "networking"), arg(hide = true))]
cldr_tag: String,
#[arg(long, value_name = "PATH")]
#[arg(
help = "Path to a local cldr-{version}-json-full.zip directory (see https://github.com/unicode-org/cldr-json/releases).\n\
Note that some markers do not support versions before 41.0.0."
)]
#[cfg(feature = "provider")]
cldr_root: Option<PathBuf>,
#[arg(long, value_name = "TAG", default_value = "latest")]
#[arg(
help = "Download ICU data from this GitHub tag (https://github.com/unicode-org/icu/tags)\n\
Use 'latest' for the latest version verified to work with this version of the binary.\n\
Ignored if '--icuexport-root' is present. Requires binary to be built with `networking` Cargo feature (enabled by default).\n\
Note that some markers do not support versions before release-71-1."
)]
#[cfg_attr(not(feature = "networking"), arg(hide = true))]
#[cfg(feature = "provider")]
icuexport_tag: String,
#[arg(long, value_name = "PATH")]
#[arg(
help = "Path to a local icuexport directory (see https://github.com/unicode-org/icu/releases).\n\
Note that some markers do not support versions before release-71-1."
)]
#[cfg(feature = "provider")]
icuexport_root: Option<PathBuf>,
#[arg(long, value_name = "TAG", default_value = "latest")]
#[arg(
help = "Download segmentation LSTM models from this GitHub tag (https://github.com/unicode-org/lstm_word_segmentation/tags)\n\
Use 'latest' for the latest version verified to work with this version of the binary.\n\
Ignored if '--segmenter-lstm-root' is present. Requires binary to be built with `networking` Cargo feature (enabled by default)."
)]
#[cfg_attr(not(feature = "networking"), arg(hide = true))]
#[cfg(feature = "provider")]
segmenter_lstm_tag: String,
#[arg(long, value_name = "PATH")]
#[arg(
help = "Path to a local segmentation LSTM directory (see https://github.com/unicode-org/lstm_word_segmentation/releases)."
)]
#[cfg(feature = "provider")]
segmenter_lstm_root: Option<PathBuf>,
#[arg(long, value_name = "TAG", default_value = "latest")]
#[arg(
help = "Download tzdb from this IANA tag (https://data.iana.org/time-zones/releases/)\n\
Use 'latest' for the latest version verified to work with this version of the binary.\n\
Ignored if '--tzdb-root' is present. Requires binary to be built with `networking` Cargo feature (enabled by default)."
)]
#[cfg_attr(not(feature = "networking"), arg(hide = true))]
#[cfg(feature = "provider")]
tzdb_tag: String,
#[arg(long, value_name = "PATH")]
#[arg(help = "Path to a local tzdb directory \
(see any zip file from https://data.iana.org/time-zones/releases/, \
directory structure matching https://data.iana.org/time-zones/tzdb-2025a/).")]
#[cfg(feature = "provider")]
tzdb_root: Option<PathBuf>,
#[arg(long, value_enum, default_value_t = TrieType::Small)]
#[arg(
help = "Whether to optimize CodePointTrie data structures for size (\"small\") or speed (\"fast\").\n\
Using \"fast\" mode increases performance of CJK text processing and segmentation. For more\n\
information, see the TrieType enum."
)]
#[cfg(feature = "provider")]
trie_type: TrieType,
#[arg(long, value_enum, default_value_t = CollationRootHan::Implicit)]
#[arg(help = "Which collation han database to use.")]
#[cfg(feature = "provider")]
collation_root_han: CollationRootHan,
#[arg(long, value_enum, num_args = 1..)]
#[arg(
help = "Which less-common collation tables to include. 'search-all' includes all search tables."
)]
include_collations: Vec<CollationTable>,
#[arg(long, short, num_args = 1..)]
#[arg(
help = "Include these data markers in the output. Accepts multiple arguments.\n\
Set to 'all' for all markers, or 'none' for no markers."
)]
markers: Vec<String>,
#[arg(long, value_name = "BINARY")]
#[arg(help = "Analyzes the binary and only includes markers that are used by the binary.")]
markers_for_bin: Option<PathBuf>,
#[arg(long, short, num_args = 0..)]
#[cfg_attr(feature = "provider", arg(default_value = "recommended"))]
#[arg(
help = "Include this locale in the output. Accepts multiple arguments. \
Set to 'full' or 'modern' for the respective CLDR locale sets, 'none' for no locales, \
or 'recommended' for the recommended set of locales."
)]
locales: Vec<String>,
#[arg(long = "out", short, value_name = "PATH")]
#[arg(
help = "Path to output directory or file. Must be empty or non-existent, unless \
--overwrite is present, in which case the directory is deleted first. \
For --format=blob, omit this option to dump to stdout. \
For --format={dir,mod} defaults to 'icu4x_data'."
)]
output: Option<PathBuf>,
#[arg(long)]
#[arg(
help = "--format=baked only: use types from individual `icu_*` crates instead of the `icu` meta-crate."
)]
use_separate_crates: bool,
#[arg(long)]
#[arg(help = "--format=baked only: don't include fallback code inside the baked provider")]
no_internal_fallback: bool,
#[arg(long, value_enum)]
#[arg(
help = "configures the deduplication of locales for exported data payloads. \
If not set, determined by the export format: \
if --format=baked, a more aggressive deduplication strategy is used."
)]
deduplication: Option<Deduplication>,
#[arg(long, num_args = 0.., default_value = "recommended")]
#[arg(
help = "Include these segmenter models in the output. Accepts multiple arguments. \
Defaults to 'recommended' for the recommended set of models. Use 'none' for no models"
)]
segmenter_models: Vec<String>,
#[arg(long)]
#[arg(help = "Use data from this blob file instead of generating it from sources")]
#[cfg(feature = "blob_input")]
input_blob: Option<PathBuf>,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Format {
Fs,
Blob,
Baked,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Syntax {
Json,
Bincode,
Postcard,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum TrieType {
Small,
Fast,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
// Mirrors icu_provider_export::CollationRootHan
enum CollationRootHan {
Unihan,
Implicit,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum CollationTable {
Search,
Searchji,
#[value(alias = "search*")] // for backwards compatability
SearchAll,
}
impl CollationTable {
fn to_datagen_value(self) -> &'static str {
match self {
Self::Search => "search",
Self::Searchji => "searchji",
Self::SearchAll => "search*",
}
}
}
// Mirrors icu_provider_export::DeduplicationStrategy
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Deduplication {
Maximal,
RetainBaseLanguages,
None,
}
fn main() -> eyre::Result<()> {
let cli = Cli::parse();
if cli.verbose {
SimpleLogger::new()
.with_level(log::LevelFilter::Trace)
// wasmer logging is very noisy
.with_module_level("wasmer", log::LevelFilter::Warn)
.init()
.unwrap()
} else {
SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.env()
.init()
.unwrap()
}
let markers = if !cli.markers.is_empty() {
match cli.markers.as_slice() {
[x] if x == "none" => Default::default(),
[x] if x == "all" => {
#[cfg(feature = "experimental")]
log::info!("The icu4x-datagen crate has been built with the `experimental` feature, so `--markers all` includes experimental markers");
#[cfg(not(feature = "experimental"))]
log::info!("The icu4x-datagen crate has been built without the `experimental` feature, so `--markers all` does not include experimental markers");
all_markers()
}
markers => markers
.iter()
.map(|k| match marker_lookup().get(k.as_str()) {
Some(Some(marker)) => Ok(*marker),
Some(None) => {
eyre::bail!("Marker {k:?} requires `experimental` Cargo feature")
}
None => eyre::bail!("Unknown marker {k:?}"),
})
.collect::<Result<_, _>>()?,
}
} else if let Some(bin_path) = &cli.markers_for_bin {
icu::markers_for_bin(&std::fs::read(bin_path)?)?
.into_iter()
.collect()
} else {
eyre::bail!("--markers or --markers-for-bin are required.")
};
enum PreprocessedLocales {
Locales(Vec<DataLocale>),
Full,
}
#[allow(unused_mut)]
let mut preprocessed_locales = if cli.locales.as_slice() == ["none"] {
Some(PreprocessedLocales::Locales(vec![]))
} else if cli.locales.as_slice() == ["full"] {
Some(PreprocessedLocales::Full)
} else {
if cli.locales.as_slice() == ["all"] {
log::warn!(
"`--locales all` selects the Allar language. Use `--locales full` for all locales"
);
}
None
};
let (provider, fallbacker): (Box<dyn ExportableProvider>, _) = match () {
() if markers == [HelloWorldV1::INFO] => {
// Just do naive fallback instead of pulling in compiled data or something. We only use this code path to debug
// providers, so we don't need 100% correct fallback.
(Box::new(icu_provider::hello_world::HelloWorldProvider), LocaleFallbacker::new_without_data())
}
() if markers.contains(&HelloWorldV1::INFO) => {
eyre::bail!("HelloWorldV1 is only allowed as the only marker")
}
#[cfg(feature = "blob_input")]
() if cli.input_blob.is_some() => {
let provider = icu_provider_blob::BlobDataProvider::try_new_from_blob(
std::fs::read(cli.input_blob.unwrap())?.into(),
)?;
let fallbacker = LocaleFallbacker::try_new_with_buffer_provider(&provider)?;
(Box::new(ReexportableBlobDataProvider(provider)), fallbacker)
},
#[cfg(all(not(feature = "provider"), feature = "blob_input"))]
() => eyre::bail!("--input-blob is required without the `provider` Cargo feature"),
#[cfg(feature = "provider")]
() => {
let mut p = SourceDataProvider::new_custom();
p = p.with_collation_root_han(match cli.collation_root_han {
CollationRootHan::Unihan => icu_provider_source::CollationRootHan::Unihan,
CollationRootHan::Implicit => {
icu_provider_source::CollationRootHan::Implicit
}
});
if cli.trie_type == TrieType::Fast {
p = p.with_fast_tries();
}
p = match (cli.cldr_root, cli.cldr_tag.as_str()) {
(Some(path), _) => p.with_cldr(&path)?,
#[cfg(feature = "networking")]
(_, "latest") => p.with_cldr_for_tag(SourceDataProvider::TESTED_CLDR_TAG),
#[cfg(feature = "networking")]
(_, tag) => p.with_cldr_for_tag(tag),
#[cfg(not(feature = "networking"))]
(None, _) => {
eyre::bail!(
"Please set --cldr-root or enable the `networking` Cargo feature"
)
}
};
p = match (cli.icuexport_root, cli.icuexport_tag.as_str()) {
(Some(path), _) => p.with_icuexport(&path)?,
#[cfg(feature = "networking")]
(_, "latest") => {
p.with_icuexport_for_tag(SourceDataProvider::TESTED_ICUEXPORT_TAG)
}
#[cfg(feature = "networking")]
(_, tag) => p.with_icuexport_for_tag(tag),
#[cfg(not(feature = "networking"))]
(None, _) => {
eyre::bail!(
"Please set --icuexport-root or enable the `networking` Cargo feature"
)
}
};
p = match (cli.segmenter_lstm_root, cli.segmenter_lstm_tag.as_str()) {
(Some(path), _) => p.with_segmenter_lstm(&path)?,
#[cfg(feature = "networking")]
(_, "latest") => {
p.with_segmenter_lstm_for_tag(SourceDataProvider::TESTED_SEGMENTER_LSTM_TAG)
}
#[cfg(feature = "networking")]
(_, tag) => p.with_segmenter_lstm_for_tag(tag),
#[cfg(not(feature = "networking"))]
(None, _) => {
eyre::bail!(
"Please set --segmenter-lstm-root or enable the `networking` Cargo feature"
)
}
};
p = match (cli.tzdb_root, cli.tzdb_tag.as_str()) {
(Some(path), _) => p.with_tzdb(&path)?,
#[cfg(feature = "networking")]
(_, "latest") => {
p.with_tzdb_for_tag(SourceDataProvider::TESTED_TZDB_TAG)
}
#[cfg(feature = "networking")]
(_, tag) => p.with_tzdb_for_tag(tag),
#[cfg(not(feature = "networking"))]
(None, _) => {
eyre::bail!(
"Please set --tzdb-root or enable the `networking` Cargo feature"
)
}
};
if cli.locales.as_slice() == ["recommended"] {
preprocessed_locales = Some(PreprocessedLocales::Locales(
p.locales_for_coverage_levels([
icu_provider_source::CoverageLevel::Modern,
icu_provider_source::CoverageLevel::Moderate,
icu_provider_source::CoverageLevel::Basic,
])?
.into_iter()
.collect(),
));
} else if let Some(locale_subsets) = cli
.locales
.iter()
.map(|s| match &**s {
"basic" => Some(icu_provider_source::CoverageLevel::Basic),
"moderate" => Some(icu_provider_source::CoverageLevel::Moderate),
"modern" => Some(icu_provider_source::CoverageLevel::Modern),
_ => None,
})
.collect::<Option<Vec<_>>>()
{
preprocessed_locales = Some(PreprocessedLocales::Locales(
p.locales_for_coverage_levels(locale_subsets.into_iter())?
.into_iter()
.collect(),
));
}
let fallbacker = LocaleFallbacker::try_new_unstable(&p)?;
(Box::new(p), fallbacker)
}
#[cfg(not(any(feature = "provider", feature = "blob_input")))]
() => eyre::bail!("Only the `HelloWorld marker is supported without Cargo features `blob_input` or `provider`"),
};
let locale_families = match preprocessed_locales {
Some(PreprocessedLocales::Full) => vec![DataLocaleFamily::FULL],
Some(PreprocessedLocales::Locales(locales)) => locales
.into_iter()
.map(DataLocaleFamily::with_descendants)
.collect(),
None => cli
.locales
.into_iter()
.map(|family_str| family_str.parse().wrap_err(family_str))
.collect::<eyre::Result<Vec<_>>>()?,
};
let deduplication_strategy = match cli.deduplication {
Some(Deduplication::Maximal) => DeduplicationStrategy::Maximal,
Some(Deduplication::RetainBaseLanguages) => {
DeduplicationStrategy::RetainBaseLanguages
}
Some(Deduplication::None) => DeduplicationStrategy::None,
None => match cli.format {
Format::Fs | Format::Blob => DeduplicationStrategy::None,
Format::Baked if cli.no_internal_fallback && cli.deduplication.is_none() =>
eyre::bail!("--no-internal-fallback requires an explicit --deduplication value. Baked exporter would default to maximal deduplication, which might not be intended"),
Format::Baked => DeduplicationStrategy::Maximal,
}
};
let mut driver = ExportDriver::new(locale_families, deduplication_strategy.into(), fallbacker);
driver = driver.with_markers(markers);
driver = driver.with_additional_collations(
cli.include_collations
.iter()
.map(|c| c.to_datagen_value().to_owned()),
);
driver = if cli.segmenter_models.as_slice() == ["none"] {
driver.with_segmenter_models([])
} else if cli.segmenter_models.as_slice() == ["recommended"] {
driver.with_segmenter_models([
"Burmese_codepoints_exclusive_model4_heavy".into(),
"burmesedict".into(),
"cjdict".into(),
"Khmer_codepoints_exclusive_model4_heavy".into(),
"khmerdict".into(),
"Lao_codepoints_exclusive_model4_heavy".into(),
"laodict".into(),
"Thai_codepoints_exclusive_model4_heavy".into(),
"thaidict".into(),
])
} else {
driver.with_segmenter_models(cli.segmenter_models.clone())
};
let _metadata = match cli.format {
#[cfg(not(feature = "fs_exporter"))]
Format::Fs => {
eyre::bail!("Exporting to an FsProvider requires the `fs_exporter` Cargo feature")
}
#[cfg(feature = "fs_exporter")]
Format::Fs => driver.export(&provider, {
use icu_provider_export::fs_exporter::*;
FilesystemExporter::try_new(
match cli.syntax {
Syntax::Bincode => Box::<serializers::Bincode>::default(),
Syntax::Postcard => Box::<serializers::Postcard>::default(),
Syntax::Json if cli.pretty => Box::new(serializers::Json::pretty()),
Syntax::Json => Box::<serializers::Json>::default(),
},
{
let mut options = Options::default();
options.root = cli.output.unwrap_or_else(|| PathBuf::from("icu4x_data"));
if cli.overwrite {
options.overwrite = OverwriteOption::RemoveAndReplace
}
options
},
)?
})?,
#[cfg(not(feature = "blob_exporter"))]
Format::Blob => {
eyre::bail!("Exporting to a BlobProvider requires the `blob_exporter` Cargo feature")
}
#[cfg(feature = "blob_exporter")]
Format::Blob => driver.export(&provider, {
use icu_provider_export::blob_exporter::*;
let sink: Box<dyn std::io::Write + Sync> = if let Some(path) = cli.output {
if !cli.overwrite && path.exists() {
eyre::bail!("Output path is present: {:?}", path);
}
Box::new(
std::fs::File::create(&path)
.with_context(|| path.to_string_lossy().to_string())?,
)
} else {
Box::new(std::io::stdout())
};
BlobExporter::new_with_sink(sink)
})?,
#[cfg(not(feature = "baked_exporter"))]
Format::Baked => {
eyre::bail!("Exporting to a baked provider requires the `baked_exporter` Cargo feature")
}
#[cfg(feature = "baked_exporter")]
Format::Baked => driver.export(&provider, {
icu_provider_export::baked_exporter::BakedExporter::new(
cli.output.unwrap_or_else(|| PathBuf::from("icu4x_data")),
{
let mut options = icu_provider_export::baked_exporter::Options::default();
options.pretty = cli.pretty;
options.use_internal_fallback = !cli.no_internal_fallback;
options.use_separate_crates = cli.use_separate_crates;
options.overwrite = cli.overwrite;
options
},
)?
})?,
};
Ok(())
}
macro_rules! cb {
($($marker_ty:ty:$marker:ident,)+ #[experimental] $($emarker_ty:ty:$emarker:ident,)+) => {
fn all_markers() -> Vec<DataMarkerInfo> {
vec![
$(
<$marker_ty>::INFO,
)+
$(
#[cfg(feature = "experimental")]
<$emarker_ty>::INFO,
)+
]
}
fn marker_lookup() -> &'static HashMap<String, Option<DataMarkerInfo>> {
use std::sync::OnceLock;
static LOOKUP: OnceLock<HashMap<String, Option<DataMarkerInfo>>> = OnceLock::new();
LOOKUP.get_or_init(|| {
[
(stringify!(icu_provider::hello_world::HelloWorldV1).replace(' ', ""), Some(icu_provider::hello_world::HelloWorldV1::INFO)),
(stringify!(HelloWorldV1).into(), Some(icu_provider::hello_world::HelloWorldV1::INFO)),
$(
(stringify!($marker_ty).replace(' ', ""), Some(<$marker_ty>::INFO)),
(stringify!($marker).into(), Some(<$marker_ty>::INFO)),
)+
$(
#[cfg(feature = "experimental")]
(stringify!($emarker_ty).replace(' ', ""), Some(<$emarker_ty>::INFO)),
#[cfg(feature = "experimental")]
(stringify!($emarker).into(), Some(<$emarker_ty>::INFO)),
#[cfg(not(feature = "experimental"))]
(stringify!($emarker_ty).replace(' ', ""), None),
#[cfg(not(feature = "experimental"))]
(stringify!($emarker).into(), None),
)+
]
.into_iter()
.collect()
})
}
#[test]
fn test_lookup() {
assert_eq!(marker_lookup().get("ListAndV1"), Some(&Some(icu::list::provider::ListAndV1::INFO)));
assert_eq!(marker_lookup().get("icu::list::provider::ListAndV1"), Some(&Some(icu::list::provider::ListAndV1::INFO)));
assert_eq!(marker_lookup().get("foo"), None);
}
#[cfg(feature = "blob_input")]
icu_provider::export::make_exportable_provider!(
ReexportableBlobDataProvider,
[
icu_provider::hello_world::HelloWorldV1,
$(
$marker_ty,
)+
$(
#[cfg(feature = "experimental")]
$emarker_ty,
)+
]
);
}
}
extern crate alloc;
icu_provider_registry::registry!(cb);
#[cfg(feature = "blob_input")]
use icu_provider::buf::DeserializingBufferProvider;
#[cfg(feature = "blob_input")]
use icu_provider::prelude::*;
#[cfg(feature = "blob_input")]
use icu_provider_blob::BlobDataProvider;
#[cfg(feature = "blob_input")]
struct ReexportableBlobDataProvider(icu_provider_blob::BlobDataProvider);
#[cfg(feature = "blob_input")]
impl<M: DataMarker> DataProvider<M> for ReexportableBlobDataProvider
where
BlobDataProvider: AsDeserializingBufferProvider,
for<'a> DeserializingBufferProvider<'a, BlobDataProvider>: DataProvider<M>,
{
fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
self.0.as_deserializing().load(req)
}
}
#[cfg(feature = "blob_input")]
impl<M: DataMarker> IterableDataProvider<M> for ReexportableBlobDataProvider
where
BlobDataProvider: AsDeserializingBufferProvider,
for<'a> DeserializingBufferProvider<'a, BlobDataProvider>: DataProvider<M>,
{
fn iter_ids(&self) -> Result<std::collections::BTreeSet<DataIdentifierCow>, DataError> {
self.0.iter_ids_for_marker(M::INFO)
}
}