| 1 |
#ifndef ICU4XDateFormatter_HPP |
| 2 |
#define ICU4XDateFormatter_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 "ICU4XDateFormatter.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XLocale; |
| 16 |
#include "ICU4XDateLength.hpp" |
| 17 |
class ICU4XDateFormatter; |
| 18 |
#include "ICU4XError.hpp" |
| 19 |
class ICU4XDate; |
| 20 |
class ICU4XIsoDate; |
| 21 |
class ICU4XDateTime; |
| 22 |
class ICU4XIsoDateTime; |
| 23 |
|
| 24 |
/** |
| 25 |
* A destruction policy for using ICU4XDateFormatter with std::unique_ptr. |
| 26 |
*/ |
| 27 |
struct ICU4XDateFormatterDeleter { |
| 28 |
void operator()(capi::ICU4XDateFormatter* l) const noexcept { |
| 29 |
capi::ICU4XDateFormatter_destroy(l); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
/** |
| 34 |
* An ICU4X DateFormatter object capable of formatting a [`ICU4XDate`] as a string, |
| 35 |
* using some calendar specified at runtime in the locale. |
| 36 |
* |
| 37 |
* See the [Rust documentation for `DateFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html) for more information. |
| 38 |
*/ |
| 39 |
class ICU4XDateFormatter { |
| 40 |
public: |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates a new [`ICU4XDateFormatter`] from locale data. |
| 44 |
* |
| 45 |
* See the [Rust documentation for `try_new_with_length`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.try_new_with_length) for more information. |
| 46 |
*/ |
| 47 |
static diplomat::result<ICU4XDateFormatter, ICU4XError> create_with_length(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDateLength date_length); |
| 48 |
|
| 49 |
/** |
| 50 |
* Formats a [`ICU4XDate`] to a string. |
| 51 |
* |
| 52 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 53 |
*/ |
| 54 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_date_to_writeable(const ICU4XDate& value, W& write) const; |
| 55 |
|
| 56 |
/** |
| 57 |
* Formats a [`ICU4XDate`] to a string. |
| 58 |
* |
| 59 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 60 |
*/ |
| 61 |
diplomat::result<std::string, ICU4XError> format_date(const ICU4XDate& value) const; |
| 62 |
|
| 63 |
/** |
| 64 |
* Formats a [`ICU4XIsoDate`] to a string. |
| 65 |
* |
| 66 |
* Will convert to this formatter's calendar first |
| 67 |
* |
| 68 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 69 |
*/ |
| 70 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_iso_date_to_writeable(const ICU4XIsoDate& value, W& write) const; |
| 71 |
|
| 72 |
/** |
| 73 |
* Formats a [`ICU4XIsoDate`] to a string. |
| 74 |
* |
| 75 |
* Will convert to this formatter's calendar first |
| 76 |
* |
| 77 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 78 |
*/ |
| 79 |
diplomat::result<std::string, ICU4XError> format_iso_date(const ICU4XIsoDate& value) const; |
| 80 |
|
| 81 |
/** |
| 82 |
* Formats a [`ICU4XDateTime`] to a string. |
| 83 |
* |
| 84 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 85 |
*/ |
| 86 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_datetime_to_writeable(const ICU4XDateTime& value, W& write) const; |
| 87 |
|
| 88 |
/** |
| 89 |
* Formats a [`ICU4XDateTime`] to a string. |
| 90 |
* |
| 91 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 92 |
*/ |
| 93 |
diplomat::result<std::string, ICU4XError> format_datetime(const ICU4XDateTime& value) const; |
| 94 |
|
| 95 |
/** |
| 96 |
* Formats a [`ICU4XIsoDateTime`] to a string. |
| 97 |
* |
| 98 |
* Will convert to this formatter's calendar first |
| 99 |
* |
| 100 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 101 |
*/ |
| 102 |
template<typename W> diplomat::result<std::monostate, ICU4XError> format_iso_datetime_to_writeable(const ICU4XIsoDateTime& value, W& write) const; |
| 103 |
|
| 104 |
/** |
| 105 |
* Formats a [`ICU4XIsoDateTime`] to a string. |
| 106 |
* |
| 107 |
* Will convert to this formatter's calendar first |
| 108 |
* |
| 109 |
* See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/struct.DateFormatter.html#method.format) for more information. |
| 110 |
*/ |
| 111 |
diplomat::result<std::string, ICU4XError> format_iso_datetime(const ICU4XIsoDateTime& value) const; |
| 112 |
inline const capi::ICU4XDateFormatter* AsFFI() const { return this->inner.get(); } |
| 113 |
inline capi::ICU4XDateFormatter* AsFFIMut() { return this->inner.get(); } |
| 114 |
inline explicit ICU4XDateFormatter(capi::ICU4XDateFormatter* i) : inner(i) {} |
| 115 |
ICU4XDateFormatter() = default; |
| 116 |
ICU4XDateFormatter(ICU4XDateFormatter&&) noexcept = default; |
| 117 |
ICU4XDateFormatter& operator=(ICU4XDateFormatter&& other) noexcept = default; |
| 118 |
private: |
| 119 |
std::unique_ptr<capi::ICU4XDateFormatter, ICU4XDateFormatterDeleter> inner; |
| 120 |
}; |
| 121 |
|
| 122 |
#include "ICU4XDataProvider.hpp" |
| 123 |
#include "ICU4XLocale.hpp" |
| 124 |
#include "ICU4XDate.hpp" |
| 125 |
#include "ICU4XIsoDate.hpp" |
| 126 |
#include "ICU4XDateTime.hpp" |
| 127 |
#include "ICU4XIsoDateTime.hpp" |
| 128 |
|
| 129 |
inline diplomat::result<ICU4XDateFormatter, ICU4XError> ICU4XDateFormatter::create_with_length(const ICU4XDataProvider& provider, const ICU4XLocale& locale, ICU4XDateLength date_length) { |
| 130 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_create_with_length(provider.AsFFI(), locale.AsFFI(), static_cast<capi::ICU4XDateLength>(date_length)); |
| 131 |
diplomat::result<ICU4XDateFormatter, ICU4XError> diplomat_result_out_value; |
| 132 |
if (diplomat_result_raw_out_value.is_ok) { |
| 133 |
diplomat_result_out_value = diplomat::Ok<ICU4XDateFormatter>(ICU4XDateFormatter(diplomat_result_raw_out_value.ok)); |
| 134 |
} else { |
| 135 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 136 |
} |
| 137 |
return diplomat_result_out_value; |
| 138 |
} |
| 139 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDateFormatter::format_date_to_writeable(const ICU4XDate& value, W& write) const { |
| 140 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 141 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_date(this->inner.get(), value.AsFFI(), &write_writer); |
| 142 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 143 |
if (diplomat_result_raw_out_value.is_ok) { |
| 144 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 145 |
} else { |
| 146 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 147 |
} |
| 148 |
return diplomat_result_out_value; |
| 149 |
} |
| 150 |
inline diplomat::result<std::string, ICU4XError> ICU4XDateFormatter::format_date(const ICU4XDate& value) const { |
| 151 |
std::string diplomat_writeable_string; |
| 152 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 153 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_date(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 154 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 155 |
if (diplomat_result_raw_out_value.is_ok) { |
| 156 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 157 |
} else { |
| 158 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 159 |
} |
| 160 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 161 |
} |
| 162 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDateFormatter::format_iso_date_to_writeable(const ICU4XIsoDate& value, W& write) const { |
| 163 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 164 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_iso_date(this->inner.get(), value.AsFFI(), &write_writer); |
| 165 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 166 |
if (diplomat_result_raw_out_value.is_ok) { |
| 167 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 168 |
} else { |
| 169 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 170 |
} |
| 171 |
return diplomat_result_out_value; |
| 172 |
} |
| 173 |
inline diplomat::result<std::string, ICU4XError> ICU4XDateFormatter::format_iso_date(const ICU4XIsoDate& value) const { |
| 174 |
std::string diplomat_writeable_string; |
| 175 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 176 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_iso_date(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 177 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 178 |
if (diplomat_result_raw_out_value.is_ok) { |
| 179 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 180 |
} else { |
| 181 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 182 |
} |
| 183 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 184 |
} |
| 185 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDateFormatter::format_datetime_to_writeable(const ICU4XDateTime& value, W& write) const { |
| 186 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 187 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_datetime(this->inner.get(), value.AsFFI(), &write_writer); |
| 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 |
inline diplomat::result<std::string, ICU4XError> ICU4XDateFormatter::format_datetime(const ICU4XDateTime& value) const { |
| 197 |
std::string diplomat_writeable_string; |
| 198 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 199 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_datetime(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 200 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 201 |
if (diplomat_result_raw_out_value.is_ok) { |
| 202 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 203 |
} else { |
| 204 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 205 |
} |
| 206 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 207 |
} |
| 208 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XDateFormatter::format_iso_datetime_to_writeable(const ICU4XIsoDateTime& value, W& write) const { |
| 209 |
capi::DiplomatWriteable write_writer = diplomat::WriteableTrait<W>::Construct(write); |
| 210 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_iso_datetime(this->inner.get(), value.AsFFI(), &write_writer); |
| 211 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 212 |
if (diplomat_result_raw_out_value.is_ok) { |
| 213 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 214 |
} else { |
| 215 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 216 |
} |
| 217 |
return diplomat_result_out_value; |
| 218 |
} |
| 219 |
inline diplomat::result<std::string, ICU4XError> ICU4XDateFormatter::format_iso_datetime(const ICU4XIsoDateTime& value) const { |
| 220 |
std::string diplomat_writeable_string; |
| 221 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 222 |
auto diplomat_result_raw_out_value = capi::ICU4XDateFormatter_format_iso_datetime(this->inner.get(), value.AsFFI(), &diplomat_writeable_out); |
| 223 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 224 |
if (diplomat_result_raw_out_value.is_ok) { |
| 225 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 226 |
} else { |
| 227 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 228 |
} |
| 229 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 230 |
} |
| 231 |
#endif |