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
// 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 ).

//! Helpers for switching between multiple providers.

use alloc::collections::BTreeSet;
#[cfg(feature = "export")]
use icu_provider::export::ExportableProvider;
use icu_provider::prelude::*;

/// A provider that is one of two types determined at runtime.
///
/// Data provider traits implemented by both `P0` and `P1` are implemented on
/// `EitherProvider<P0, P1>`.
#[allow(clippy::exhaustive_enums)] // this is stable
#[derive(Debug)]
pub enum EitherProvider<P0, P1> {
    /// A value of type `P0`.
    A(P0),
    /// A value of type `P1`.
    B(P1),
}

impl<P0: AnyProvider, P1: AnyProvider> AnyProvider for EitherProvider<P0, P1> {
    #[inline]
    fn load_any(&self, marker: DataMarkerInfo, req: DataRequest) -> Result<AnyResponse, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.load_any(marker, req),
            B(p) => p.load_any(marker, req),
        }
    }
}

impl<M: DynamicDataMarker, P0: DynamicDataProvider<M>, P1: DynamicDataProvider<M>>
    DynamicDataProvider<M> for EitherProvider<P0, P1>
{
    #[inline]
    fn load_data(
        &self,
        marker: DataMarkerInfo,
        req: DataRequest,
    ) -> Result<DataResponse<M>, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.load_data(marker, req),
            B(p) => p.load_data(marker, req),
        }
    }
}

impl<M: DynamicDataMarker, P0: DynamicDryDataProvider<M>, P1: DynamicDryDataProvider<M>>
    DynamicDryDataProvider<M> for EitherProvider<P0, P1>
{
    #[inline]
    fn dry_load_data(
        &self,
        marker: DataMarkerInfo,
        req: DataRequest,
    ) -> Result<DataResponseMetadata, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.dry_load_data(marker, req),
            B(p) => p.dry_load_data(marker, req),
        }
    }
}

impl<M: DataMarker, P0: DataProvider<M>, P1: DataProvider<M>> DataProvider<M>
    for EitherProvider<P0, P1>
{
    #[inline]
    fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.load(req),
            B(p) => p.load(req),
        }
    }
}

impl<M: DataMarker, P0: DryDataProvider<M>, P1: DryDataProvider<M>> DryDataProvider<M>
    for EitherProvider<P0, P1>
{
    #[inline]
    fn dry_load(&self, req: DataRequest) -> Result<DataResponseMetadata, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.dry_load(req),
            B(p) => p.dry_load(req),
        }
    }
}

impl<
        M: DynamicDataMarker,
        P0: IterableDynamicDataProvider<M>,
        P1: IterableDynamicDataProvider<M>,
    > IterableDynamicDataProvider<M> for EitherProvider<P0, P1>
{
    #[inline]
    fn iter_ids_for_marker(
        &self,
        marker: DataMarkerInfo,
    ) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.iter_ids_for_marker(marker),
            B(p) => p.iter_ids_for_marker(marker),
        }
    }
}

impl<M: DataMarker, P0: IterableDataProvider<M>, P1: IterableDataProvider<M>>
    IterableDataProvider<M> for EitherProvider<P0, P1>
{
    #[inline]
    fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
        use EitherProvider::*;
        match self {
            A(p) => p.iter_ids(),
            B(p) => p.iter_ids(),
        }
    }
}

#[cfg(feature = "export")]
impl<P0, P1> ExportableProvider for EitherProvider<P0, P1>
where
    P0: ExportableProvider,
    P1: ExportableProvider,
{
    fn supported_markers(&self) -> std::collections::HashSet<DataMarkerInfo> {
        use EitherProvider::*;
        match self {
            A(p) => p.supported_markers(),
            B(p) => p.supported_markers(),
        }
    }
}