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
/*
* Copyright 2007 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.Map;
// import java.util.regex.Pattern;
use regex::Regex;
use crate::RXingResult;
use once_cell::sync::Lazy;
static COMMA: Lazy<Regex> = Lazy::new(|| Regex::new(",").unwrap());
static ATEXT_ALPHANUMERIC: Lazy<Regex> =
Lazy::new(|| Regex::new("[a-zA-Z0-9@.!#$%&'*+\\-/=?^_`{|}~]+").unwrap());
use super::{
EmailAddressParsedRXingResult, EmailDoCoMoResultParser, ParsedClientResult, ResultParser,
};
/**
* Represents a result that encodes an e-mail address, either as a plain address
* like "joe@example.org" or a mailto: URL like "mailto:joe@example.org".
*
* @author Sean Owen
*/
pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
let rawText = ResultParser::getMassagedText(result);
if rawText.starts_with("mailto:") || rawText.starts_with("MAILTO:") {
// If it starts with mailto:, assume it is definitely trying to be an email address
let mut hostEmail = &rawText[7..];
if let Some(queryStart) = hostEmail.find('?') {
hostEmail = &hostEmail[..queryStart];
}
// int queryStart = hostEmail.indexOf('?');
// if (queryStart >= 0) {
// hostEmail = hostEmail.substring(0, queryStart);
// }
// try {
let tmp = ResultParser::urlDecode(hostEmail).ok()?;
hostEmail = tmp.as_str();
// } catch (IllegalArgumentException iae) {
// return null;
// }
let mut tos = if hostEmail.is_empty() {
Vec::new()
} else {
COMMA.split(hostEmail).map(|s| s.to_owned()).collect()
};
// if (!hostEmail.isEmpty()) {
// tos = COMMA.split(hostEmail);
// }
let nameValues = ResultParser::parseNameValuePairs(&rawText);
let mut ccs: Vec<String> = Vec::new();
let mut bccs: Vec<String> = Vec::new();
let mut subject = String::default();
let mut body = String::default();
if let Some(nv) = nameValues {
// if (nameValues != null) {
if tos.is_empty() {
if let Some(tosString) = nv.get("to") {
tos = COMMA.split(tosString).map(|s| s.to_owned()).collect();
}
// if tosString != null {
// tos = COMMA.split(tosString);
// }
}
if let Some(ccString) = nv.get("cc") {
ccs = COMMA.split(ccString).map(|s| s.to_owned()).collect();
}
// if ccString != null {
// ccs = COMMA.split(ccString);
// }
if let Some(bccString) = nv.get("bcc") {
bccs = COMMA.split(bccString).map(|s| s.to_owned()).collect();
}
// if bccString != null {
// bccs = COMMA.split(bccString);
// }
subject = nv.get("subject").unwrap_or(&String::default()).clone();
body = nv.get("body").unwrap_or(&String::default()).clone();
}
Some(ParsedClientResult::EmailResult(
EmailAddressParsedRXingResult::with_details(tos, ccs, bccs, subject, body),
))
} else {
// let atext_alphanumeric = Regex::new("[a-zA-Z0-9@.!#$%&'*+\\-/=?^_`{|}~]+").unwrap();
if !EmailDoCoMoResultParser::isBasicallyValidEmailAddress(&rawText, &ATEXT_ALPHANUMERIC) {
return None;
}
Some(ParsedClientResult::EmailResult(
EmailAddressParsedRXingResult::new(rawText),
))
}
}