still working through addresses

This commit is contained in:
Henry Schimke
2022-09-10 14:55:32 -05:00
parent a71138590f
commit 582dd75c02
11 changed files with 896 additions and 633 deletions

View File

@@ -0,0 +1,99 @@
/*
* 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.ArrayList;
// import java.util.List;
use crate::RXingResult;
use super::{AddressBookParsedRXingResult, ParsedClientResult, ResultParser};
/**
* Implements KDDI AU's address book format. See
* <a href="http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html">
* http://www.au.kddi.com/ezfactory/tec/two_dimensions/index.html</a>.
* (Thanks to Yuzo for translating!)
*
* @author Sean Owen
*/
pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
let rawText = ResultParser::getMassagedText(result);
// MEMORY is mandatory; seems like a decent indicator, as does end-of-record separator CR/LF
if !rawText.contains("MEMORY") || !rawText.contains("\r\n") {
return None;
}
// NAME1 and NAME2 have specific uses, namely written name and pronunciation, respectively.
// Therefore we treat them specially instead of as an array of names.
let name = ResultParser::matchSinglePrefixedField("NAME1:", &rawText, '\r', true);
let pronunciation = ResultParser::matchSinglePrefixedField("NAME2:", &rawText, '\r', true);
let phoneNumbers = matchMultipleValuePrefix("TEL", &rawText);
let emails = matchMultipleValuePrefix("MAIL", &rawText);
let note = ResultParser::matchSinglePrefixedField("MEMORY:", &rawText, '\r', false)?;
let address = ResultParser::matchSinglePrefixedField("ADD:", &rawText, '\r', true);
let addresses = if address.is_none() {
Vec::new()
} else {
vec![address?]
};
if let Ok(new_data) = AddressBookParsedRXingResult::with_details(
ResultParser::maybeWrap(name)?,
Vec::new(),
"".to_owned(),
phoneNumbers,
Vec::new(),
Vec::new(),
Vec::new(),
"".to_owned(),
note,
addresses,
Vec::new(),
"".to_owned(),
"".to_owned(),
"".to_owned(),
Vec::new(),
Vec::new(),
) {
Some(ParsedClientResult::AddressBookResult(new_data))
} else {
None
}
}
fn matchMultipleValuePrefix(prefix: &str, rawText: &str) -> Vec<String> {
let mut values = Vec::new();
// For now, always 3, and always trim
for i in 1..=3 {
// for (int i = 1; i <= 3; i++) {
let value = ResultParser::matchSinglePrefixedField(
&format!("{}{}:", prefix, i),
rawText,
'\r',
true,
);
if value.is_none() {
break;
}
values.push(value.unwrap());
}
values
}