// 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 icu_provider::prelude::*; #[cfg(feature = "datagen")] use icu_provider::datagen; /// A provider that is one of two types determined at runtime. /// /// Data provider traits implemented by both `P0` and `P1` are implemented on /// `EitherProvider`. #[allow(clippy::exhaustive_enums)] // this is stable pub enum EitherProvider { /// A value of type `P0`. A(P0), /// A value of type `P1`. B(P1), } impl AnyProvider for EitherProvider { #[inline] fn load_any(&self, key: DataKey, req: DataRequest) -> Result { use EitherProvider::*; match self { A(p) => p.load_any(key, req), B(p) => p.load_any(key, req), } } } impl BufferProvider for EitherProvider { #[inline] fn load_buffer( &self, key: DataKey, req: DataRequest, ) -> Result, DataError> { use EitherProvider::*; match self { A(p) => p.load_buffer(key, req), B(p) => p.load_buffer(key, req), } } } impl, P1: DynamicDataProvider> DynamicDataProvider for EitherProvider { #[inline] fn load_data(&self, key: DataKey, req: DataRequest) -> Result, DataError> { use EitherProvider::*; match self { A(p) => p.load_data(key, req), B(p) => p.load_data(key, req), } } } impl, P1: DataProvider> DataProvider for EitherProvider { #[inline] fn load(&self, req: DataRequest) -> Result, DataError> { use EitherProvider::*; match self { A(p) => p.load(req), B(p) => p.load(req), } } } #[cfg(feature = "datagen")] impl< M: DataMarker, P0: datagen::IterableDynamicDataProvider, P1: datagen::IterableDynamicDataProvider, > datagen::IterableDynamicDataProvider for EitherProvider { #[inline] fn supported_locales_for_key( &self, key: DataKey, ) -> Result, DataError> { use EitherProvider::*; match self { A(p) => p.supported_locales_for_key(key), B(p) => p.supported_locales_for_key(key), } } } #[cfg(feature = "datagen")] impl< M: KeyedDataMarker, P0: datagen::IterableDataProvider, P1: datagen::IterableDataProvider, > datagen::IterableDataProvider for EitherProvider { #[inline] fn supported_locales(&self) -> Result, DataError> { use EitherProvider::*; match self { A(p) => p.supported_locales(), B(p) => p.supported_locales(), } } }