all client parsing tests pass except known upce

This commit is contained in:
Henry Schimke
2022-09-16 16:56:22 -05:00
parent ed646d3a63
commit f0e83abf7e
6 changed files with 125 additions and 105 deletions

View File

@@ -47,7 +47,8 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
let phoneNumbers = matchMultipleValuePrefix("TEL", &rawText); let phoneNumbers = matchMultipleValuePrefix("TEL", &rawText);
let emails = matchMultipleValuePrefix("MAIL", &rawText); let emails = matchMultipleValuePrefix("MAIL", &rawText);
let note = ResultParser::matchSinglePrefixedField("MEMORY:", &rawText, '\r', false)?; let note = ResultParser::matchSinglePrefixedField("MEMORY:", &rawText, '\r', false)
.unwrap_or_default();
let address = ResultParser::matchSinglePrefixedField("ADD:", &rawText, '\r', true); let address = ResultParser::matchSinglePrefixedField("ADD:", &rawText, '\r', true);
let addresses = if address.is_none() { let addresses = if address.is_none() {
Vec::new() Vec::new()
@@ -55,7 +56,11 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
vec![address?] vec![address?]
}; };
if let Ok(new_data) = AddressBookParsedRXingResult::with_details( if let Ok(new_data) = AddressBookParsedRXingResult::with_details(
ResultParser::maybeWrap(name)?, if let Some(nm) = ResultParser::maybeWrap(name) {
nm
} else {
Vec::new()
},
Vec::new(), Vec::new(),
"".to_owned(), "".to_owned(),
phoneNumbers, phoneNumbers,

View File

@@ -27,7 +27,7 @@
// import java.util.regex.Matcher; // import java.util.regex.Matcher;
// import java.util.regex.Pattern; // import java.util.regex.Pattern;
use chrono::{Date, DateTime, NaiveDateTime, TimeZone, Utc}; use chrono::{Date, DateTime, Local, NaiveDateTime, TimeZone, Utc};
use chrono_tz::Tz; use chrono_tz::Tz;
use regex::Regex; use regex::Regex;
@@ -229,6 +229,17 @@ impl CalendarParsedRXingResult {
))), ))),
}; };
} }
// 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::ParseException(format!(
"couldn't parse local time: {}",
e
))),
};
}
Self::parseDateTimeString(&when) Self::parseDateTimeString(&when)
} }

View File

