| 1 |
#ifndef ICU4XDataProvider_HPP |
| 2 |
#define ICU4XDataProvider_HPP |
| 3 |
#include <stdint.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <algorithm> |
| 7 |
#include <memory> |
| 8 |
#include <variant> |
| 9 |
#include <optional> |
| 10 |
#include "diplomat_runtime.hpp" |
| 11 |
|
| 12 |
#include "ICU4XDataProvider.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
#include "ICU4XError.hpp" |
| 16 |
class ICU4XLocaleFallbacker; |
| 17 |
|
| 18 |
/** |
| 19 |
* A destruction policy for using ICU4XDataProvider with std::unique_ptr. |
| 20 |
*/ |
| 21 |
struct ICU4XDataProviderDeleter { |
| 22 |
void operator()(capi::ICU4XDataProvider* l) const noexcept { |
| 23 |
capi::ICU4XDataProvider_destroy(l); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
/** |
| 28 |
* An ICU4X data provider, capable of loading ICU4X data keys from some source. |
| 29 |
* |
| 30 |
* See the [Rust documentation for `icu_provider`](https://docs.rs/icu_provider/latest/icu_provider/index.html) for more information. |
| 31 |
*/ |
| 32 |
class ICU4XDataProvider { |
| 33 |
public: |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructs an [`ICU4XDataProvider`] that uses compiled data. |
| 37 |
* |
| 38 |
* Requires the `compiled_data` feature. |
| 39 |
* |
| 40 |
* This provider cannot be modified or combined with other providers, so `enable_fallback`, |
| 41 |
* `enabled_fallback_with`, `fork_by_locale`, and `fork_by_key` will return `Err`s. |
| 42 |
*/ |
| 43 |
static ICU4XDataProvider create_compiled(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructs an `FsDataProvider` and returns it as an [`ICU4XDataProvider`]. |
| 47 |
* Requires the `provider_fs` Cargo feature. |
| 48 |
* Not supported in WASM. |
| 49 |
* |
| 50 |
* See the [Rust documentation for `FsDataProvider`](https://docs.rs/icu_provider_fs/latest/icu_provider_fs/struct.FsDataProvider.html) for more information. |
| 51 |
*/ |
| 52 |
static diplomat::result<ICU4XDataProvider, ICU4XError> create_fs(const std::string_view path); |
| 53 |
|
| 54 |
/** |
| 55 |
* Deprecated |
| 56 |
* |
| 57 |
* Use `create_compiled()`. |
| 58 |
*/ |
| 59 |
static ICU4XDataProvider create_test(); |
| 60 |
|
| 61 |
/** |
| 62 |
* Constructs a `BlobDataProvider` and returns it as an [`ICU4XDataProvider`]. |
| 63 |
* |
| 64 |
* See the [Rust documentation for `BlobDataProvider`](https://docs.rs/icu_provider_blob/latest/icu_provider_blob/struct.BlobDataProvider.html) for more information. |
| 65 |
* |
| 66 |
* Lifetimes: `blob` must live for the duration of the program. |
| 67 |
*/ |
| 68 |
static diplomat::result<ICU4XDataProvider, ICU4XError> create_from_byte_slice(const diplomat::span<const uint8_t> blob); |
| 69 |
|
| 70 |
/** |
| 71 |
* Constructs an empty [`ICU4XDataProvider`]. |
| 72 |
* |
| 73 |
* See the [Rust documentation for `EmptyDataProvider`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/empty/struct.EmptyDataProvider.html) for more information. |
| 74 |
*/ |
| 75 |
static ICU4XDataProvider create_empty(); |
| 76 |
|
| 77 |
/** |
| 78 |
* Creates a provider that tries the current provider and then, if the current provider |
| 79 |
* doesn't support the data key, another provider `other`. |
| 80 |
* |
| 81 |
* This takes ownership of the `other` provider, leaving an empty provider in its place. |
| 82 |
* |
| 83 |
* The providers must be the same type (Any or Buffer). This condition is satisfied if |
| 84 |
* both providers originate from the same constructor, such as `create_from_byte_slice` |
| 85 |
* or `create_fs`. If the condition is not upheld, a runtime error occurs. |
| 86 |
* |
| 87 |
* See the [Rust documentation for `ForkByKeyProvider`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fork/type.ForkByKeyProvider.html) for more information. |
| 88 |
*/ |
| 89 |
diplomat::result<std::monostate, ICU4XError> fork_by_key(ICU4XDataProvider& other); |
| 90 |
|
| 91 |
/** |
| 92 |
* Same as `fork_by_key` but forks by locale instead of key. |
| 93 |
* |
| 94 |
* See the [Rust documentation for `MissingLocalePredicate`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fork/predicates/struct.MissingLocalePredicate.html) for more information. |
| 95 |
*/ |
| 96 |
diplomat::result<std::monostate, ICU4XError> fork_by_locale(ICU4XDataProvider& other); |
| 97 |
|
| 98 |
/** |
| 99 |
* Enables locale fallbacking for data requests made to this provider. |
| 100 |
* |
| 101 |
* Note that the test provider (from `create_test`) already has fallbacking enabled. |
| 102 |
* |
| 103 |
* See the [Rust documentation for `try_new`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.try_new) for more information. |
| 104 |
* |
| 105 |
* Additional information: [1](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html) |
| 106 |
*/ |
| 107 |
diplomat::result<std::monostate, ICU4XError> enable_locale_fallback(); |
| 108 |
|
| 109 |
/** |
| 110 |
* See the [Rust documentation for `new_with_fallbacker`](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html#method.new_with_fallbacker) for more information. |
| 111 |
* |
| 112 |
* Additional information: [1](https://docs.rs/icu_provider_adapters/latest/icu_provider_adapters/fallback/struct.LocaleFallbackProvider.html) |
| 113 |
*/ |
| 114 |
diplomat::result<std::monostate, ICU4XError> enable_locale_fallback_with(const ICU4XLocaleFallbacker& fallbacker); |
| 115 |
inline const capi::ICU4XDataProvider* AsFFI() const { return this->inner.get(); } |
| 116 |
inline capi::ICU4XDataProvider* AsFFIMut() { return this->inner.get(); } |
| 117 |
inline explicit ICU4XDataProvider(capi::ICU4XDataProvider* i) : inner(i) {} |
| 118 |
ICU4XDataProvider() = default; |
| 119 |
ICU4XDataProvider(ICU4XDataProvider&&) noexcept = default; |
| 120 |
ICU4XDataProvider& operator=(ICU4XDataProvider&& other) noexcept = default; |
| 121 |
private: |
| 122 |
std::unique_ptr<capi::ICU4XDataProvider, ICU4XDataProviderDeleter> inner; |
| 123 |
}; |
| 124 |
|
| 125 |
#include "ICU4XLocaleFallbacker.hpp" |
| 126 |
|
| 127 |
inline ICU4XDataProvider ICU4XDataProvider::create_compiled() { |
| 128 |
return ICU4XDataProvider(capi::ICU4XDataProvider_create_compiled()); |
| 129 |
} |
| 130 |
inline diplomat::result<ICU4XDataProvider, ICU4XError> ICU4XDataProvider::create_fs(const std::string_view path) { |
| 131 |
auto diplomat_result_raw_out_value = capi::ICU4XDataProvider_create_fs(path.data(), path.size()); |
| 132 |
diplomat::result<ICU4XDataProvider, ICU4XError> diplomat_result_out_value; |
| 133 |
if (diplomat_result_raw_out_value.is_ok) { |
| 134 |
diplomat_result_out_value = diplomat::Ok<ICU4XDataProvider>(ICU4XDataProvider(diplomat_result_raw_out_value.ok)); |
| 135 |
} else { |
| 136 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 137 |
} |
| 138 |
return diplomat_result_out_value; |
| 139 |
} |
| 140 |
inline ICU4XDataProvider ICU4XDataProvider::create_test() { |
| 141 |
return ICU4XDataProvider(capi::ICU4XDataProvider_create_test()); |
| 142 |
} |
| 143 |
inline diplomat::result<ICU4XDataProvider, ICU4XError> ICU4XDataProvider::create_from_byte_slice(const diplomat::span<const uint8_t> blob) { |
| 144 |
auto diplomat_result_raw_out_value = capi::ICU4XDataProvider_create_from_byte_slice(blob.data(), blob.size()); |
| 145 |
diplomat::result<ICU4XDataProvider, ICU4XError> diplomat_result_out_value; |
| 146 |
if (diplomat_result_raw_out_value.is_ok) { |
| 147 |
diplomat_result_out_value = diplomat::Ok<ICU4XDataProvider>(ICU4XDataProvider(diplomat_result_raw_out_value.ok)); |
| 148 |
} else { |
| 149 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 150 |
} |
| 151 |
return diplomat_result_out_value; |
| 152 |
} |
| 153 |
inline ICU4XDataProvider ICU4XDataProvider::create_empty() { |
| 154 |
return ICU4XDataProvider(capi::ICU4XDataProvider_create_empty()); |
| 155 |
} |
| 156 |
inline diplomat::result<std::monostate, ICU4XError> ICU4XDataProvider::fork_by_key(ICU4XDataProvider& other) { |
| 157 |
auto diplomat_result_raw_out_value = capi::ICU4XDataProvider_fork_by_key(this->inner.get(), other.AsFFIMut()); |
| 158 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 159 |
if (diplomat_result_raw_out_value.is_ok) { |
| 160 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 161 |
} else { |
| 162 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 163 |
} |
| 164 |
return diplomat_result_out_value; |
| 165 |
} |
| 166 |
inline diplomat::result<std::monostate, ICU4XError> ICU4XDataProvider::fork_by_locale(ICU4XDataProvider& other) { |
| 167 |
auto diplomat_result_raw_out_value = capi::ICU4XDataProvider_fork_by_locale(this->inner.get(), other.AsFFIMut()); |
| 168 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 169 |
if (diplomat_result_raw_out_value.is_ok) { |
| 170 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 171 |
} else { |
| 172 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 173 |
} |
| 174 |
return diplomat_result_out_value; |
| 175 |
} |
| 176 |
inline diplomat::result<std::monostate, ICU4XError> ICU4XDataProvider::enable_locale_fallback() { |
| 177 |
auto diplomat_result_raw_out_value = capi::ICU4XDataProvider_enable_locale_fallback(this->inner.get()); |
| 178 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 179 |
if (diplomat_result_raw_out_value.is_ok) { |
| 180 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 181 |
} else { |
| 182 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 183 |
} |
| 184 |
return diplomat_result_out_value; |
| 185 |
} |
| 186 |
inline diplomat::result<std::monostate, ICU4XError> ICU4XDataProvider::enable_locale_fallback_with(const ICU4XLocaleFallbacker& fallbacker) { |
| 187 |
auto diplomat_result_raw_out_value = capi::ICU4XDataProvider_enable_locale_fallback_with(this->inner.get(), fallbacker.AsFFI()); |
| 188 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 189 |
if (diplomat_result_raw_out_value.is_ok) { |
| 190 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 191 |
} else { |
| 192 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 193 |
} |
| 194 |
return diplomat_result_out_value; |
| 195 |
} |
| 196 |
#endif |