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 #[diplomat::bridge] 6 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 7 #[diplomat::attr(auto, namespace = "icu4x")] 8 pub mod ffi { 9 use alloc::boxed::Box; 10 11 use crate::date::ffi::Weekday; 12 #[cfg(feature = "buffer_provider")] 13 use crate::provider::ffi::DataProvider; 14 #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))] 15 use crate::{errors::ffi::DataError, locale_core::ffi::Locale}; 16 17 #[diplomat::rust_link(icu::calendar::week::RelativeUnit, Enum)] 18 #[diplomat::enum_convert(icu_calendar::week::RelativeUnit)] 19 pub enum WeekRelativeUnit { 20 Previous, 21 Current, 22 Next, 23 } 24 25 #[diplomat::rust_link(icu::calendar::week::WeekOf, Struct)] 26 #[diplomat::out] 27 pub struct WeekOf { 28 pub week: u8, 29 pub unit: WeekRelativeUnit, 30 } 31 /// A Week calculator, useful to be passed in to `week_of_year()` on Date and DateTime types 32 #[diplomat::opaque] 33 #[diplomat::rust_link(icu::calendar::week::WeekCalculator, Struct)] 34 pub struct WeekCalculator(pub icu_calendar::week::WeekCalculator); 35 36 impl WeekCalculator { 37 /// Creates a new [`WeekCalculator`] from locale data using compiled data. 38 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::try_new, FnInStruct)] 39 #[diplomat::attr(supports = fallible_constructors, constructor)] 40 #[cfg(feature = "compiled_data")] create(locale: &Locale) -> Result<Box<WeekCalculator>, DataError>41 pub fn create(locale: &Locale) -> Result<Box<WeekCalculator>, DataError> { 42 let prefs = (&locale.0).into(); 43 44 Ok(Box::new(WeekCalculator( 45 icu_calendar::week::WeekCalculator::try_new(prefs)?, 46 ))) 47 } 48 /// Creates a new [`WeekCalculator`] from locale data using a particular data source. 49 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::try_new, FnInStruct)] 50 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")] 51 #[cfg(feature = "buffer_provider")] create_with_provider( provider: &DataProvider, locale: &Locale, ) -> Result<Box<WeekCalculator>, DataError>52 pub fn create_with_provider( 53 provider: &DataProvider, 54 locale: &Locale, 55 ) -> Result<Box<WeekCalculator>, DataError> { 56 let prefs = (&locale.0).into(); 57 58 Ok(Box::new(WeekCalculator( 59 icu_calendar::week::WeekCalculator::try_new_with_buffer_provider( 60 provider.get()?, 61 prefs, 62 )?, 63 ))) 64 } 65 #[diplomat::rust_link( 66 icu::calendar::week::WeekCalculator::first_weekday, 67 StructField, 68 compact 69 )] 70 #[diplomat::rust_link( 71 icu::calendar::week::WeekCalculator::min_week_days, 72 StructField, 73 compact 74 )] 75 #[diplomat::attr(auto, named_constructor)] from_first_day_of_week_and_min_week_days( first_weekday: Weekday, min_week_days: u8, ) -> Box<WeekCalculator>76 pub fn from_first_day_of_week_and_min_week_days( 77 first_weekday: Weekday, 78 min_week_days: u8, 79 ) -> Box<WeekCalculator> { 80 let mut calculator = icu_calendar::week::WeekCalculator::default(); 81 calculator.first_weekday = first_weekday.into(); 82 calculator.min_week_days = min_week_days; 83 Box::new(WeekCalculator(calculator)) 84 } 85 86 /// Returns the weekday that starts the week for this object's locale 87 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::first_weekday, StructField)] 88 #[diplomat::attr(auto, getter)] first_weekday(&self) -> Weekday89 pub fn first_weekday(&self) -> Weekday { 90 self.0.first_weekday.into() 91 } 92 /// The minimum number of days overlapping a year required for a week to be 93 /// considered part of that year 94 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::min_week_days, StructField)] 95 #[diplomat::attr(auto, getter)] min_week_days(&self) -> u896 pub fn min_week_days(&self) -> u8 { 97 self.0.min_week_days 98 } 99 100 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)] 101 #[diplomat::attr(auto, getter)] weekend(&self) -> WeekendContainsDay102 pub fn weekend(&self) -> WeekendContainsDay { 103 let mut contains = WeekendContainsDay::default(); 104 for day in self.0.weekend() { 105 match day { 106 icu_calendar::types::Weekday::Monday => contains.monday = true, 107 icu_calendar::types::Weekday::Tuesday => contains.tuesday = true, 108 icu_calendar::types::Weekday::Wednesday => contains.wednesday = true, 109 icu_calendar::types::Weekday::Thursday => contains.thursday = true, 110 icu_calendar::types::Weekday::Friday => contains.friday = true, 111 icu_calendar::types::Weekday::Saturday => contains.saturday = true, 112 icu_calendar::types::Weekday::Sunday => contains.sunday = true, 113 } 114 } 115 contains 116 } 117 } 118 119 /// Documents which days of the week are considered to be a part of the weekend 120 #[diplomat::rust_link(icu::calendar::week::WeekCalculator::weekend, FnInStruct)] 121 #[derive(Default)] 122 pub struct WeekendContainsDay { 123 pub monday: bool, 124 pub tuesday: bool, 125 pub wednesday: bool, 126 pub thursday: bool, 127 pub friday: bool, 128 pub saturday: bool, 129 pub sunday: bool, 130 } 131 } 132 133 impl From<icu_calendar::week::WeekOf> for ffi::WeekOf { from(other: icu_calendar::week::WeekOf) -> Self134 fn from(other: icu_calendar::week::WeekOf) -> Self { 135 ffi::WeekOf { 136 week: other.week, 137 unit: other.unit.into(), 138 } 139 } 140 } 141