Debian Codegraph @

third_party/rust/icu_provider_adapters/src/empty.rs

raw
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 icu_provider::prelude::*;
10
11 /// A data provider that always returns an error.
12 ///
13 /// The returned error kind is configurable.
14 ///
15 /// # Examples
16 ///
17 /// ```
18 /// use icu_provider::hello_world::HelloWorldV1Marker;
19 /// use icu_provider::prelude::*;
20 /// use icu_provider_adapters::empty::EmptyDataProvider;
21 ///
22 /// let provider = EmptyDataProvider::new();
23 ///
24 /// assert!(matches!(
25 /// provider.load_any(HelloWorldV1Marker::KEY, Default::default()),
26 /// Err(DataError {
27 /// kind: DataErrorKind::MissingDataKey,
28 /// ..
29 /// })
30 /// ));
31 /// ```
32 #[derive(Debug)]
33 pub struct EmptyDataProvider {
34 error_kind: DataErrorKind,
35 }
36
37 impl Default for EmptyDataProvider {
38 fn default() -> Self {
39 Self::new()
40 }
41 }
42
43 impl EmptyDataProvider {
44 /// Creates a data provider that always returns [`DataErrorKind::MissingDataKey`].
45 pub fn new() -> Self {
46 Self {
47 error_kind: DataErrorKind::MissingDataKey,
48 }
49 }
50
51 /// Creates a data provider that always returns the specified error kind.
52 pub fn new_with_error_kind(error_kind: DataErrorKind) -> Self {
53 Self { error_kind }
54 }
55 }
56
57 impl AnyProvider for EmptyDataProvider {
58 fn load_any(&self, key: DataKey, base_req: DataRequest) -> Result<AnyResponse, DataError> {
59 Err(self.error_kind.with_req(key, base_req))
60 }
61 }
62
63 impl BufferProvider for EmptyDataProvider {
64 fn load_buffer(
65 &self,
66 key: DataKey,
67 base_req: DataRequest,
68 ) -> Result<DataResponse<BufferMarker>, DataError> {
69 Err(self.error_kind.with_req(key, base_req))
70 }
71 }
72
73 impl<M> DynamicDataProvider<M> for EmptyDataProvider
74 where
75 M: DataMarker,
76 {
77 fn load_data(&self, key: DataKey, base_req: DataRequest) -> Result<DataResponse<M>, DataError> {
78 Err(self.error_kind.with_req(key, base_req))
79 }
80 }
81
82 impl<M> DataProvider<M> for EmptyDataProvider
83 where
84 M: KeyedDataMarker,
85 {
86 fn load(&self, base_req: DataRequest) -> Result<DataResponse<M>, DataError> {
87 Err(self.error_kind.with_req(M::KEY, base_req))
88 }
89 }
90
91 #[cfg(feature = "datagen")]
92 impl<M> icu_provider::datagen::IterableDataProvider<M> for EmptyDataProvider
93 where
94 M: KeyedDataMarker,
95 {
96 fn supported_locales(&self) -> Result<alloc::vec::Vec<DataLocale>, DataError> {
97 Ok(vec![])
98 }
99 }
100
101 #[cfg(feature = "datagen")]
102 impl<M> icu_provider::datagen::IterableDynamicDataProvider<M> for EmptyDataProvider
103 where
104 M: DataMarker,
105 {
106 fn supported_locales_for_key(
107 &self,
108 _: DataKey,
109 ) -> Result<alloc::vec::Vec<DataLocale>, DataError> {
110 Ok(vec![])
111 }
112 }