| 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 |
use icu_timezone::CustomTimeZone; |
| 6 |
|
| 7 |
#[diplomat::bridge] |
| 8 |
pub mod ffi { |
| 9 |
use crate::errors::ffi::ICU4XError; |
| 10 |
use alloc::boxed::Box; |
| 11 |
use core::fmt::Write; |
| 12 |
use icu_timezone::CustomTimeZone; |
| 13 |
use icu_timezone::GmtOffset; |
| 14 |
use icu_timezone::ZoneVariant; |
| 15 |
|
| 16 |
#[diplomat::opaque] |
| 17 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone, Struct)] |
| 18 |
pub struct ICU4XCustomTimeZone(pub CustomTimeZone); |
| 19 |
|
| 20 |
impl ICU4XCustomTimeZone { |
| 21 |
/// Creates a time zone from an offset string. |
| 22 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::from_str, FnInStruct)] |
| 23 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::try_from_bytes, FnInStruct, hidden)] |
| 24 |
#[diplomat::rust_link(icu::timezone::GmtOffset::from_str, FnInStruct, hidden)] |
| 25 |
#[diplomat::rust_link(icu::timezone::GmtOffset::try_from_bytes, FnInStruct, hidden)] |
| 26 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_string")] |
| 27 |
pub fn create_from_string(s: &DiplomatStr) -> Result<Box<ICU4XCustomTimeZone>, ICU4XError> { |
| 28 |
Ok(Box::new(ICU4XCustomTimeZone::from( |
| 29 |
CustomTimeZone::try_from_bytes(s)?, |
| 30 |
))) |
| 31 |
} |
| 32 |
|
| 33 |
/// Creates a time zone with no information. |
| 34 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::new_empty, FnInStruct)] |
| 35 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "empty")] |
| 36 |
pub fn create_empty() -> Box<ICU4XCustomTimeZone> { |
| 37 |
Box::new(CustomTimeZone::new_empty().into()) |
| 38 |
} |
| 39 |
|
| 40 |
/// Creates a time zone for UTC. |
| 41 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::utc, FnInStruct)] |
| 42 |
#[diplomat::rust_link(icu::timezone::GmtOffset::utc, FnInStruct, hidden)] |
| 43 |
#[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "utc")] |
| 44 |
pub fn create_utc() -> Box<ICU4XCustomTimeZone> { |
| 45 |
Box::new(CustomTimeZone::utc().into()) |
| 46 |
} |
| 47 |
|
| 48 |
/// Sets the `gmt_offset` field from offset seconds. |
| 49 |
/// |
| 50 |
/// Errors if the offset seconds are out of range. |
| 51 |
#[diplomat::rust_link(icu::timezone::GmtOffset::try_from_offset_seconds, FnInStruct)] |
| 52 |
#[diplomat::rust_link(icu::timezone::GmtOffset, Struct, compact)] |
| 53 |
#[diplomat::rust_link( |
| 54 |
icu::timezone::GmtOffset::from_offset_seconds_unchecked, |
| 55 |
FnInStruct, |
| 56 |
hidden |
| 57 |
)] |
| 58 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::new_with_offset, FnInStruct, hidden)] |
| 59 |
pub fn try_set_gmt_offset_seconds( |
| 60 |
&mut self, |
| 61 |
offset_seconds: i32, |
| 62 |
) -> Result<(), ICU4XError> { |
| 63 |
self.0.gmt_offset = Some(GmtOffset::try_from_offset_seconds(offset_seconds)?); |
| 64 |
Ok(()) |
| 65 |
} |
| 66 |
|
| 67 |
/// Clears the `gmt_offset` field. |
| 68 |
#[diplomat::rust_link(icu::timezone::GmtOffset::offset_seconds, FnInStruct)] |
| 69 |
#[diplomat::rust_link(icu::timezone::GmtOffset, Struct, compact)] |
| 70 |
pub fn clear_gmt_offset(&mut self) { |
| 71 |
self.0.gmt_offset.take(); |
| 72 |
} |
| 73 |
|
| 74 |
/// Returns the value of the `gmt_offset` field as offset seconds. |
| 75 |
/// |
| 76 |
/// Errors if the `gmt_offset` field is empty. |
| 77 |
#[diplomat::rust_link(icu::timezone::GmtOffset::offset_seconds, FnInStruct)] |
| 78 |
#[diplomat::rust_link(icu::timezone::GmtOffset, Struct, compact)] |
| 79 |
#[diplomat::attr(supports = accessors, getter)] |
| 80 |
pub fn gmt_offset_seconds(&self) -> Result<i32, ICU4XError> { |
| 81 |
self.0 |
| 82 |
.gmt_offset |
| 83 |
.ok_or(ICU4XError::TimeZoneMissingInputError) |
| 84 |
.map(GmtOffset::offset_seconds) |
| 85 |
} |
| 86 |
|
| 87 |
/// Returns whether the `gmt_offset` field is positive. |
| 88 |
/// |
| 89 |
/// Errors if the `gmt_offset` field is empty. |
| 90 |
#[diplomat::rust_link(icu::timezone::GmtOffset::is_positive, FnInStruct)] |
| 91 |
#[diplomat::attr(supports = accessors, getter)] |
| 92 |
pub fn is_gmt_offset_positive(&self) -> Result<bool, ICU4XError> { |
| 93 |
self.0 |
| 94 |
.gmt_offset |
| 95 |
.ok_or(ICU4XError::TimeZoneMissingInputError) |
| 96 |
.map(GmtOffset::is_positive) |
| 97 |
} |
| 98 |
|
| 99 |
/// Returns whether the `gmt_offset` field is zero. |
| 100 |
/// |
| 101 |
/// Errors if the `gmt_offset` field is empty (which is not the same as zero). |
| 102 |
#[diplomat::rust_link(icu::timezone::GmtOffset::is_zero, FnInStruct)] |
| 103 |
#[diplomat::attr(supports = accessors, getter)] |
| 104 |
pub fn is_gmt_offset_zero(&self) -> Result<bool, ICU4XError> { |
| 105 |
self.0 |
| 106 |
.gmt_offset |
| 107 |
.ok_or(ICU4XError::TimeZoneMissingInputError) |
| 108 |
.map(GmtOffset::is_zero) |
| 109 |
} |
| 110 |
|
| 111 |
/// Returns whether the `gmt_offset` field has nonzero minutes. |
| 112 |
/// |
| 113 |
/// Errors if the `gmt_offset` field is empty. |
| 114 |
#[diplomat::rust_link(icu::timezone::GmtOffset::has_minutes, FnInStruct)] |
| 115 |
#[diplomat::attr(supports = accessors, getter)] |
| 116 |
pub fn gmt_offset_has_minutes(&self) -> Result<bool, ICU4XError> { |
| 117 |
self.0 |
| 118 |
.gmt_offset |
| 119 |
.ok_or(ICU4XError::TimeZoneMissingInputError) |
| 120 |
.map(GmtOffset::has_minutes) |
| 121 |
} |
| 122 |
|
| 123 |
/// Returns whether the `gmt_offset` field has nonzero seconds. |
| 124 |
/// |
| 125 |
/// Errors if the `gmt_offset` field is empty. |
| 126 |
#[diplomat::rust_link(icu::timezone::GmtOffset::has_seconds, FnInStruct)] |
| 127 |
#[diplomat::attr(supports = accessors, getter)] |
| 128 |
pub fn gmt_offset_has_seconds(&self) -> Result<bool, ICU4XError> { |
| 129 |
self.0 |
| 130 |
.gmt_offset |
| 131 |
.ok_or(ICU4XError::TimeZoneMissingInputError) |
| 132 |
.map(GmtOffset::has_seconds) |
| 133 |
} |
| 134 |
|
| 135 |
/// Sets the `time_zone_id` field from a BCP-47 string. |
| 136 |
/// |
| 137 |
/// Errors if the string is not a valid BCP-47 time zone ID. |
| 138 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::time_zone_id, StructField)] |
| 139 |
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)] |
| 140 |
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::from_str, FnInStruct, hidden)] |
| 141 |
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::deref, FnInStruct, hidden)] |
| 142 |
#[diplomat::rust_link( |
| 143 |
icu::timezone::TimeZoneBcp47Id::Target, |
| 144 |
AssociatedTypeInStruct, |
| 145 |
hidden |
| 146 |
)] |
| 147 |
pub fn try_set_time_zone_id(&mut self, id: &DiplomatStr) -> Result<(), ICU4XError> { |
| 148 |
self.0.time_zone_id = Some(icu_timezone::TimeZoneBcp47Id( |
| 149 |
tinystr::TinyAsciiStr::from_bytes(id) |
| 150 |
.map_err(|_| ICU4XError::TimeZoneInvalidIdError)?, |
| 151 |
)); |
| 152 |
Ok(()) |
| 153 |
} |
| 154 |
|
| 155 |
/// Sets the `time_zone_id` field from an IANA string by looking up |
| 156 |
/// the corresponding BCP-47 string. |
| 157 |
/// |
| 158 |
/// Errors if the string is not a valid BCP-47 time zone ID. |
| 159 |
#[diplomat::rust_link(icu::timezone::IanaToBcp47MapperBorrowed::get, FnInStruct)] |
| 160 |
pub fn try_set_iana_time_zone_id( |
| 161 |
&mut self, |
| 162 |
mapper: &crate::iana_bcp47_mapper::ffi::ICU4XIanaToBcp47Mapper, |
| 163 |
id: &DiplomatStr, |
| 164 |
) -> Result<(), ICU4XError> { |
| 165 |
let id = core::str::from_utf8(id).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?; |
| 166 |
self.0.time_zone_id = Some( |
| 167 |
mapper |
| 168 |
.0 |
| 169 |
.as_borrowed() |
| 170 |
.get(id) |
| 171 |
.ok_or(ICU4XError::TimeZoneInvalidIdError)?, |
| 172 |
); |
| 173 |
Ok(()) |
| 174 |
} |
| 175 |
|
| 176 |
// *** TODO: in 2.0 please replace try_set_iana_time_zone_id with try_set_iana_time_zone_id_2 *** |
| 177 |
|
| 178 |
/// Sets the `time_zone_id` field from an IANA string by looking up |
| 179 |
/// the corresponding BCP-47 string. |
| 180 |
/// |
| 181 |
/// Errors if the string is not a valid BCP-47 time zone ID. |
| 182 |
pub fn try_set_iana_time_zone_id_2( |
| 183 |
&mut self, |
| 184 |
mapper: &crate::timezone_mapper::ffi::ICU4XTimeZoneIdMapper, |
| 185 |
id: &DiplomatStr, |
| 186 |
) -> Result<(), ICU4XError> { |
| 187 |
self.0.time_zone_id = Some( |
| 188 |
mapper |
| 189 |
.0 |
| 190 |
.as_borrowed() |
| 191 |
.iana_bytes_to_bcp47(id) |
| 192 |
.ok_or(ICU4XError::TimeZoneInvalidIdError)?, |
| 193 |
); |
| 194 |
Ok(()) |
| 195 |
} |
| 196 |
|
| 197 |
/// Clears the `time_zone_id` field. |
| 198 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::time_zone_id, StructField)] |
| 199 |
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)] |
| 200 |
pub fn clear_time_zone_id(&mut self) { |
| 201 |
self.0.time_zone_id.take(); |
| 202 |
} |
| 203 |
|
| 204 |
/// Writes the value of the `time_zone_id` field as a string. |
| 205 |
/// |
| 206 |
/// Errors if the `time_zone_id` field is empty. |
| 207 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::time_zone_id, StructField)] |
| 208 |
#[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)] |
| 209 |
#[diplomat::attr(supports = accessors, getter)] |
| 210 |
pub fn time_zone_id( |
| 211 |
&self, |
| 212 |
write: &mut diplomat_runtime::DiplomatWriteable, |
| 213 |
) -> Result<(), ICU4XError> { |
| 214 |
write.write_str( |
| 215 |
self.0 |
| 216 |
.time_zone_id |
| 217 |
.ok_or(ICU4XError::TimeZoneMissingInputError)? |
| 218 |
.0 |
| 219 |
.as_str(), |
| 220 |
)?; |
| 221 |
Ok(()) |
| 222 |
} |
| 223 |
|
| 224 |
/// Sets the `metazone_id` field from a string. |
| 225 |
/// |
| 226 |
/// Errors if the string is not a valid BCP-47 metazone ID. |
| 227 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::metazone_id, StructField)] |
| 228 |
#[diplomat::rust_link(icu::timezone::MetazoneId, Struct, compact)] |
| 229 |
#[diplomat::rust_link(icu::timezone::MetazoneId::from_str, FnInStruct, hidden)] |
| 230 |
pub fn try_set_metazone_id(&mut self, id: &DiplomatStr) -> Result<(), ICU4XError> { |
| 231 |
self.0.metazone_id = Some(icu_timezone::MetazoneId( |
| 232 |
tinystr::TinyAsciiStr::from_bytes(id) |
| 233 |
.map_err(|_| ICU4XError::TimeZoneInvalidIdError)?, |
| 234 |
)); |
| 235 |
Ok(()) |
| 236 |
} |
| 237 |
|
| 238 |
/// Clears the `metazone_id` field. |
| 239 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::metazone_id, StructField)] |
| 240 |
#[diplomat::rust_link(icu::timezone::MetazoneId, Struct, compact)] |
| 241 |
pub fn clear_metazone_id(&mut self) { |
| 242 |
self.0.metazone_id.take(); |
| 243 |
} |
| 244 |
|
| 245 |
/// Writes the value of the `metazone_id` field as a string. |
| 246 |
/// |
| 247 |
/// Errors if the `metazone_id` field is empty. |
| 248 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::metazone_id, StructField)] |
| 249 |
#[diplomat::rust_link(icu::timezone::MetazoneId, Struct, compact)] |
| 250 |
#[diplomat::attr(supports = accessors, getter)] |
| 251 |
pub fn metazone_id( |
| 252 |
&self, |
| 253 |
write: &mut diplomat_runtime::DiplomatWriteable, |
| 254 |
) -> Result<(), ICU4XError> { |
| 255 |
write.write_str( |
| 256 |
self.0 |
| 257 |
.metazone_id |
| 258 |
.ok_or(ICU4XError::TimeZoneMissingInputError)? |
| 259 |
.0 |
| 260 |
.as_str(), |
| 261 |
)?; |
| 262 |
Ok(()) |
| 263 |
} |
| 264 |
|
| 265 |
/// Sets the `zone_variant` field from a string. |
| 266 |
/// |
| 267 |
/// Errors if the string is not a valid zone variant. |
| 268 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField)] |
| 269 |
#[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)] |
| 270 |
#[diplomat::rust_link(icu::timezone::ZoneVariant::from_str, FnInStruct, hidden)] |
| 271 |
pub fn try_set_zone_variant(&mut self, id: &DiplomatStr) -> Result<(), ICU4XError> { |
| 272 |
self.0.zone_variant = Some(icu_timezone::ZoneVariant( |
| 273 |
tinystr::TinyAsciiStr::from_bytes(id) |
| 274 |
.map_err(|_| ICU4XError::TimeZoneInvalidIdError)?, |
| 275 |
)); |
| 276 |
Ok(()) |
| 277 |
} |
| 278 |
|
| 279 |
/// Clears the `zone_variant` field. |
| 280 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField)] |
| 281 |
#[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)] |
| 282 |
pub fn clear_zone_variant(&mut self) { |
| 283 |
self.0.zone_variant.take(); |
| 284 |
} |
| 285 |
|
| 286 |
/// Writes the value of the `zone_variant` field as a string. |
| 287 |
/// |
| 288 |
/// Errors if the `zone_variant` field is empty. |
| 289 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField)] |
| 290 |
#[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)] |
| 291 |
#[diplomat::attr(supports = accessors, getter)] |
| 292 |
pub fn zone_variant( |
| 293 |
&self, |
| 294 |
write: &mut diplomat_runtime::DiplomatWriteable, |
| 295 |
) -> Result<(), ICU4XError> { |
| 296 |
write.write_str( |
| 297 |
self.0 |
| 298 |
.zone_variant |
| 299 |
.ok_or(ICU4XError::TimeZoneMissingInputError)? |
| 300 |
.0 |
| 301 |
.as_str(), |
| 302 |
)?; |
| 303 |
Ok(()) |
| 304 |
} |
| 305 |
|
| 306 |
/// Sets the `zone_variant` field to "standard" time, which may or may |
| 307 |
/// not correspond to a display name with "Standard" in its name. |
| 308 |
#[diplomat::rust_link(icu::timezone::ZoneVariant::standard, FnInStruct)] |
| 309 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
| 310 |
pub fn set_standard_time(&mut self) { |
| 311 |
self.0.zone_variant = Some(ZoneVariant::standard()) |
| 312 |
} |
| 313 |
|
| 314 |
/// Sets the `zone_variant` field to "daylight" time, which may or may |
| 315 |
/// not correspond to a display name with "Daylight" in its name. |
| 316 |
#[diplomat::rust_link(icu::timezone::ZoneVariant::daylight, FnInStruct)] |
| 317 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
| 318 |
pub fn set_daylight_time(&mut self) { |
| 319 |
self.0.zone_variant = Some(ZoneVariant::daylight()) |
| 320 |
} |
| 321 |
|
| 322 |
/// Returns whether the `zone_variant` field is standard time. |
| 323 |
/// |
| 324 |
/// Errors if the `zone_variant` field is empty. |
| 325 |
#[diplomat::rust_link(icu::timezone::ZoneVariant::standard, FnInStruct)] |
| 326 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
| 327 |
#[diplomat::attr(supports = accessors, getter)] |
| 328 |
pub fn is_standard_time(&self) -> Result<bool, ICU4XError> { |
| 329 |
Ok(self |
| 330 |
.0 |
| 331 |
.zone_variant |
| 332 |
.ok_or(ICU4XError::TimeZoneMissingInputError)? |
| 333 |
== ZoneVariant::standard()) |
| 334 |
} |
| 335 |
|
| 336 |
/// Returns whether the `zone_variant` field is daylight time. |
| 337 |
/// |
| 338 |
/// Errors if the `zone_variant` field is empty. |
| 339 |
#[diplomat::rust_link(icu::timezone::ZoneVariant::daylight, FnInStruct)] |
| 340 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
| 341 |
#[diplomat::attr(supports = accessors, getter)] |
| 342 |
pub fn is_daylight_time(&self) -> Result<bool, ICU4XError> { |
| 343 |
Ok(self |
| 344 |
.0 |
| 345 |
.zone_variant |
| 346 |
.ok_or(ICU4XError::TimeZoneMissingInputError)? |
| 347 |
== ZoneVariant::daylight()) |
| 348 |
} |
| 349 |
|
| 350 |
/// Sets the metazone based on the time zone and the local timestamp. |
| 351 |
#[diplomat::rust_link(icu::timezone::CustomTimeZone::maybe_calculate_metazone, FnInStruct)] |
| 352 |
#[diplomat::rust_link( |
| 353 |
icu::timezone::MetazoneCalculator::compute_metazone_from_time_zone, |
| 354 |
FnInStruct, |
| 355 |
compact |
| 356 |
)] |
| 357 |
#[cfg(feature = "icu_timezone")] |
| 358 |
pub fn maybe_calculate_metazone( |
| 359 |
&mut self, |
| 360 |
metazone_calculator: &crate::metazone_calculator::ffi::ICU4XMetazoneCalculator, |
| 361 |
local_datetime: &crate::datetime::ffi::ICU4XIsoDateTime, |
| 362 |
) { |
| 363 |
self.0 |
| 364 |
.maybe_calculate_metazone(&metazone_calculator.0, &local_datetime.0); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
impl From<CustomTimeZone> for ffi::ICU4XCustomTimeZone { |
| 370 |
fn from(other: CustomTimeZone) -> Self { |
| 371 |
Self(other) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
impl From<ffi::ICU4XCustomTimeZone> for CustomTimeZone { |
| 376 |
fn from(other: ffi::ICU4XCustomTimeZone) -> Self { |
| 377 |
other.0 |
| 378 |
} |
| 379 |
} |