• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 use alloc::collections::BTreeSet;
10 #[cfg(feature = "export")]
11 use icu_provider::export::ExportableProvider;
12 use 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)]
36 pub struct EmptyDataProvider {
37     error_kind: DataErrorKind,
38 }
39 
40 impl Default for EmptyDataProvider {
default() -> Self41     fn default() -> Self {
42         Self::new()
43     }
44 }
45 
46 impl EmptyDataProvider {
47     /// Creates a data provider that always returns [`DataErrorKind::MarkerNotFound`].
new() -> Self48     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.
new_with_error_kind(error_kind: DataErrorKind) -> Self55     pub fn new_with_error_kind(error_kind: DataErrorKind) -> Self {
56         Self { error_kind }
57     }
58 }
59 
60 impl<M> DynamicDataProvider<M> for EmptyDataProvider
61 where
62     M: DynamicDataMarker,
63 {
load_data( &self, marker: DataMarkerInfo, base_req: DataRequest, ) -> Result<DataResponse<M>, DataError>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 
73 impl<M> DataProvider<M> for EmptyDataProvider
74 where
75     M: DataMarker,
76 {
load(&self, base_req: DataRequest) -> Result<DataResponse<M>, DataError>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 
82 impl<M> IterableDataProvider<M> for EmptyDataProvider
83 where
84     M: DataMarker,
85 {
iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError>86     fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
87         Ok(Default::default())
88     }
89 }
90 
91 impl<M> IterableDynamicDataProvider<M> for EmptyDataProvider
92 where
93     M: DynamicDataMarker,
94 {
iter_ids_for_marker( &self, _: DataMarkerInfo, ) -> Result<BTreeSet<DataIdentifierCow>, DataError>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")]
104 impl ExportableProvider for EmptyDataProvider {
supported_markers(&self) -> alloc::collections::BTreeSet<DataMarkerInfo>105     fn supported_markers(&self) -> alloc::collections::BTreeSet<DataMarkerInfo> {
106         Default::default()
107     }
108 }
109