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
/*
* 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 com.google.zxing.RXingResult;
// import java.util.List;
use crate::RXingResult;
use super::{CalendarParsedRXingResult, ParsedClientResult, ResultParser, VCardResultParser};
/**
* Partially implements the iCalendar format's "VEVENT" format for specifying a
* calendar event. See RFC 2445. This supports SUMMARY, LOCATION, GEO, DTSTART and DTEND fields.
*
* @author Sean Owen
*/
pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
let rawText = ResultParser::getMassagedText(result);
if !rawText.contains("BEGIN:VEVENT") {
return None;
}
// if (vEventStart < 0) {
// return null;
// }
let summary = matchSingleVCardPrefixedField("SUMMARY", &rawText);
let start = matchSingleVCardPrefixedField("DTSTART", &rawText);
if start.is_empty() {
return None;
}
let end = matchSingleVCardPrefixedField("DTEND", &rawText);
let duration = matchSingleVCardPrefixedField("DURATION", &rawText);
let location = matchSingleVCardPrefixedField("LOCATION", &rawText);
let organizer = stripMailto(&matchSingleVCardPrefixedField("ORGANIZER", &rawText));
let mut attendees = matchVCardPrefixedField("ATTENDEE", &rawText);
if !attendees.is_empty() {
for attendee in &mut attendees {
// for i in 0..attendees.len() {
// for (int i = 0; i < attendees.length; i++) {
*attendee = stripMailto(attendee);
}
}
let description = matchSingleVCardPrefixedField("DESCRIPTION", &rawText);
let geoString = matchSingleVCardPrefixedField("GEO", &rawText);
let latitude;
let longitude;
if geoString.is_empty() {
latitude = f64::NAN;
longitude = f64::NAN;
} else if let Some(semicolon) = geoString.find(';') {
latitude = if let Ok(l) = geoString[..semicolon].parse() {
l
} else {
return None;
};
longitude = if let Ok(l) = geoString[semicolon + 1..].parse() {
l
} else {
return None;
}
} else {
return None;
}
if let Ok(cpr) = CalendarParsedRXingResult::new(
summary,
start,
end,
duration,
location,
organizer,
attendees,
description,
latitude,
longitude,
) {
Some(ParsedClientResult::CalendarEventResult(cpr))
} else {
None
}
// try {
// return new CalendarParsedRXingResult(summary,
// start,
// end,
// duration,
// location,
// organizer,
// attendees,
// description,
// latitude,
// longitude);
// } catch (IllegalArgumentException ignored) {
// return null;
// }
}
fn matchSingleVCardPrefixedField(prefix: &str, rawText: &str) -> String {
if let Some(values) =
VCardResultParser::matchSingleVCardPrefixedField(prefix, rawText, true, false)
{
if values.is_empty() {
String::default()
} else {
let tz_mod = if values.len() > 1 {
if let Some(v) = values.get(values.len() - 2) {
if let Some(tz_loc) = v.find("TZID=") {
v[tz_loc + 5..].to_owned()
} else {
String::default()
}
} else {
String::default()
}
} else {
String::default()
};
let root_time = values.last().unwrap().clone();
format!("{root_time}{tz_mod}")
}
} else {
String::default()
}
// return values == null || values.isEmpty() ? null : values.get(0);
}
fn matchVCardPrefixedField(prefix: &str, rawText: &str) -> Vec<String> {
if let Some(values) = VCardResultParser::matchVCardPrefixedField(prefix, rawText, true, false) {
if values.is_empty() {
Vec::new()
} else {
let size = values.len();
let mut result = vec![String::default(); size]; //new String[size];
for (i, res) in result.iter_mut().enumerate().take(size) {
// for i in 0..size {
// for (int i = 0; i < size; i++) {
*res = values.get(i).unwrap().get(0).unwrap().clone();
}
result
}
} else {
Vec::new()
}
// if values == null || values.isEmpty() {
// return null;
// }
// int size = values.size();
// String[] result = new String[size];
// for (int i = 0; i < size; i++) {
// result[i] = values.get(i).get(0);
// }
// return result;
}
fn stripMailto(s: &str) -> String {
let mut s = s;
if !s.is_empty() && (s.starts_with("mailto:") || s.starts_with("MAILTO:")) {
s = &s[7..];
}
s.to_owned()
}