1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
use alloc::boxed::Box;
use alloc::sync::Arc;
#[cfg(feature = "buffer_provider")]
use crate::unstable::errors::ffi::DataError;
use crate::unstable::locale_core::ffi::Locale;
#[cfg(feature = "buffer_provider")]
use crate::unstable::provider::ffi::DataProvider;
/// The various calendar types currently supported by [`Calendar`]
#[diplomat::enum_convert(icu_calendar::AnyCalendarKind, needs_wildcard)]
#[diplomat::rust_link(icu::calendar::AnyCalendarKind, Enum)]
pub enum CalendarKind {
/// The kind of an Iso calendar
Iso = 0,
/// The kind of a Gregorian calendar
Gregorian = 1,
/// The kind of a Buddhist calendar
Buddhist = 2,
/// The kind of a Japanese calendar with modern eras
Japanese = 3,
/// The kind of a Japanese calendar with modern and historic eras
JapaneseExtended = 4,
/// The kind of an Ethiopian calendar, with Amete Mihret era
Ethiopian = 5,
/// The kind of an Ethiopian calendar, with Amete Alem era
EthiopianAmeteAlem = 6,
/// The kind of a Indian calendar
Indian = 7,
/// The kind of a Coptic calendar
Coptic = 8,
/// The kind of a Dangi calendar
Dangi = 9,
/// The kind of a Chinese calendar
Chinese = 10,
/// The kind of a Hebrew calendar
Hebrew = 11,
/// The kind of a Hijri tabular, type II leap years, Friday epoch, calendar
HijriTabularTypeIIFriday = 12,
/// The kind of a Hijri simulated, Mecca calendar
HijriSimulatedMecca = 18,
/// The kind of a Hijri tabular, type II leap years, Thursday epoch, calendar
HijriTabularTypeIIThursday = 14,
/// The kind of a Hijri Umm al-Qura calendar
HijriUmmAlQura = 15,
/// The kind of a Persian calendar
Persian = 16,
/// The kind of a Roc calendar
Roc = 17,
}
impl CalendarKind {
/// Creates a new [`CalendarKind`] for the specified locale, using compiled data.
#[diplomat::rust_link(icu::calendar::AnyCalendarKind::new, FnInEnum)]
pub fn create(locale: &Locale) -> Self {
let prefs = (&locale.0).into();
icu_calendar::AnyCalendarKind::new(prefs).into()
}
}
#[diplomat::opaque]
#[diplomat::transparent_convert]
#[diplomat::rust_link(icu::calendar::AnyCalendar, Enum)]
pub struct Calendar(pub Arc<icu_calendar::AnyCalendar>);
impl Calendar {
/// Creates a new [`Calendar`] for the specified kind, using compiled data.
#[diplomat::rust_link(icu::calendar::AnyCalendar::new, FnInEnum)]
#[diplomat::attr(auto, constructor)]
#[cfg(feature = "compiled_data")]
pub fn create(kind: CalendarKind) -> Box<Calendar> {
Box::new(Calendar(Arc::new(icu_calendar::AnyCalendar::new(
kind.into(),
))))
}
/// Creates a new [`Calendar`] for the specified kind, using a particular data source.
#[diplomat::rust_link(icu::calendar::AnyCalendar::new, FnInEnum)]
#[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "new_with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(
provider: &DataProvider,
kind: CalendarKind,
) -> Result<Box<Calendar>, DataError> {
Ok(Box::new(Calendar(Arc::new(
icu_calendar::AnyCalendar::try_new_with_buffer_provider(
provider.get()?,
kind.into(),
)?,
))))
}
/// Returns the kind of this calendar
#[diplomat::rust_link(icu::calendar::AnyCalendar::kind, FnInEnum)]
#[diplomat::attr(auto, getter)]
#[diplomat::attr(demo_gen, disable)] // this just returns the single constructor argument
pub fn kind(&self) -> CalendarKind {
self.0.kind().into()
}
}
}