| 1 |
#ifndef ICU4XLocaleDisplayNamesFormatter_HPP |
| 2 |
#define ICU4XLocaleDisplayNamesFormatter_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 "ICU4XLocaleDisplayNamesFormatter.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XLocale; |
| 16 |
struct ICU4XDisplayNamesOptionsV1; |
| 17 |
class ICU4XLocaleDisplayNamesFormatter; |
| 18 |
#include "ICU4XError.hpp" |
| 19 |
|
| 20 |
/** |
| 21 |
* A destruction policy for using ICU4XLocaleDisplayNamesFormatter with std::unique_ptr. |
| 22 |
*/ |
| 23 |
struct ICU4XLocaleDisplayNamesFormatterDeleter { |
| 24 |
void operator()(capi::ICU4XLocaleDisplayNamesFormatter* l) const noexcept { |
| 25 |
capi::ICU4XLocaleDisplayNamesFormatter_destroy(l); |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
/** |
| 30 |
* See the [Rust documentation for `LocaleDisplayNamesFormatter`](https://docs.rs/icu/latest/icu/displaynames/struct.LocaleDisplayNamesFormatter.html) for more information. |
| 31 |
*/ |
| 32 |
class ICU4XLocaleDisplayNamesFormatter { |
| 33 |
public: |
| 34 |
|
| 35 |
/** |
| 36 |
* Creates a new `LocaleDisplayNamesFormatter` from locale data and an options bag. |
| 37 |
* |
| 38 |
* See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/displaynames/struct.LocaleDisplayNamesFormatter.html#method.try_new) for more information. |
| 39 |
*/ |
| 40 |
static diplomat::result<ICU4XLocaleDisplayNamesFormatter, ICU4XError> create(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDisplayNamesOptionsV1 options); |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the locale-specific display name of a locale. |
| 44 |
* |
| 45 |
* See the [Rust documentation for `of`](https://docs.rs/icu/latest/icu/displaynames/struct.LocaleDisplayNamesFormatter.html#method.of) for more information. |
| 46 |
*/ |
| 47 |
template<typename W> diplomat::result<std::monostate, ICU4XError> of_to_writeable(const ICU4XLocale& locale, W& write) const; |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns the locale-specific display name of a locale. |
| 51 |
* |
| 52 |
* See the [Rust documentation for `of`](https://docs.rs/icu/latest/icu/displaynames/struct.LocaleDisplayNamesFormatter.html#method.of) for more information. |
| 53 |
*/ |
| 54 |
diplomat::result<std::string, ICU4XError> of(const ICU4XLocale& locale) const; |
| 55 |
inline const capi::ICU4XLocaleDisplayNamesFormatter* AsFFI() const { return this->inner.get(); } |
| 56 |
inline capi::ICU4XLocaleDisplayNamesFormatter* AsFFIMut() { return this->inner.get(); } |
| 57 |
inline explicit ICU4XLocaleDisplayNamesFormatter(capi::ICU4XLocaleDisplayNamesFormatter* i) : inner(i) {} |
| 58 |
ICU4XLocaleDisplayNamesFormatter() = default; |
| 59 |
ICU4XLocaleDisplayNamesFormatter(ICU4XLocaleDisplayNamesFormatter&&) noexcept = default; |
| 60 |
ICU4XLocaleDisplayNamesFormatter& operator=(ICU4XLocaleDisplayNamesFormatter&& other) noexcept = default; |
| 61 |
private: |
| 62 |
std::unique_ptr<capi::ICU4XLocaleDisplayNamesFormatter, ICU4XLocaleDisplayNamesFormatterDeleter> inner; |
| 63 |
}; |
| 64 |
|
| 65 |
#include "ICU4XDataProvider.hpp" |
| 66 |
#include "ICU4XLocale.hpp" |
| 67 |
#include "ICU4XDisplayNamesOptionsV1.hpp" |
| 68 |
|
| 69 |
inline diplomat::result<ICU4XLocaleDisplayNamesFormatter, ICU4XError> ICU4XLocaleDisplayNamesFormatter::create(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDisplayNamesOptionsV1 options) { |
| 70 |
ICU4XDisplayNamesOptionsV1 diplomat_wrapped_struct_options = options; |
| 71 |
auto diplomat_result_raw_out_value = capi::ICU4XLocaleDisplayNamesFormatter_create(provider.AsFFI(), locale.AsFFI(), capi::ICU4XDisplayNamesOptionsV1{ .style = static_cast<capi::ICU4XDisplayNamesStyle>(diplomat_wrapped_struct_options.style), .fallback = static_cast<capi::ICU4XDisplayNamesFallback>(diplomat_wrapped_struct_options.fallback), .language_display = static_cast<capi::ICU4XLanguageDisplay>(diplomat_wrapped_struct_options.language_display) }); |
| 72 |
diplomat::result<ICU4XLocaleDisplayNamesFormatter, ICU4XError> diplomat_result_out_value; |
| 73 |
if (diplomat_result_raw_out_value.is_ok) { |
| 74 |
diplomat_result_out_value = diplomat::Ok<ICU4XLocaleDisplayNamesFormatter>(ICU4XLocaleDisplayNamesFormatter(diplomat_result_raw_out_value.ok)); |
| 75 |
} else { |
| 76 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 77 |
} |
| 78 |
return diplomat_result_out_value; |
| 79 |
} |
| 80 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XLocaleDisplayNamesFormatter::of_to_writeable(const ICU4XLocale& locale, W& write) const { |
| 81 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 82 |
auto diplomat_result_raw_out_value = capi::ICU4XLocaleDisplayNamesFormatter_of(this->inner.get(), locale.AsFFI(), &write_writer); |
| 83 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 84 |
if (diplomat_result_raw_out_value.is_ok) { |
| 85 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 86 |
} else { |
| 87 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 88 |
} |
| 89 |
return diplomat_result_out_value; |
| 90 |
} |
| 91 |
inline diplomat::result<std::string, ICU4XError> ICU4XLocaleDisplayNamesFormatter::of(const ICU4XLocale& locale) const { |
| 92 |
std::string diplomat_writeable_string; |
| 93 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 94 |
auto diplomat_result_raw_out_value = capi::ICU4XLocaleDisplayNamesFormatter_of(this->inner.get(), locale.AsFFI(), &diplomat_writeable_out); |
| 95 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 96 |
if (diplomat_result_raw_out_value.is_ok) { |
| 97 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 98 |
} else { |
| 99 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 100 |
} |
| 101 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 102 |
} |
| 103 |
#endif |