icu_datagen 1.5.0

Generate data for ICU4X DataProvider
Documentation
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// 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 ).

// If no exporter feature is enabled this all doesn't make sense
#![cfg_attr(
    not(any(
        feature = "blob_exporter",
        feature = "fs_exporter",
        feature = "baked_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_datagen::prelude::*;
use icu_provider::datagen::ExportableProvider;
use simple_logger::SimpleLogger;
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_datagen/{}", 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, default_value_t = Format::DeprecatedDefault, hide_default_value = true)]
    #[arg(
        help = "Select the output format: a directory tree of files, a single blob, or a Rust module."
    )]
    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=dir only: serde serialization format.")]
    syntax: Syntax,

    #[arg(short, long)]
    #[arg(help = "--format=mod, --format=dir only: pretty-print the Rust or JSON output files.")]
    pretty: bool,

    #[arg(long, hide = true)]
    #[arg(
        help = "--format=dir only: whether to add a fingerprints file to the output. This feature will be removed in a future version."
    )]
    fingerprint: 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 keys 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 keys 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 keys 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 keys 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_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 = CollationHanDatabase::Implicit)]
    #[arg(help = "Which collation han database to use.")]
    #[cfg(feature = "provider")]
    collation_han_database: CollationHanDatabase,

    #[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, hide = true)]
    #[arg(help = "Deprecated, use --locales full or --locales modern")]
    cldr_locale_subset: bool,

    #[arg(long, short, num_args = 1..)]
    #[arg(
        help = "Include these resource keys in the output. Accepts multiple arguments.\n\
                Set to 'all' for all keys, or 'none' for no keys."
    )]
    keys: Vec<String>,

    #[arg(long, value_name = "KEY_FILE")]
    #[arg(
        help = "Path to text file with resource keys to include, one per line. Empty lines \
                  and lines starting with '#' are ignored.\n
                  Requires the `legacy_api` Cargo feature."
    )]
    #[cfg(feature = "legacy_api")]
    key_file: Option<PathBuf>,

    #[arg(long, value_name = "BINARY")]
    #[arg(help = "Analyzes the binary and only includes keys that are used by the binary.")]
    keys_for_bin: Option<PathBuf>,

    #[arg(long, hide = true)]
    #[arg(help = "Deprecated: alias for --keys all")]
    all_keys: bool,

    #[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, hide = true)]
    #[arg(help = "Deprecated: alias for --locales full")]
    all_locales: bool,

    #[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,blob2}, omit this option to dump to stdout. \
                  For --format={dir,mod} defaults to 'icu4x_data'."
    )]
    output: Option<PathBuf>,

    #[arg(long, hide = true)]
    #[arg(
        help = "--format=mod only: insert feature gates for individual `icu_*` crates. Requires --use-separate-crates"
    )]
    insert_feature_gates: bool,

    #[arg(long)]
    #[arg(
        help = "--format=mod only: use types from individual `icu_*` crates instead of the `icu` meta-crate."
    )]
    use_separate_crates: bool,

    // TODO(#2856): Change the default to Auto in 2.0
    #[arg(short, long, value_enum, default_value_t = Fallback::Hybrid)]
    #[arg(
        hide = true,
        help = "Deprecated: use --deduplication, --runtime-fallback-location, or --without-fallback"
    )]
    fallback: Fallback,

    #[arg(long)]
    #[arg(
        help = "disables locale fallback, instead exporting exactly the locales specified in --locales. \
                Cannot be used with --deduplication, --runtime-fallback-location"
    )]
    without_fallback: bool,

    #[arg(long, value_enum)]
    #[arg(help = "configures where runtime fallback should take place in code. \
                If not set, determined by the exporter: \
                internal fallback is used if the exporter supports it. \
                Cannot be used with --without-fallback")]
    runtime_fallback_location: Option<RuntimeFallbackLocation>,

    #[arg(long, value_enum)]
    #[arg(
        help = "configures the deduplication of locales for exported data payloads. \
                If not set, determined by `runtime_fallback_location`: \
                if internal fallback is enabled, a more aggressive deduplication strategy is used. \
                Cannot be used with --without-fallback"
    )]
    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 {
    Dir,
    Blob,
    Blob2,
    Mod,
    DeprecatedDefault,
}

