Skip to main content

icu_provider_adapters/
empty.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5//! Empty data provider implementations.
6//!
7//! Use [`EmptyDataProvider`] as a stand-in for a provider that always fails.
8
9use alloc::collections::BTreeSet;
10#[cfg(feature = "export")]
11use icu_provider::export::ExportableProvider;
12use icu_provider::prelude::*;
13
14/// A data provider that always returns an error.
15///
16/// The returned error kind is configurable.
17///
18/// # Examples
19///
20/// ```
21/// use icu_provider::hello_world::HelloWorldV1;
22/// use icu_provider::prelude::*;
23/// use icu_provider_adapters::empty::EmptyDataProvider;
24///
25/// let provider = EmptyDataProvider::new();
26///
27/// assert!(matches!(
28///     DataProvider::<HelloWorldV1>::load(&provider, Default::default()),
29///     Err(DataError {
30///         kind: DataErrorKind::MarkerNotFound,
31///         ..
32///     })
33/// ));
34/// ```
35#[derive(Debug)]
36pub struct EmptyDataProvider {
37    error_kind: DataErrorKind,
38}
39
40impl Default for EmptyDataProvider {
41    fn default() -> Self {
42        Self::new()
43    }
44}
45
46impl EmptyDataProvider {
47    /// Creates a data provider that always returns [`DataErrorKind::MarkerNotFound`].
48    pub fn new() -> Self {
49        Self {
50            error_kind: DataErrorKind::MarkerNotFound,
51        }
52    }
53
54    /// Creates a data provider that always returns the specified error kind.
55    pub fn new_with_error_kind(error_kind: DataErrorKind) -> Self {
56        Self { error_kind }
57    }
58}
59
60impl<M> DynamicDataProvider<M> for EmptyDataProvider
61where
62    M: DynamicDataMarker,
63{
64    fn load_data(
65        &self,
66        marker: DataMarkerInfo,
67        base_req: DataRequest,
68    ) -> Result<DataResponse<M>, DataError> {
69        Err(self.error_kind.with_req(marker, base_req))
70    }
71}
72
73impl<M> DataProvider<M> for EmptyDataProvider
74where
75    M: DataMarker,
76{
77    fn load(&self, base_req: DataRequest) -> Result<DataResponse<M>, DataError> {
78        Err(self.error_kind.with_req(M::INFO, base_req))
79    }
80}
81
82impl<M> IterableDataProvider<M> for EmptyDataProvider
83where
84    M: DataMarker,
85{
86    fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError> {
87        Ok(Default::default())
88    }
89}
90
91impl<M> IterableDynamicDataProvider<M> for EmptyDataProvider
92where
93    M: DynamicDataMarker,
94{
95    fn iter_ids_for_marker(
96        &self,
97        _: DataMarkerInfo,
98    ) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError> {
99        Ok(Default::default())
100    }
101}
102
103#[cfg(feature = "export")]
104impl ExportableProvider for EmptyDataProvider {
105    fn supported_markers(&self) -> alloc::collections::BTreeSet<DataMarkerInfo> {
106        Default::default()
107    }
108}