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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// package com.google.zxing.client.result;
// import java.text.DateFormat;
// import java.text.ParseException;
// import java.text.SimpleDateFormat;
// import java.util.Calendar;
// import java.util.Date;
// import java.util.GregorianCalendar;
// import java.util.Locale;
// import java.util.TimeZone;
// import java.util.regex.Matcher;
// import java.util.regex.Pattern;
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use chrono_tz::Tz;
use once_cell::sync::Lazy;
use regex::Regex;
use crate::common::Result;
use crate::exceptions::Exceptions;
use super::{maybe_append_multiple, maybe_append_string, ParsedRXingResult, ParsedRXingResultType};
// const RFC2445_DURATION: &'static str =
// "P(?:(\\d+)W)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?)?";
const RFC2445_DURATION_FIELD_UNITS: [i64; 5] = [
7 * 24 * 60 * 60 * 1000, // 1 week
24 * 60 * 60 * 1000, // 1 day
60 * 60 * 1000, // 1 hour
60 * 1000, // 1 minute
1000, // 1 second
];
static DATE_TIME: Lazy<Regex> = Lazy::new(|| Regex::new("[0-9]{8}(T[0-9]{6}Z?)?").unwrap());
static RFC2445_DURATION: Lazy<Regex> = Lazy::new(|| {
Regex::new("P(?:(\\d+)W)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?)?").unwrap()
});
// const DATE_TIME: &'static str = "[0-9]{8}(T[0-9]{6}Z?)?";
/**
* Represents a parsed result that encodes a calendar event at a certain time, optionally
* with attendees and a location.
*
* @author Sean Owen
*/
#[derive(Debug)]
pub struct CalendarParsedRXingResult {
summary: String,
start: i64,
startAllDay: bool,
end: i64,
endAllDay: bool,
location: String,
organizer: String,
attendees: Vec<String>,
description: String,
latitude: f64,
longitude: f64,
}
impl ParsedRXingResult for CalendarParsedRXingResult {
fn getType(&self) -> super::ParsedRXingResultType {
ParsedRXingResultType::CALENDAR
}
fn getDisplayRXingResult(&self) -> String {
let mut result = String::with_capacity(100);
maybe_append_string(&self.summary, &mut result);
maybe_append_string(
&Self::format_event(self.startAllDay, self.start),
&mut result,
);
maybe_append_string(&Self::format_event(self.endAllDay, self.end), &mut result);
maybe_append_string(&self.location, &mut result);
maybe_append_string(&self.organizer, &mut result);
maybe_append_multiple(&self.attendees, &mut result);
maybe_append_string(&self.description, &mut result);
result
}
}
impl CalendarParsedRXingResult {
#[allow(clippy::too_many_arguments)]
pub fn new(
summary: String,
startString: String,
endString: String,
durationString: String,
location: String,
organizer: String,
attendees: Vec<String>,
description: String,
latitude: f64,
longitude: f64,
) -> Result<Self> {
let start = Self::parseDate(startString.clone())?;
let end = if endString.is_empty() {
let durationMS = Self::parseDurationMS(&durationString)?;
if durationMS < 0 {
-1
} else {
start + (durationMS / 1000)
}
} else {
Self::parseDate(endString.clone())?
};
// try {
// this.start = parseDate(startString);
// } catch (ParseException pe) {
// throw new IllegalArgumentException(pe.toString());
// }
// if (endString == null) {
// long durationMS = parseDurationMS(durationString);
// end = durationMS < 0L ? -1L : start + durationMS;
// } else {
// try {
// this.end = parseDate(endString);
// } catch (ParseException pe) {
// throw new IllegalArgumentException(pe.toString());
// }
// }
let startAllDay = startString.len() == 8;
let endAllDay = !endString.is_empty() && endString.len() == 8;
Ok(Self {
summary,
start,
startAllDay,
end,
endAllDay,
location,
organizer,
attendees,
description,
latitude,
longitude,
})
}
/**
* Parses a string as a date. RFC 2445 allows the start and end fields to be of type DATE (e.g. 20081021)
* or DATE-TIME (e.g. 20081021T123000 for local time, or 20081021T123000Z for UTC).
*
* @param when The string to parse
* @throws ParseException if not able to parse as a date
*/
fn parseDate(when: String) -> Result<i64> {
if !DATE_TIME.is_match(&when) {
return Err(Exceptions::parse_with(when));
}
if when.len() == 8 {
// Show only year/month/day
let date_format_string = "%Y%m%dT%H%M%SZ";
// DateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
// For dates without a time, for purposes of interacting with Android, the resulting timestamp
// needs to be midnight of that day in GMT. See:
// http://code.google.com/p/android/issues/detail?id=8330
return match Utc.datetime_from_str(&format!("{}T000000Z", &when,), date_format_string) {
Ok(dtm) => Ok(dtm.timestamp()),
Err(e) => Err(Exceptions::parse_with(e.to_string())),
};
}
// The when string can be local time, or UTC if it ends with a Z
if when.len() == 16
&& when
.chars()
.nth(15)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
== 'Z'
{
return match Utc.datetime_from_str(&when, "%Y%m%dT%H%M%SZ") {
Ok(dtm) => Ok(dtm.with_timezone(&Utc).timestamp()),
Err(e) => Err(Exceptions::parse_with(format!(
"couldn't parse string: {e}"
))),
};
}
// Try once more, with weird tz formatting
if when.len() > 16 {
let time_part = &when[..15];
let tz_part = &when[15..];
let tz_parsed: Tz = match tz_part.parse() {
Ok(time_zone) => time_zone,
Err(e) => {
return Err(Exceptions::parse_with(format!(
"couldn't parse timezone '{tz_part}': {e}"
)))
}
};
return match Utc.datetime_from_str(time_part, "%Y%m%dT%H%M%S") {
Ok(dtm) => Ok(dtm.with_timezone(&tz_parsed).timestamp()),
Err(e) => Err(Exceptions::parse_with(format!(
"couldn't parse string: {e}"
))),
};
}
// Try a final time with an exact length
if when.len() == 15 {
return match Utc.datetime_from_str(&when, "%Y%m%dT%H%M%S") {
Ok(dtm) => Ok(dtm.timestamp()),
Err(e) => Err(Exceptions::parse_with(format!(
"couldn't parse local time: {e}"
))),
};
}
Self::parseDateTimeString(&when)
}
fn format_event(allDay: bool, date: i64) -> String {
if date < 0 {
return String::default();
}
let format_string = if allDay { "%F" } else { "%c" };
// DateFormat format = allDay
// ? DateFormat.getDateInstance(DateFormat.MEDIUM)
// : DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
// return format.format(date);
if let Some(dtm) = NaiveDateTime::from_timestamp_opt(date, 0) {
dtm.format(format_string).to_string()
} else {
String::default()
}
}
fn parseDurationMS(durationString: &str) -> Result<i64> {
if durationString.is_empty() {
return Ok(-1);
}
// let regex = Regex::new(RFC2445_DURATION).unwrap();
if let Some(m) = RFC2445_DURATION.captures(durationString) {
let mut durationMS: i64 = 0;
for (i, unit) in RFC2445_DURATION_FIELD_UNITS.iter().enumerate() {
// for i in 0..RFC2445_DURATION_FIELD_UNITS.len() {
// for (int i = 0; i < RFC2445_DURATION_FIELD_UNITS.length; i++) {
let fieldValue = m.get(i + 1);
if let Some(parseable) = fieldValue {
let z = parseable
.as_str()
.parse::<i64>()
.map_err(|e| Exceptions::parse_with(e.to_string()))?;
durationMS += unit * z;
}
}
Ok(durationMS)
} else {
Ok(-1)
}
// if (!m.matches()) {
// return -1L;
// }
// long durationMS = 0L;
// for (int i = 0; i < RFC2445_DURATION_FIELD_UNITS.length; i++) {
// String fieldValue = m.group(i + 1);
// if (fieldValue != null) {
// durationMS += RFC2445_DURATION_FIELD_UNITS[i] * Integer.parseInt(fieldValue);
// }
// }
// return durationMS;
}
fn parseDateTimeString(dateTimeString: &str) -> Result<i64> {
if let Ok(dtm) = DateTime::parse_from_str(dateTimeString, "%Y%m%dT%H%M%S") {
Ok(dtm.timestamp())
} else {
Err(Exceptions::parse_with(format!(
"Couldn't parse {dateTimeString}"
)))
}
// DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
// return format.parse(dateTimeString).getTime();
}
pub fn getSummary(&self) -> &String {
&self.summary
}
/**
* @return start time
* @see #getEndTimestamp()
*/
pub fn getStartTimestamp(&self) -> i64 {
self.start
}
/**
* @return true if start time was specified as a whole day
*/
pub fn isStartAllDay(&self) -> bool {
self.startAllDay
}
/**
* @return event end {@link Date}, or -1 if event has no duration
* @see #getStartTimestamp()
*/
pub fn getEndTimestamp(&self) -> i64 {
self.end
}
/**
* @return true if end time was specified as a whole day
*/
pub fn isEndAllDay(&self) -> bool {
self.endAllDay
}
pub fn getLocation(&self) -> &str {
&self.location
}
pub fn getOrganizer(&self) -> &str {
&self.organizer
}
pub fn getAttendees(&self) -> &Vec<String> {
&self.attendees
}
pub fn getDescription(&self) -> &str {
&self.description
}
pub fn getLatitude(&self) -> f64 {
self.latitude
}
pub fn getLongitude(&self) -> f64 {
self.longitude
}
}
impl PartialEq for CalendarParsedRXingResult {
fn eq(&self, other: &Self) -> bool {
self.summary == other.summary
&& self.start == other.start
&& self.startAllDay == other.startAllDay
&& self.end == other.end
&& self.endAllDay == other.endAllDay
&& self.location == other.location
&& self.organizer == other.organizer
&& self.attendees == other.attendees
&& self.description == other.description
&& self.latitude == other.latitude
&& self.longitude == other.longitude
}
}
impl Eq for CalendarParsedRXingResult {}