#[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 crate::CollationHanDatabase
enum CollationHanDatabase {
    Unihan,
    Implicit,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum CollationTable {
    Gb2312,
    Big5han,
    Search,
    Searchji,
    #[value(alias = "search*")] // for backwards compatability
    SearchAll,
}

impl CollationTable {
    fn to_datagen_value(self) -> &'static str {
        match self {
            Self::Gb2312 => "gb2312",
            Self::Big5han => "big5han",
            Self::Search => "search",
            Self::Searchji => "searchji",
            Self::SearchAll => "search*",
        }
    }
}

// Mirrors crate::FallbackMode
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Fallback {
    Auto,
    Hybrid,
    Runtime,
    RuntimeManual,
    Preresolved,
}

// Mirrors crate::DeduplicationStrategy
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Deduplication {
    Maximal,
    RetainBaseLanguages,
    None,
}

// Mirrors crate::RuntimeFallbackLocation
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum RuntimeFallbackLocation {
    Internal,
    External,
}

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()
            .env()
            .with_level(log::LevelFilter::Info)
            .init()
            .unwrap()
    }

    #[allow(unused_mut)]
    let mut preprocessed_locales = if cli.locales.as_slice() == ["none"] {
        Some(PreprocessedLocales::LanguageIdentifiers(vec![]))
    } else if cli.locales.as_slice() == ["full"] || cli.all_locales {
        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: Box<dyn ExportableProvider> = match () {
        #[cfg(feature = "blob_input")]
        () if cli.input_blob.is_some() => Box::new(ReexportableBlobDataProvider(
            icu_provider_blob::BlobDataProvider::try_new_from_blob(
                std::fs::read(cli.input_blob.unwrap())?.into(),
            )?,
        )),

        #[cfg(not(feature = "provider"))]
        () => eyre::bail!("--input-blob is required without the `provider` Cargo feature"),

        #[cfg(feature = "provider")]
        () => {
            let mut p = DatagenProvider::new_custom();

            p = p.with_collation_han_database(match cli.collation_han_database {
                CollationHanDatabase::Unihan => icu_datagen::CollationHanDatabase::Unihan,
                CollationHanDatabase::Implicit => icu_datagen::CollationHanDatabase::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(DatagenProvider::LATEST_TESTED_CLDR_TAG),
                #[cfg(feature = "networking")]
                (_, tag) => p.with_cldr_for_tag(tag),
                #[cfg(not(feature = "networking"))]
                (None, _) => {
                    eyre::bail!(
                        "Downloading data from tags requires 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(DatagenProvider::LATEST_TESTED_ICUEXPORT_TAG)
                }
                #[cfg(feature = "networking")]
                (_, tag) => p.with_icuexport_for_tag(tag),
                #[cfg(not(feature = "networking"))]
                (None, _) => {
                    eyre::bail!(
                        "Downloading data from tags requires 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(DatagenProvider::LATEST_TESTED_SEGMENTER_LSTM_TAG)
                }
                #[cfg(feature = "networking")]
                (_, tag) => p.with_segmenter_lstm_for_tag(tag),
                #[cfg(not(feature = "networking"))]
                (None, _) => {
                    eyre::bail!(
                        "Downloading data from tags requires the `networking` Cargo feature"
                    )
                }
            };

            if cli.locales.as_slice() == ["recommended"] {
                preprocessed_locales = Some(PreprocessedLocales::LanguageIdentifiers(
                    p.locales_for_coverage_levels([
                        CoverageLevel::Modern,
                        CoverageLevel::Moderate,
                        CoverageLevel::Basic,
                    ])?
                    .into_iter()
                    .collect(),
                ));
            } else if let Some(locale_subsets) = cli
                .locales
                .iter()
                .map(|s| match &**s {
                    "basic" => Some(CoverageLevel::Basic),
                    "moderate" => Some(CoverageLevel::Moderate),
                    "modern" => Some(CoverageLevel::Modern),
                    _ => None,
                })
                .collect::<Option<Vec<_>>>()
            {
                preprocessed_locales = Some(PreprocessedLocales::LanguageIdentifiers(
                    p.locales_for_coverage_levels(locale_subsets.into_iter())?
                        .into_iter()
                        .collect(),
                ));
            }

            Box::new(p)
        }

        #[cfg(all(not(feature = "blob_input"), not(feature = "provider")))]
        () => compile_error!("Feature `bin` requires either `blob_input` or `provider`"),
    };

    let mut driver = DatagenDriver::new();

    driver = driver.with_keys(if cli.all_keys {
        icu_datagen::all_keys()
    } else if !cli.keys.is_empty() {
        match cli.keys.as_slice() {
            [x] if x == "none" => Default::default(),
            [x] if x == "all" => icu_datagen::all_keys(),
            [x] if x == "experimental-all" => {
                log::warn!("--keys=experimental-all is deprecated, using --keys=all.");
                log::warn!("--keys=all behavior is dependent on activated Cargo features, so");
                log::warn!("building with experimental features includes experimental keys");
                icu_datagen::all_keys()
            }
            keys => keys
                .iter()
                .map(|k| icu_datagen::key(k).ok_or(eyre::eyre!(k.to_string())))
                .collect::<Result<_, _>>()?,
        }
    } else if let Some(bin_path) = &cli.keys_for_bin {
        icu_datagen::keys_from_bin(bin_path)?.into_iter().collect()
    } else {
        #[cfg(feature = "legacy_api")]
        if let Some(key_file_path) = &cli.key_file {
            log::warn!("The --key-file argument is deprecated.");
            #[allow(deprecated)]
            icu_datagen::keys_from_file(key_file_path)
                .with_context(|| key_file_path.to_string_lossy().into_owned())?
                .into_iter()
                .collect()
        } else {
            eyre::bail!("--keys or --keys-for-bin are required.")
        }
        #[cfg(not(feature = "legacy_api"))]
        eyre::bail!("--keys or --keys-for-bin are required.")
    });

    enum PreprocessedLocales {
        LanguageIdentifiers(Vec<LanguageIdentifier>),
        Full,
    }

    if cli.without_fallback || matches!(cli.fallback, Fallback::Preresolved) {
        driver = driver.with_locales_no_fallback(
            match preprocessed_locales {
                Some(PreprocessedLocales::Full) => {
                    eyre::bail!("--without-fallback needs an explicit locale list")
                }
                Some(PreprocessedLocales::LanguageIdentifiers(lids)) => lids,
                None => cli
                    .locales
                    .into_iter()
                    .map(|langid_str| langid_str.parse().wrap_err(langid_str))
                    .collect::<eyre::Result<Vec<_>>>()?,
            },
            Default::default(),
        );
    } else {
        let locale_families = match preprocessed_locales {
            Some(PreprocessedLocales::Full) => vec![LocaleFamily::FULL],
            Some(PreprocessedLocales::LanguageIdentifiers(lids)) => lids
                .into_iter()
                .map(LocaleFamily::with_descendants)
                .collect(),
            None => cli
                .locales
                .into_iter()
                .map(|family_str| family_str.parse().wrap_err(family_str))
                .collect::<eyre::Result<Vec<_>>>()?,
        };
        let mut options: FallbackOptions = Default::default();
        options.deduplication_strategy =
            match (cli.deduplication, cli.fallback, cli.without_fallback) {
                (None, _, true) => None,
                (Some(_), _, true) => {
                    eyre::bail!("cannot combine --without-fallback and --deduplication")
                }
                (Some(x), _, false) => match x {
                    Deduplication::Maximal => Some(icu_datagen::DeduplicationStrategy::Maximal),
                    Deduplication::RetainBaseLanguages => {
                        Some(icu_datagen::DeduplicationStrategy::RetainBaseLanguages)
                    }
                    Deduplication::None => Some(icu_datagen::DeduplicationStrategy::None),
                },
                (None, fallback_mode, false) => match fallback_mode {
                    Fallback::Auto => None,
                    Fallback::Hybrid => Some(icu_datagen::DeduplicationStrategy::None),
                    Fallback::Runtime => Some(icu_datagen::DeduplicationStrategy::Maximal),
                    Fallback::RuntimeManual => Some(icu_datagen::DeduplicationStrategy::Maximal),
                    Fallback::Preresolved => None,
                },
            };
        options.runtime_fallback_location = match (
            cli.runtime_fallback_location,
            cli.fallback,
            cli.without_fallback,
        ) {
            (None, _, true) => None,
            (Some(_), _, true) => {
                eyre::bail!("cannot combine --without-fallback and --runtime-fallback-location")
            }
            (Some(RuntimeFallbackLocation::Internal), _, false) => {
                Some(icu_datagen::RuntimeFallbackLocation::Internal)
            }
            (Some(RuntimeFallbackLocation::External), _, false) => {
                Some(icu_datagen::RuntimeFallbackLocation::External)
            }
            (None, Fallback::Auto, false) => None,
            (None, Fallback::Hybrid, false) => Some(icu_datagen::RuntimeFallbackLocation::External),
            (None, Fallback::Runtime, false) => {
                Some(icu_datagen::RuntimeFallbackLocation::Internal)
            }
            (None, Fallback::RuntimeManual, false) => {
                Some(icu_datagen::RuntimeFallbackLocation::External)
            }
            (None, Fallback::Preresolved, false) => None,
        };
        driver = driver.with_locales_and_fallback(locale_families, options);
    }
    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())
    };

    if cli.format == Format::DeprecatedDefault {
        log::warn!(
            "Defaulting to --format=dir. This will become a required parameter in the future."
        );
    }

    match cli.format {
        #[cfg(not(feature = "fs_exporter"))]
        Format::Dir | Format::DeprecatedDefault => {
            eyre::bail!("Exporting to an FsProvider requires the `fs_exporter` Cargo feature")
        }
        #[cfg(feature = "fs_exporter")]
        Format::Dir | Format::DeprecatedDefault => driver.export(&provider, {
            use icu_provider_fs::export::*;

            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 = ExporterOptions::default();
                    options.root = cli.output.unwrap_or_else(|| PathBuf::from("icu4x_data"));
                    if cli.overwrite {
                        options.overwrite = OverwriteOption::RemoveAndReplace
                    }
                    #[allow(deprecated)] // obviously
                    {
                        options.fingerprint = cli.fingerprint;
                    }
                    options
                },
            )?
        })?,
        #[cfg(not(feature = "blob_exporter"))]
        Format::Blob | Format::Blob2 => {
            eyre::bail!("Exporting to a BlobProvider requires the `blob_exporter` Cargo feature")
        }
        #[cfg(feature = "blob_exporter")]
        Format::Blob | Format::Blob2 => driver.export(&provider, {
            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())
            };
            if cli.format == Format::Blob {
                icu_provider_blob::export::BlobExporter::new_with_sink(sink)
            } else {
                icu_provider_blob::export::BlobExporter::new_v2_with_sink(sink)
            }
        })?,
        #[cfg(not(feature = "baked_exporter"))]
        Format::Mod => {
            eyre::bail!("Exporting to a baked provider requires the `baked_exporter` Cargo feature")
        }
        #[cfg(feature = "baked_exporter")]
        Format::Mod => driver.export(&provider, {
            icu_datagen::baked_exporter::BakedExporter::new(
                cli.output.unwrap_or_else(|| PathBuf::from("icu4x_data")),
                {
                    let mut options = icu_datagen::baked_exporter::Options::default();
                    options.pretty = cli.pretty;
                    options.insert_feature_gates = cli.insert_feature_gates;
                    options.use_separate_crates = cli.use_separate_crates;
                    options.overwrite = cli.overwrite;
                    options
                },
            )?
        })?,
    }

    Ok(())
}

#[cfg(feature = "blob_input")]
use icu_provider::datagen::*;
#[cfg(feature = "blob_input")]
use icu_provider::prelude::*;
#[cfg(feature = "blob_input")]
use icu_provider::serde::DeserializingBufferProvider;
#[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: KeyedDataMarker> 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: KeyedDataMarker> IterableDataProvider<M> for ReexportableBlobDataProvider
where
    BlobDataProvider: AsDeserializingBufferProvider,
    for<'a> DeserializingBufferProvider<'a, BlobDataProvider>: DataProvider<M>,
{
    fn supported_locales(&self) -> Result<Vec<DataLocale>, DataError> {
        self.0.supported_locales_for_key(M::KEY)
    }
}

#[cfg(feature = "blob_input")]
icu_datagen::make_exportable_provider!(ReexportableBlobDataProvider);