rrule-rust 2.0.0-alpha.4 → 2.0.0-next.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{index.d.ts → dist/index.d.ts} +4 -1
- package/{index.js → dist/index.js} +62 -4
- package/package.json +41 -31
- package/.eslintrc.js +0 -58
- package/.prettierrc +0 -4
- package/.releaserc +0 -46
- package/.yarnrc.yml +0 -1
- package/Cargo.toml +0 -22
- package/benchmark/.gitignore +0 -3
- package/benchmark/index.ts +0 -59
- package/build.rs +0 -5
- package/jest.config.ts +0 -7
- package/src/js/frequency.rs +0 -40
- package/src/js/month.rs +0 -56
- package/src/js/n_weekday.rs +0 -36
- package/src/js/rrule.rs +0 -275
- package/src/js/rrule_datetime.rs +0 -128
- package/src/js/rrule_set.rs +0 -258
- package/src/js/rrule_timezone.rs +0 -52
- package/src/js/weekday.rs +0 -40
- package/src/js.rs +0 -17
- package/src/lib.rs +0 -1
- package/tsconfig.eslint.json +0 -6
- package/tsconfig.json +0 -24
package/src/js/rrule.rs
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
use super::{Frequency, Month, NWeekday, RRuleDateTime, Weekday};
|
|
2
|
-
use chrono::DateTime;
|
|
3
|
-
use napi::{bindgen_prelude::Array, Either, Env};
|
|
4
|
-
use napi_derive::napi;
|
|
5
|
-
use replace_with::replace_with_or_abort;
|
|
6
|
-
|
|
7
|
-
#[napi(js_name = "RRule")]
|
|
8
|
-
pub struct RRule {
|
|
9
|
-
rrule: rrule::RRule<rrule::Unvalidated>,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
#[napi]
|
|
13
|
-
impl RRule {
|
|
14
|
-
#[napi(constructor)]
|
|
15
|
-
pub fn new(frequency: Frequency) -> Self {
|
|
16
|
-
let rrule = rrule::RRule::new(frequency.into());
|
|
17
|
-
|
|
18
|
-
RRule { rrule }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
#[napi(factory, ts_return_type = "RRule")]
|
|
22
|
-
pub fn parse(str: String) -> napi::Result<Self> {
|
|
23
|
-
let rrule: rrule::RRule<rrule::Unvalidated> = str
|
|
24
|
-
.parse()
|
|
25
|
-
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e))?;
|
|
26
|
-
|
|
27
|
-
Ok(RRule { rrule })
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
#[napi(getter)]
|
|
31
|
-
pub fn frequency(&self) -> napi::Result<Frequency> {
|
|
32
|
-
Ok(Frequency::from(self.rrule.get_freq()))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
#[napi(getter)]
|
|
36
|
-
pub fn interval(&self) -> napi::Result<u16> {
|
|
37
|
-
Ok(self.rrule.get_interval())
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
#[napi(getter)]
|
|
41
|
-
pub fn count(&self) -> napi::Result<Option<u32>> {
|
|
42
|
-
Ok(self.rrule.get_count())
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
#[napi(getter, ts_return_type = "NWeekday[]")]
|
|
46
|
-
pub fn by_weekday(&self) -> Vec<NWeekday> {
|
|
47
|
-
return self
|
|
48
|
-
.rrule
|
|
49
|
-
.get_by_weekday()
|
|
50
|
-
.iter()
|
|
51
|
-
.map(|nday| NWeekday::from(*nday))
|
|
52
|
-
.collect();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
#[napi(getter)]
|
|
56
|
-
pub fn by_hour(&self) -> napi::Result<Vec<u8>> {
|
|
57
|
-
Ok(self.rrule.get_by_hour().to_vec())
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
#[napi(getter)]
|
|
61
|
-
pub fn by_minute(&self) -> napi::Result<Vec<u8>> {
|
|
62
|
-
Ok(self.rrule.get_by_minute().to_vec())
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
#[napi(getter)]
|
|
66
|
-
pub fn by_second(&self) -> napi::Result<Vec<u8>> {
|
|
67
|
-
Ok(self.rrule.get_by_second().to_vec())
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
#[napi(getter)]
|
|
71
|
-
pub fn by_monthday(&self) -> napi::Result<Vec<i8>> {
|
|
72
|
-
Ok(self.rrule.get_by_month_day().to_vec())
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
#[napi(getter)]
|
|
76
|
-
pub fn by_setpos(&self) -> napi::Result<Vec<i32>> {
|
|
77
|
-
Ok(self.rrule.get_by_set_pos().to_vec())
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
#[napi(getter, ts_return_type = "Month[]")]
|
|
81
|
-
pub fn by_month(&self, env: Env) -> napi::Result<Array> {
|
|
82
|
-
let months = self.rrule.get_by_month();
|
|
83
|
-
let mut arr = env.create_array(0)?;
|
|
84
|
-
|
|
85
|
-
for month in months.iter() {
|
|
86
|
-
arr.insert(Month::from(month))?;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
Ok(arr)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
#[napi(getter)]
|
|
93
|
-
pub fn by_weekno(&self) -> napi::Result<Vec<i8>> {
|
|
94
|
-
Ok(self.rrule.get_by_week_no().to_vec())
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
#[napi(getter)]
|
|
98
|
-
pub fn by_yearday(&self) -> napi::Result<Vec<i16>> {
|
|
99
|
-
Ok(self.rrule.get_by_year_day().to_vec())
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
#[napi(getter)]
|
|
103
|
-
pub fn weekstart(&self) -> napi::Result<Weekday> {
|
|
104
|
-
Ok(Weekday::from(self.rrule.get_week_start()))
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
#[napi(getter)]
|
|
108
|
-
pub fn until(&self) -> napi::Result<Option<RRuleDateTime>> {
|
|
109
|
-
Ok(match self.rrule.get_until() {
|
|
110
|
-
Some(until) => Some(RRuleDateTime::new_with_date_time(until.clone())),
|
|
111
|
-
None => None,
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
#[napi]
|
|
116
|
-
pub fn to_string(&self) -> napi::Result<String> {
|
|
117
|
-
Ok(self.rrule.to_string())
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
#[napi]
|
|
121
|
-
pub fn set_interval(&mut self, interval: u16) -> napi::Result<&Self> {
|
|
122
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.interval(interval));
|
|
123
|
-
|
|
124
|
-
Ok(self)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
#[napi]
|
|
128
|
-
pub fn set_count(&mut self, count: u32) -> napi::Result<&Self> {
|
|
129
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.count(count));
|
|
130
|
-
|
|
131
|
-
Ok(self)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
#[napi]
|
|
135
|
-
pub fn set_by_weekday(
|
|
136
|
-
&mut self,
|
|
137
|
-
#[napi(ts_arg_type = "ReadonlyArray<NWeekday | Weekday>")] weekdays: Vec<
|
|
138
|
-
Either<NWeekday, Weekday>,
|
|
139
|
-
>,
|
|
140
|
-
) -> napi::Result<&Self> {
|
|
141
|
-
let by_weekday = weekdays
|
|
142
|
-
.into_iter()
|
|
143
|
-
.map(|weekday| match weekday {
|
|
144
|
-
Either::A(nday) => nday.into(),
|
|
145
|
-
Either::B(weekday) => rrule::NWeekday::Every(weekday.into()),
|
|
146
|
-
})
|
|
147
|
-
.collect();
|
|
148
|
-
|
|
149
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_weekday(by_weekday));
|
|
150
|
-
|
|
151
|
-
Ok(self)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
#[napi]
|
|
155
|
-
pub fn set_by_hour(
|
|
156
|
-
&mut self,
|
|
157
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] hours: Vec<u8>,
|
|
158
|
-
) -> napi::Result<&Self> {
|
|
159
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_hour(hours));
|
|
160
|
-
|
|
161
|
-
Ok(self)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
#[napi]
|
|
165
|
-
pub fn set_by_minute(
|
|
166
|
-
&mut self,
|
|
167
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] minutes: Vec<u8>,
|
|
168
|
-
) -> napi::Result<&Self> {
|
|
169
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_minute(minutes));
|
|
170
|
-
|
|
171
|
-
Ok(self)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
#[napi]
|
|
175
|
-
pub fn set_by_second(
|
|
176
|
-
&mut self,
|
|
177
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] seconds: Vec<u8>,
|
|
178
|
-
) -> napi::Result<&Self> {
|
|
179
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_second(seconds));
|
|
180
|
-
|
|
181
|
-
Ok(self)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
#[napi]
|
|
185
|
-
pub fn set_by_monthday(
|
|
186
|
-
&mut self,
|
|
187
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] days: Vec<i8>,
|
|
188
|
-
) -> napi::Result<&Self> {
|
|
189
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_month_day(days));
|
|
190
|
-
|
|
191
|
-
Ok(self)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
#[napi]
|
|
195
|
-
pub fn set_by_setpos(
|
|
196
|
-
&mut self,
|
|
197
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] poses: Vec<i32>,
|
|
198
|
-
) -> napi::Result<&Self> {
|
|
199
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_set_pos(poses));
|
|
200
|
-
|
|
201
|
-
Ok(self)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
#[napi]
|
|
205
|
-
pub fn set_by_month(
|
|
206
|
-
&mut self,
|
|
207
|
-
#[napi(ts_arg_type = "ReadonlyArray<Month>")] months: Array,
|
|
208
|
-
) -> napi::Result<&Self> {
|
|
209
|
-
let mut vec: Vec<chrono::Month> = Vec::new();
|
|
210
|
-
|
|
211
|
-
for i in 0..months.len() {
|
|
212
|
-
let month: Month = months.get(i).unwrap().unwrap();
|
|
213
|
-
|
|
214
|
-
vec.push(month.into());
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_month(&vec));
|
|
218
|
-
|
|
219
|
-
Ok(self)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
#[napi]
|
|
223
|
-
pub fn set_by_weekno(
|
|
224
|
-
&mut self,
|
|
225
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] week_numbers: Vec<i8>,
|
|
226
|
-
) -> napi::Result<&Self> {
|
|
227
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_week_no(week_numbers));
|
|
228
|
-
|
|
229
|
-
Ok(self)
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
#[napi]
|
|
233
|
-
pub fn set_by_yearday(
|
|
234
|
-
&mut self,
|
|
235
|
-
#[napi(ts_arg_type = "ReadonlyArray<number>")] days: Vec<i16>,
|
|
236
|
-
) -> napi::Result<&Self> {
|
|
237
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.by_year_day(days));
|
|
238
|
-
|
|
239
|
-
Ok(self)
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
#[napi]
|
|
243
|
-
pub fn set_weekstart(&mut self, day: Weekday) -> napi::Result<&Self> {
|
|
244
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.week_start(day.into()));
|
|
245
|
-
|
|
246
|
-
Ok(self)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
#[napi]
|
|
250
|
-
pub fn set_until(
|
|
251
|
-
&mut self,
|
|
252
|
-
date_time: napi::Either<&RRuleDateTime, napi::JsDate>,
|
|
253
|
-
) -> napi::Result<&Self> {
|
|
254
|
-
let date_time = RRuleDateTime::from(date_time);
|
|
255
|
-
replace_with_or_abort(&mut self.rrule, |self_| self_.until(date_time.into()));
|
|
256
|
-
|
|
257
|
-
Ok(self)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
pub fn validate(&self, dt_start: DateTime<rrule::Tz>) -> napi::Result<rrule::RRule> {
|
|
261
|
-
return Ok(
|
|
262
|
-
self
|
|
263
|
-
.rrule
|
|
264
|
-
.clone()
|
|
265
|
-
.validate(dt_start)
|
|
266
|
-
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e))?,
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
impl From<rrule::RRule<rrule::Unvalidated>> for RRule {
|
|
272
|
-
fn from(rrule: rrule::RRule<rrule::Unvalidated>) -> Self {
|
|
273
|
-
Self { rrule }
|
|
274
|
-
}
|
|
275
|
-
}
|
package/src/js/rrule_datetime.rs
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
use super::RRuleTimezone;
|
|
2
|
-
use chrono::DateTime;
|
|
3
|
-
use chrono::Datelike;
|
|
4
|
-
use chrono::TimeZone;
|
|
5
|
-
use chrono::Timelike;
|
|
6
|
-
use napi::bindgen_prelude::*;
|
|
7
|
-
use napi_derive::napi;
|
|
8
|
-
use std::str::FromStr;
|
|
9
|
-
|
|
10
|
-
#[napi(js_name = "RRuleDateTime")]
|
|
11
|
-
pub struct RRuleDateTime {
|
|
12
|
-
date_time: chrono::DateTime<rrule::Tz>,
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
#[napi]
|
|
16
|
-
impl RRuleDateTime {
|
|
17
|
-
#[napi(constructor)]
|
|
18
|
-
pub fn new_with_js_date(
|
|
19
|
-
date: napi::Either<napi::JsDate, f64>,
|
|
20
|
-
timezone: Option<String>,
|
|
21
|
-
) -> napi::Result<Self> {
|
|
22
|
-
let timestamp = match date {
|
|
23
|
-
Either::A(date) => date.value_of(),
|
|
24
|
-
Either::B(date) => Ok(date),
|
|
25
|
-
}?;
|
|
26
|
-
|
|
27
|
-
let mut date_time = rrule::Tz::LOCAL.timestamp_nanos((timestamp * 1_000_000f64) as i64);
|
|
28
|
-
if let Some(timezone) = timezone {
|
|
29
|
-
let tz: rrule::Tz = RRuleTimezone::from_str(&timezone)?.into();
|
|
30
|
-
|
|
31
|
-
date_time = date_time.with_timezone(&tz);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
Ok(RRuleDateTime::new_with_date_time(date_time))
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
pub fn new_with_date_time(date_time: DateTime<rrule::Tz>) -> Self {
|
|
38
|
-
RRuleDateTime { date_time }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
#[napi(getter)]
|
|
42
|
-
pub fn timestamp(&self) -> i64 {
|
|
43
|
-
self.date_time.timestamp_millis()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
#[napi(getter)]
|
|
47
|
-
pub fn timezone(&self) -> RRuleTimezone {
|
|
48
|
-
RRuleTimezone::new_with_tz(self.date_time.timezone())
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
#[napi(getter)]
|
|
52
|
-
pub fn day(&self) -> u32 {
|
|
53
|
-
self.date_time.day()
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
#[napi(getter)]
|
|
57
|
-
pub fn month(&self) -> u32 {
|
|
58
|
-
self.date_time.month()
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
#[napi(getter)]
|
|
62
|
-
pub fn year(&self) -> i32 {
|
|
63
|
-
self.date_time.year()
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
#[napi(getter)]
|
|
67
|
-
pub fn hour(&self) -> u32 {
|
|
68
|
-
self.date_time.hour()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
#[napi(getter)]
|
|
72
|
-
pub fn minute(&self) -> u32 {
|
|
73
|
-
self.date_time.minute()
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
#[napi(getter)]
|
|
77
|
-
pub fn second(&self) -> u32 {
|
|
78
|
-
self.date_time.second()
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
#[napi(getter)]
|
|
82
|
-
pub fn millisecond(&self) -> u32 {
|
|
83
|
-
let nanoseconds = self.date_time.nanosecond();
|
|
84
|
-
nanoseconds / 1_000_000
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
#[napi(getter)]
|
|
88
|
-
pub fn to_string(&self) -> String {
|
|
89
|
-
self.date_time.to_string()
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
#[napi(ts_return_type = "Date")]
|
|
93
|
-
pub fn to_date(&self, env: Env) -> napi::Result<napi::JsDate> {
|
|
94
|
-
env.create_date(self.date_time.timestamp_millis() as f64)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
#[napi(ts_return_type = "Date")]
|
|
98
|
-
pub fn to_utc_date(&self, env: Env) -> napi::Result<napi::JsDate> {
|
|
99
|
-
env.create_date(self.date_time.naive_utc().and_utc().timestamp_millis() as f64)
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
impl From<DateTime<rrule::Tz>> for RRuleDateTime {
|
|
104
|
-
fn from(date_time: DateTime<rrule::Tz>) -> Self {
|
|
105
|
-
RRuleDateTime::new_with_date_time(date_time)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
impl Into<DateTime<rrule::Tz>> for RRuleDateTime {
|
|
110
|
-
fn into(self) -> DateTime<rrule::Tz> {
|
|
111
|
-
self.date_time
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
impl From<napi::JsDate> for RRuleDateTime {
|
|
116
|
-
fn from(date: napi::JsDate) -> Self {
|
|
117
|
-
RRuleDateTime::new_with_js_date(napi::Either::A(date), None).unwrap()
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
impl From<napi::Either<&RRuleDateTime, napi::JsDate>> for RRuleDateTime {
|
|
122
|
-
fn from(date: napi::Either<&RRuleDateTime, napi::JsDate>) -> Self {
|
|
123
|
-
match date {
|
|
124
|
-
Either::A(date) => RRuleDateTime::new_with_date_time(date.date_time),
|
|
125
|
-
Either::B(date) => RRuleDateTime::new_with_js_date(napi::Either::A(date), None).unwrap(),
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
package/src/js/rrule_set.rs
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
use super::{RRule, RRuleDateTime};
|
|
2
|
-
use chrono::DateTime;
|
|
3
|
-
use napi::{
|
|
4
|
-
bindgen_prelude::{Reference, SharedReference},
|
|
5
|
-
iterator::Generator,
|
|
6
|
-
Env,
|
|
7
|
-
};
|
|
8
|
-
use napi_derive::napi;
|
|
9
|
-
use replace_with::replace_with_or_abort;
|
|
10
|
-
|
|
11
|
-
#[napi(js_name = "RRuleSet")]
|
|
12
|
-
pub struct RRuleSet {
|
|
13
|
-
rrule_set: rrule::RRuleSet,
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
#[napi]
|
|
17
|
-
impl RRuleSet {
|
|
18
|
-
#[napi(constructor)]
|
|
19
|
-
pub fn new(dtstart: napi::Either<&RRuleDateTime, napi::JsDate>) -> napi::Result<Self> {
|
|
20
|
-
let rrule_set = rrule::RRuleSet::new(RRuleDateTime::from(dtstart).into());
|
|
21
|
-
|
|
22
|
-
Ok(RRuleSet { rrule_set })
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
#[napi(factory, ts_return_type = "RRuleSet")]
|
|
26
|
-
pub fn parse(str: String) -> napi::Result<Self> {
|
|
27
|
-
let rrule_set: rrule::RRuleSet = str
|
|
28
|
-
.parse()
|
|
29
|
-
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e))?;
|
|
30
|
-
|
|
31
|
-
Ok(RRuleSet { rrule_set })
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
#[napi]
|
|
35
|
-
pub fn to_string(&self) -> napi::Result<String> {
|
|
36
|
-
Ok(self.rrule_set.to_string())
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
#[napi]
|
|
40
|
-
pub fn add_rrule(&mut self, js_rrule: &RRule) -> napi::Result<&Self> {
|
|
41
|
-
let dt_start = self.rrule_set.get_dt_start().clone();
|
|
42
|
-
let rrule = js_rrule.validate(dt_start)?;
|
|
43
|
-
|
|
44
|
-
replace_with_or_abort(&mut self.rrule_set, |self_| self_.rrule(rrule));
|
|
45
|
-
|
|
46
|
-
Ok(self)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
#[napi]
|
|
50
|
-
pub fn add_exrule(&mut self, js_rrule: &RRule) -> napi::Result<&Self> {
|
|
51
|
-
let rrule = js_rrule.validate(*self.rrule_set.get_dt_start())?;
|
|
52
|
-
|
|
53
|
-
replace_with_or_abort(&mut self.rrule_set, |self_| self_.exrule(rrule));
|
|
54
|
-
|
|
55
|
-
Ok(self)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
#[napi]
|
|
59
|
-
pub fn add_exdate(
|
|
60
|
-
&mut self,
|
|
61
|
-
date_time: napi::Either<&RRuleDateTime, napi::JsDate>,
|
|
62
|
-
) -> napi::Result<&Self> {
|
|
63
|
-
replace_with_or_abort(&mut self.rrule_set, |self_| {
|
|
64
|
-
self_.exdate(RRuleDateTime::from(date_time).into())
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
Ok(self)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
#[napi]
|
|
71
|
-
pub fn add_rdate(
|
|
72
|
-
&mut self,
|
|
73
|
-
date_time: napi::Either<&RRuleDateTime, napi::JsDate>,
|
|
74
|
-
) -> napi::Result<&Self> {
|
|
75
|
-
replace_with_or_abort(&mut self.rrule_set, |self_| {
|
|
76
|
-
self_.rdate(RRuleDateTime::from(date_time).into())
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
Ok(self)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
#[napi(getter)]
|
|
83
|
-
pub fn dtstart(&self) -> RRuleDateTime {
|
|
84
|
-
RRuleDateTime::new_with_date_time(self.rrule_set.get_dt_start().clone())
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
#[napi]
|
|
88
|
-
pub fn get_rrules(&self) -> Vec<RRule> {
|
|
89
|
-
return self
|
|
90
|
-
.rrule_set
|
|
91
|
-
.get_rrule()
|
|
92
|
-
.iter()
|
|
93
|
-
.map(|rrule| RRule::from(to_unvalidated(rrule)))
|
|
94
|
-
.collect();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
#[napi]
|
|
98
|
-
pub fn get_exrules(&self) -> Vec<RRule> {
|
|
99
|
-
return self
|
|
100
|
-
.rrule_set
|
|
101
|
-
.get_exrule()
|
|
102
|
-
.iter()
|
|
103
|
-
.map(|rrule| RRule::from(to_unvalidated(rrule)))
|
|
104
|
-
.collect();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
#[napi]
|
|
108
|
-
pub fn get_exdates(&self) -> Vec<RRuleDateTime> {
|
|
109
|
-
return self
|
|
110
|
-
.rrule_set
|
|
111
|
-
.get_exdate()
|
|
112
|
-
.iter()
|
|
113
|
-
.map(|date| RRuleDateTime::new_with_date_time(date.clone()))
|
|
114
|
-
.collect();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
#[napi]
|
|
118
|
-
pub fn get_rdates(&self) -> Vec<RRuleDateTime> {
|
|
119
|
-
return self
|
|
120
|
-
.rrule_set
|
|
121
|
-
.get_rdate()
|
|
122
|
-
.iter()
|
|
123
|
-
.map(|date| RRuleDateTime::new_with_date_time(date.clone()))
|
|
124
|
-
.collect();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
fn is_after(
|
|
128
|
-
&self,
|
|
129
|
-
timestamp: DateTime<rrule::Tz>,
|
|
130
|
-
after_timestamp: DateTime<rrule::Tz>,
|
|
131
|
-
inclusive: Option<bool>,
|
|
132
|
-
) -> bool {
|
|
133
|
-
let inclusive = inclusive.unwrap_or(false);
|
|
134
|
-
|
|
135
|
-
if inclusive && timestamp < after_timestamp {
|
|
136
|
-
return false;
|
|
137
|
-
} else if !inclusive && timestamp <= after_timestamp {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
true
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
fn is_before(
|
|
145
|
-
&self,
|
|
146
|
-
timestamp: DateTime<rrule::Tz>,
|
|
147
|
-
before_timestamp: DateTime<rrule::Tz>,
|
|
148
|
-
inclusive: Option<bool>,
|
|
149
|
-
) -> bool {
|
|
150
|
-
let inclusive = inclusive.unwrap_or(false);
|
|
151
|
-
|
|
152
|
-
if inclusive && timestamp > before_timestamp {
|
|
153
|
-
return false;
|
|
154
|
-
} else if !inclusive && timestamp >= before_timestamp {
|
|
155
|
-
return false;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
true
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
#[napi]
|
|
162
|
-
pub fn all(&self, limit: Option<u32>) -> Vec<RRuleDateTime> {
|
|
163
|
-
let iter = self.rrule_set.into_iter();
|
|
164
|
-
if let Some(limit) = limit {
|
|
165
|
-
return iter
|
|
166
|
-
.take(limit as usize)
|
|
167
|
-
.map(|date| RRuleDateTime::new_with_date_time(date))
|
|
168
|
-
.collect();
|
|
169
|
-
} else {
|
|
170
|
-
return iter
|
|
171
|
-
.map(|date| RRuleDateTime::new_with_date_time(date))
|
|
172
|
-
.collect();
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
#[napi]
|
|
177
|
-
pub fn between(
|
|
178
|
-
&self,
|
|
179
|
-
after: napi::Either<&RRuleDateTime, napi::JsDate>,
|
|
180
|
-
before: napi::Either<&RRuleDateTime, napi::JsDate>,
|
|
181
|
-
inclusive: Option<bool>,
|
|
182
|
-
) -> napi::Result<Vec<RRuleDateTime>> {
|
|
183
|
-
let _after = RRuleDateTime::from(after).into();
|
|
184
|
-
let _before = RRuleDateTime::from(before).into();
|
|
185
|
-
return Ok(
|
|
186
|
-
self
|
|
187
|
-
.rrule_set
|
|
188
|
-
.into_iter()
|
|
189
|
-
.take_while(|date| {
|
|
190
|
-
let is_before = self.is_before(*date, _before, inclusive);
|
|
191
|
-
|
|
192
|
-
is_before
|
|
193
|
-
})
|
|
194
|
-
.filter(|date| {
|
|
195
|
-
let is_after = self.is_after(*date, _after, inclusive);
|
|
196
|
-
|
|
197
|
-
is_after
|
|
198
|
-
})
|
|
199
|
-
.map(|date| RRuleDateTime::new_with_date_time(date))
|
|
200
|
-
.collect::<Vec<_>>(),
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
#[napi]
|
|
205
|
-
pub fn occurrences(&self, this: Reference<RRuleSet>, env: Env) -> napi::Result<Occurrences> {
|
|
206
|
-
let iter = this.share_with(env, |set| Ok(set.rrule_set.into_iter()))?;
|
|
207
|
-
Ok(Occurrences { iter })
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
#[napi(iterator)]
|
|
212
|
-
pub struct Occurrences {
|
|
213
|
-
iter: SharedReference<RRuleSet, rrule::RRuleSetIter<'static>>,
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
#[napi]
|
|
217
|
-
impl Generator for Occurrences {
|
|
218
|
-
type Yield = RRuleDateTime;
|
|
219
|
-
type Next = ();
|
|
220
|
-
type Return = ();
|
|
221
|
-
|
|
222
|
-
fn next(&mut self, _next: Option<Self::Next>) -> Option<Self::Yield> {
|
|
223
|
-
self
|
|
224
|
-
.iter
|
|
225
|
-
.next()
|
|
226
|
-
.map(|date| RRuleDateTime::new_with_date_time(date))
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
fn to_unvalidated(rrule: &rrule::RRule) -> rrule::RRule<rrule::Unvalidated> {
|
|
231
|
-
let by_month = rrule
|
|
232
|
-
.get_by_month()
|
|
233
|
-
.iter()
|
|
234
|
-
.map(|m| chrono::Month::try_from(*m).unwrap())
|
|
235
|
-
.collect::<Vec<_>>();
|
|
236
|
-
let mut unvalidated = rrule::RRule::new(rrule.get_freq())
|
|
237
|
-
.interval(rrule.get_interval())
|
|
238
|
-
.week_start(rrule.get_week_start())
|
|
239
|
-
.by_set_pos(rrule.get_by_set_pos().to_vec())
|
|
240
|
-
.by_month(&by_month)
|
|
241
|
-
.by_month_day(rrule.get_by_month_day().to_vec())
|
|
242
|
-
.by_year_day(rrule.get_by_year_day().to_vec())
|
|
243
|
-
.by_week_no(rrule.get_by_week_no().to_vec())
|
|
244
|
-
.by_weekday(rrule.get_by_weekday().to_vec())
|
|
245
|
-
.by_hour(rrule.get_by_hour().to_vec())
|
|
246
|
-
.by_minute(rrule.get_by_minute().to_vec())
|
|
247
|
-
.by_second(rrule.get_by_second().to_vec());
|
|
248
|
-
|
|
249
|
-
if let Some(count) = rrule.get_count() {
|
|
250
|
-
unvalidated = unvalidated.count(count);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if let Some(until) = rrule.get_until() {
|
|
254
|
-
unvalidated = unvalidated.until(*until);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
unvalidated
|
|
258
|
-
}
|
package/src/js/rrule_timezone.rs
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
use napi_derive::napi;
|
|
2
|
-
use std::str::FromStr;
|
|
3
|
-
|
|
4
|
-
#[derive(Debug)]
|
|
5
|
-
#[napi(js_name = "RRuleTimezone")]
|
|
6
|
-
pub struct RRuleTimezone {
|
|
7
|
-
tz: rrule::Tz,
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
#[napi]
|
|
11
|
-
impl RRuleTimezone {
|
|
12
|
-
#[napi(constructor)]
|
|
13
|
-
pub fn new(tz: String) -> napi::Result<Self> {
|
|
14
|
-
Ok(tz.parse()?)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
pub fn new_with_tz(tz: rrule::Tz) -> Self {
|
|
18
|
-
RRuleTimezone { tz }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The name of the timezone. If the timezone is local, it will return "Local".
|
|
23
|
-
*/
|
|
24
|
-
#[napi(getter)]
|
|
25
|
-
pub fn name(&self) -> String {
|
|
26
|
-
self.tz.name().to_string()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
#[napi(getter)]
|
|
30
|
-
pub fn is_local(&self) -> bool {
|
|
31
|
-
self.tz.is_local()
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
impl Into<rrule::Tz> for RRuleTimezone {
|
|
36
|
-
fn into(self) -> rrule::Tz {
|
|
37
|
-
self.tz
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
impl FromStr for RRuleTimezone {
|
|
42
|
-
type Err = napi::Error;
|
|
43
|
-
|
|
44
|
-
fn from_str(str: &str) -> Result<Self, Self::Err> {
|
|
45
|
-
str
|
|
46
|
-
.parse::<chrono_tz::Tz>()
|
|
47
|
-
.map(|tz| RRuleTimezone {
|
|
48
|
-
tz: rrule::Tz::Tz(tz),
|
|
49
|
-
})
|
|
50
|
-
.map_err(|err| napi::Error::new(napi::Status::GenericFailure, err.to_string()))
|
|
51
|
-
}
|
|
52
|
-
}
|