| 1 |
#ifndef ICU4XTimeFormatter_HPP |
| 2 |
#define ICU4XTimeFormatter_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 "ICU4XTimeFormatter.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XLocale; |
| 16 |
#include "ICU4XTimeLength.hpp" |
| 17 |
class ICU4XTimeFormatter; |
| 18 |
#include "ICU4XError.hpp" |
| 19 |
class ICU4XTime; |
| 20 |
class ICU4XDateTime; |
| 21 |
class ICU4XIsoDateTime; |
| 22 |
|
| 23 |
/** |
| 24 |
* A destruction policy for using ICU4XTimeFormatter with std::unique_ptr. |
| 25 |
*/ |
| 26 |
struct ICU4XTimeFormatterDeleter { |
| 27 |
void operator()(capi::ICU4XTimeFormatter* l) const noexcept { |
| 28 |
capi::ICU4XTimeFormatter_destroy(l); |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
/** |
| 33 |
* An ICU4X TimeFormatter object capable of formatting an [`ICU4XTime`] type (and others) as a string |
| 34 |
* |
| 35 |
* See the [Rust documentation for `TimeFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html) for more information. |
| 36 |
*/ |
| 37 |
class ICU4XTimeFormatter { |
| 38 |
public: |
| 39 |
|
| 40 |
/** |
| 41 |
* Creates a new [`ICU4XTimeFormatter`] from locale data. |
| 42 |
* |
| 43 |
* See the [Rust documentation for `try_new_with_length`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.try_new_with_length) for more information. |
| 44 |
*/ |
| 45 |
static diplomat::result<ICU4XTimeFormatter, ICU4XError> create_with_length(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XTimeLength length); |
| 46 |
|
| 47 |
/** |
| 48 |
* Formats a [`ICU4XTime`] to a string. |
| 49 |
* |
| 50 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.format) for more information. |
| 51 |
*/ |
| 52 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_time_to_writeable(const ICU4XTime& value, W& write) const; |
| 53 |
|
| 54 |
/** |
| 55 |
* Formats a [`ICU4XTime`] to a string. |
| 56 |
* |
| 57 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.format) for more information. |
| 58 |
*/ |
| 59 |
diplomat::result<std::string, ICU4XError> format_time(const ICU4XTime& value) const; |
| 60 |
|
| 61 |
/** |
| 62 |
* Formats a [`ICU4XDateTime`] to a string. |
| 63 |
* |
| 64 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.format) for more information. |
| 65 |
*/ |
| 66 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_datetime_to_writeable(const ICU4XDateTime& value, W& write) const; |
| 67 |
|
| 68 |
/** |
| 69 |
* Formats a [`ICU4XDateTime`] to a string. |
| 70 |
* |
| 71 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.format) for more information. |
| 72 |
*/ |
| 73 |
diplomat::result<std::string, ICU4XError> format_datetime(const ICU4XDateTime& value) const; |
| 74 |
|
| 75 |
/** |
| 76 |
* Formats a [`ICU4XIsoDateTime`] to a string. |
| 77 |
* |
| 78 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.format) for more information. |
| 79 |
*/ |
| 80 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_iso_datetime_to_writeable(const ICU4XIsoDateTime& value, W& write) const; |
| 81 |
|
| 82 |
/** |
| 83 |
* Formats a [`ICU4XIsoDateTime`] to a string. |
| 84 |
* |
| 85 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.TimeFormatter.html#method.format) for more information. |
| 86 |
*/ |
| 87 |
diplomat::result<std::string, ICU4XError> format_iso_datetime(const ICU4XIsoDateTime& value) const; |
| 88 |
inline const capi::ICU4XTimeFormatter* AsFFI() const { return this->inner.get(); } |
| 89 |
inline capi::ICU4XTimeFormatter* AsFFIMut() { return this->inner.get(); } |
| 90 |
inline explicit ICU4XTimeFormatter(capi::ICU4XTimeFormatter* i) : inner(i) {} |
| 91 |
ICU4XTimeFormatter() = default; |
| 92 |
ICU4XTimeFormatter(ICU4XTimeFormatter&&) noexcept = default; |
| 93 |
ICU4XTimeFormatter& operator=(ICU4XTimeFormatter&& other) noexcept = default; |
| 94 |
private: |
| 95 |
std::unique_ptr<capi::ICU4XTimeFormatter, ICU4XTimeFormatterDeleter> inner; |
| 96 |
}; |
| 97 |
|
| 98 |
#include "ICU4XDataProvider.hpp" |
| 99 |
#include "ICU4XLocale.hpp" |
| 100 |
#include "ICU4XTime.hpp" |
| 101 |
#include "ICU4XDateTime.hpp" |
| 102 |
#include "ICU4XIsoDateTime.hpp" |
| 103 |
|
| 104 |
inline diplomat::result<ICU4XTimeFormatter, ICU4XError> ICU4XTimeFormatter::create_with_length(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XTimeLength length) { |
| 105 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_create_with_length(provider.AsFFI(), locale.AsFFI(), static_cast<capi::ICU4XTimeLength>(length)); |
| 106 |
diplomat::result<ICU4XTimeFormatter, ICU4XError> diplomat_result_out_value; |
| 107 |
if (diplomat_result_raw_out_value.is_ok) { |
| 108 |
diplomat_result_out_value = diplomat::Ok<ICU4XTimeFormatter>(ICU4XTimeFormatter(diplomat_result_raw_out_value.ok)); |
| 109 |
} else { |
| 110 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 111 |
} |
| 112 |
return diplomat_result_out_value; |
| 113 |
} |
| 114 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XTimeFormatter::format_time_to_writeable(const ICU4XTime& value, W& write) const { |
| 115 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 116 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_format_time(this->inner.get(), value.AsFFI(), &write_writer); |
| 117 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 118 |
if (diplomat_result_raw_out_value.is_ok) { |
| 119 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 120 |
} else { |
| 121 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 122 |
} |
| 123 |
return diplomat_result_out_value; |
| 124 |
} |
| 125 |
inline diplomat::result<std::string, ICU4XError> ICU4XTimeFormatter::format_time(const ICU4XTime& value) const { |
| 126 |
std::string diplomat_writeable_string; |
| 127 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 128 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_format_time(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 129 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 130 |
if (diplomat_result_raw_out_value.is_ok) { |
| 131 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 132 |
} else { |
| 133 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 134 |
} |
| 135 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 136 |
} |
| 137 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XTimeFormatter::format_datetime_to_writeable(const ICU4XDateTime& value, W& write) const { |
| 138 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 139 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_format_datetime(this->inner.get(), value.AsFFI(), &write_writer); |
| 140 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 141 |
if (diplomat_result_raw_out_value.is_ok) { |
| 142 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 143 |
} else { |
| 144 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 145 |
} |
| 146 |
return diplomat_result_out_value; |
| 147 |
} |
| 148 |
inline diplomat::result<std::string, ICU4XError> ICU4XTimeFormatter::format_datetime(const ICU4XDateTime& value) const { |
| 149 |
std::string diplomat_writeable_string; |
| 150 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 151 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_format_datetime(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 152 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 153 |
if (diplomat_result_raw_out_value.is_ok) { |
| 154 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 155 |
} else { |
| 156 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 157 |
} |
| 158 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 159 |
} |
| 160 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XTimeFormatter::format_iso_datetime_to_writeable(const ICU4XIsoDateTime& value, W& write) const { |
| 161 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 162 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_format_iso_datetime(this->inner.get(), value.AsFFI(), &write_writer); |
| 163 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 164 |
if (diplomat_result_raw_out_value.is_ok) { |
| 165 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 166 |
} else { |
| 167 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 168 |
} |
| 169 |
return diplomat_result_out_value; |
| 170 |
} |
| 171 |
inline diplomat::result<std::string, ICU4XError> ICU4XTimeFormatter::format_iso_datetime(const ICU4XIsoDateTime& value) const { |
| 172 |
std::string diplomat_writeable_string; |
| 173 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 174 |
auto diplomat_result_raw_out_value = capi::ICU4XTimeFormatter_format_iso_datetime(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 175 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 176 |
if (diplomat_result_raw_out_value.is_ok) { |
| 177 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 178 |
} else { |
| 179 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 180 |
} |
| 181 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 182 |
} |
| 183 |
#endif |