@@ -27,7 +27,7 @@
// import java.util.Locale; // import java.util.Locale;
// import java.util.TimeZone; // import java.util.TimeZone;
use chrono::{TimeZone, Utc}; use chrono::{Local, TimeZone, Utc};
use crate::{client::result::ParsedRXingResult, BarcodeFormat, RXingResult}; use crate::{client::result::ParsedRXingResult, BarcodeFormat, RXingResult};
@@ -390,9 +390,9 @@ fn test_uri() {
#[test] #[test]
fn test_geo() { fn test_geo() {
do_test_rxing_result("geo:1,2", "1.0, 2.0", ParsedRXingResultType::GEO); do_test_rxing_result("geo:1,2", "1, 2", ParsedRXingResultType::GEO);
do_test_rxing_result("GEO:1,2", "1.0, 2.0", ParsedRXingResultType::GEO); do_test_rxing_result("GEO:1,2", "1, 2", ParsedRXingResultType::GEO);
do_test_rxing_result("geo:1,2,3", "1.0, 2.0, 3.0m", ParsedRXingResultType::GEO); do_test_rxing_result("geo:1,2,3", "1, 2, 3m", ParsedRXingResultType::GEO);
do_test_rxing_result( do_test_rxing_result(
"geo:80.33,-32.3344,3.35", "geo:80.33,-32.3344,3.35",
"80.33, -32.3344, 3.35m", "80.33, -32.3344, 3.35m",
@@ -501,7 +501,7 @@ fn test_vevent() {
fn format_date(year: i32, month: u32, day: u32) -> String { fn format_date(year: i32, month: u32, day: u32) -> String {
let dtm = Utc.ymd(year, month, day); let dtm = Utc.ymd(year, month, day);
dtm.format("%Y%m%D").to_string() dtm.format("%F").to_string()
// Calendar cal = Calendar.getInstance(); // Calendar cal = Calendar.getInstance();
// cal.clear(); // cal.clear();
// cal.set(year, month - 1, day); // cal.set(year, month - 1, day);
@@ -510,7 +510,7 @@ fn format_date(year: i32, month: u32, day: u32) -> String {
fn format_time(year: i32, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> String { fn format_time(year: i32, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> String {
let dtm = Utc.ymd(year, month, day).and_hms(hour, min, sec); let dtm = Utc.ymd(year, month, day).and_hms(hour, min, sec);
dtm.format("%C").to_string() dtm.format("%c").to_string()
// Calendar cal = Calendar.getInstance(); // Calendar cal = Calendar.getInstance();
// cal.clear(); // cal.clear();
// cal.set(year, month - 1, day, hour, min, sec); // cal.set(year, month - 1, day, hour, min, sec);
@@ -624,7 +624,12 @@ fn test_mmsto() {
} }
fn do_test_rxing_result(contents: &str, golden_rxing_result: &str, r_type: ParsedRXingResultType) { fn do_test_rxing_result(contents: &str, golden_rxing_result: &str, r_type: ParsedRXingResultType) {
do_test_rxing_result_long(contents, golden_rxing_result, r_type, BarcodeFormat::QR_CODE); do_test_rxing_result_long(
contents,
golden_rxing_result,
r_type,
BarcodeFormat::QR_CODE,
);
// QR code is arbitrary // QR code is arbitrary
} }

View File

@@ -35,10 +35,11 @@ use crate::{exceptions::Exceptions, RXingResult};
use super::{ use super::{
AddressBookAUResultParser, AddressBookDoCoMoResultParser, BizcardResultParser, AddressBookAUResultParser, AddressBookDoCoMoResultParser, BizcardResultParser,
BookmarkDoCoMoResultParser, EmailAddressResultParser, EmailDoCoMoResultParser, GeoResultParser, BookmarkDoCoMoResultParser, EmailAddressResultParser, EmailDoCoMoResultParser,
ISBNResultParser, ParsedClientResult, ParsedRXingResult, ProductResultParser, ExpandedProductResultParser, GeoResultParser, ISBNResultParser, ParsedClientResult,
SMSMMSResultParser, SMTPResultParser, TelResultParser, TextParsedRXingResult, URIResultParser, ParsedRXingResult, ProductResultParser, SMSMMSResultParser, SMTPResultParser, TelResultParser,
URLTOResultParser, VCardResultParser, VEventResultParser, VINResultParser, WifiResultParser, ExpandedProductResultParser, TextParsedRXingResult, URIResultParser, URLTOResultParser, VCardResultParser,
VEventResultParser, VINResultParser, WifiResultParser, SMSTOMMSTOResultParser,
}; };
/** /**
@@ -130,14 +131,14 @@ pub fn parseRXingResult(the_rxing_result: &RXingResult) -> ParsedClientResult {
&SMTPResultParser::parse, &SMTPResultParser::parse,
&TelResultParser::parse, &TelResultParser::parse,
&SMSMMSResultParser::parse, &SMSMMSResultParser::parse,
&SMSMMSResultParser::parse, &SMSTOMMSTOResultParser::parse,
&GeoResultParser::parse, &GeoResultParser::parse,
&WifiResultParser::parse, &WifiResultParser::parse,
&URLTOResultParser::parse, &URLTOResultParser::parse,
&URIResultParser::parse, &URIResultParser::parse,
&ISBNResultParser::parse, &ISBNResultParser::parse,
&ProductResultParser::parse, &ProductResultParser::parse,
& ExpandedProductResultParser::parse, &ExpandedProductResultParser::parse,
&VINResultParser::parse, &VINResultParser::parse,
]; ];
@@ -161,7 +162,9 @@ pub fn parseRXingResult(the_rxing_result: &RXingResult) -> ParsedClientResult {
pub fn maybe_append_string(value: &str, result: &mut String) { pub fn maybe_append_string(value: &str, result: &mut String) {
if !value.is_empty() { if !value.is_empty() {
result.push('\n'); if !result.is_empty() {
result.push('\n');
}
result.push_str(value); result.push_str(value);
} }
} }
@@ -171,7 +174,9 @@ pub fn maybe_append_multiple(value: &[String], result: &mut String) {
for s in value { for s in value {
// for (String s : value) { // for (String s : value) {
if !s.is_empty() { if !s.is_empty() {
result.push('\n'); if !result.is_empty() {
result.push('\n');
}
result.push_str(s); result.push_str(s);
} }
} }

View File

@@ -95,7 +95,7 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
add_number_via( add_number_via(
&mut numbers, &mut numbers,
&mut vias, &mut vias,
&sms_uriwithout_query[(last_comma) as usize..], &sms_uriwithout_query[(if last_comma > 0 { last_comma + 1} else {last_comma}) as usize..],
); );
Some(ParsedClientResult::SMSResult( Some(ParsedClientResult::SMSResult(

View File

@@ -24,106 +24,100 @@ use super::{ParsedRXingResult, ParsedRXingResultType, ResultParser};
* *
* @author Sean Owen * @author Sean Owen
*/ */
pub struct SMSParsedRXingResult { pub struct SMSParsedRXingResult {
numbers: Vec<String>,
numbers: Vec<String>, vias: Vec<String>,
vias: Vec<String>, subject: String,
subject:String, body: String,
body:String,
} }
impl ParsedRXingResult for SMSParsedRXingResult { impl ParsedRXingResult for SMSParsedRXingResult {
fn getType(&self) -> super::ParsedRXingResultType { fn getType(&self) -> super::ParsedRXingResultType {
ParsedRXingResultType::SMS ParsedRXingResultType::SMS
} }
fn getDisplayRXingResult(&self) -> String { fn getDisplayRXingResult(&self) -> String {
let mut result = String::with_capacity(100); let mut result = String::with_capacity(100);
ResultParser::maybe_append_multiple(&self.numbers, &mut result); ResultParser::maybe_append_multiple(&self.numbers, &mut result);
ResultParser::maybe_append_string(&self.subject, &mut result); ResultParser::maybe_append_string(&self.subject, &mut result);
ResultParser::maybe_append_string(&self.body, &mut result); ResultParser::maybe_append_string(&self.body, &mut result);
result result
} }
} }
impl SMSParsedRXingResult { impl SMSParsedRXingResult {
pub fn with_singles(number: String, via: String, subject: String, body: String) -> Self {
pub fn with_singles( number:String, Self {
via:String, numbers: vec![number],
subject:String, vias: vec![via],
body:String) -> Self { subject,
Self{ body,
numbers: vec![number],
vias: vec![via],
subject,
body,
}
}
pub fn with_arrays( numbers :Vec<String>,
vias:Vec<String>,
subject:String,
body:String) -> Self{
Self {
numbers,
vias,
subject,
body,
}
}
pub fn getSMSURI(&self) -> String {
let mut result = String::new();
result.push_str("sms:");
let mut first = true;
for i in 0..self.numbers.len() {
// for (int i = 0; i < numbers.length; i++) {
if first {
first = false;
} else {
result.push(',');
}
result.push_str(&self.numbers[i]);
if !self.vias.is_empty() {
result.push_str(";via=");
result.push_str(&self.vias[i]);
}
}
let has_body = !self.body.is_empty();
let has_subject = !self.subject.is_empty();
if has_body || has_subject {
result.push('?');
if has_body {
result.push_str("body=");
result.push_str(&self.body);
}
if has_subject {
if has_body {
result.push('&');
} }
result.push_str("subject=");
result.push_str(&self.subject);
}
} }
result
}
pub fn getNumbers(&self) -> &Vec<String> { pub fn with_arrays(
&self.numbers numbers: Vec<String>,
} vias: Vec<String>,
subject: String,
body: String,
) -> Self {
Self {
numbers,
vias,
subject,
body,
}
}
pub fn getVias(&self) -> &Vec<String> { pub fn getSMSURI(&self) -> String {
&self. vias let mut result = String::new();
} result.push_str("sms:");
let mut first = true;
for i in 0..self.numbers.len() {
// for (int i = 0; i < numbers.length; i++) {
if first {
first = false;
} else {
result.push(',');
}
result.push_str(&self.numbers[i]);
if !self.vias.is_empty() {
result.push_str(";via=");
result.push_str(&self.vias[i]);
}
}
let has_body = !self.body.is_empty();
let has_subject = !self.subject.is_empty();
if has_body || has_subject {
result.push('?');
if has_body {
result.push_str("body=");
result.push_str(&self.body);
}
if has_subject {
if has_body {
result.push('&');
}
result.push_str("subject=");
result.push_str(&self.subject);
}
}
result
}
pub fn getSubject(&self) -> &str { pub fn getNumbers(&self) -> &Vec<String> {
&self. subject &self.numbers
} }
pub fn getBody(&self) -> &str { pub fn getVias(&self) -> &Vec<String> {
&self. body &self.vias
} }
pub fn getSubject(&self) -> &str {
&self.subject
}
pub fn getBody(&self) -> &str {
&self.body
}
} }