potential rewrite of matchprefixed

This commit is contained in:
Henry Schimke
2022-09-03 17:17:42 -05:00
parent d80ead1d7b
commit 65029818c7
14 changed files with 443 additions and 331 deletions

View File

@@ -14,29 +14,37 @@
* limitations under the License.
*/
package com.google.zxing.client.result;
// package com.google.zxing.client.result;
use super::ParsedRXingResult;
/**
* Represents a parsed result that encodes a product ISBN number.
*
* @author jbreiden@google.com (Jeff Breidenbach)
*/
public final class ISBNParsedRXingResult extends ParsedRXingResult {
pub struct ISBNParsedRXingResult {
private final String isbn;
isbn:String,
}
impl ParsedRXingResult for ISBNParsedRXingResult {
fn getType(&self) -> super::ParsedRXingResultType {
super::ParsedRXingResultType::ISBN
}
ISBNParsedRXingResult(String isbn) {
super(ParsedRXingResultType.ISBN);
this.isbn = isbn;
fn getDisplayRXingResult(&self) -> String {
self.isbn.clone()
}
}
impl ISBNParsedRXingResult {
pub fn new( isbn:String)->Self {
Self{ isbn }
}
public String getISBN() {
return isbn;
}
@Override
public String getDisplayRXingResult() {
return isbn;
pub fn getISBN(&self) -> &str{
&self.isbn
}
}

View File

@@ -1,44 +0,0 @@
/*
* 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.BarcodeFormat;
import com.google.zxing.RXingResult;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests {@link ISBNParsedRXingResult}.
*
* @author Sean Owen
*/
public final class ISBNParsedRXingResultTestCase extends Assert {
@Test
public void testISBN() {
doTest("9784567890123");
}
private static void doTest(String contents) {
RXingResult fakeRXingResult = new RXingResult(contents, null, null, BarcodeFormat.EAN_13);
ParsedRXingResult result = RXingResultParser.parseRXingResult(fakeRXingResult);
assertSame(ParsedRXingResultType.ISBN, result.getType());
ISBNParsedRXingResult isbnRXingResult = (ISBNParsedRXingResult) result;
assertEquals(contents, isbnRXingResult.getISBN());
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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.BarcodeFormat;
// import com.google.zxing.RXingResult;
// import org.junit.Assert;
// import org.junit.Test;
/**
* Tests {@link ISBNParsedRXingResult}.
*
* @author Sean Owen
*/
// public final class ISBNParsedRXingResultTestCase extends Assert {
use crate::{
client::result::{ParsedClientResult, ParsedRXingResult, ParsedRXingResultType},
BarcodeFormat, RXingResult,
};
use super::ResultParser;
#[test]
fn testISBN() {
doTest("9784567890123");
}
fn doTest(contents: &str) {
let fakeRXingResult = RXingResult::new(contents, vec![0; 0], vec![], BarcodeFormat::EAN_13);
let result = ResultParser::parseRXingResult(&fakeRXingResult);
assert_eq!(ParsedRXingResultType::ISBN, result.getType());
if let ParsedClientResult::ISBNResult(res) = result {
assert_eq!(contents, res.getISBN());
} else {
panic!("expected ISBNResult")
}
// ISBNParsedRXingResult isbnRXingResult = (ISBNParsedRXingResult) result;
}
// }

View File

@@ -1,50 +0,0 @@
/*
* 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.BarcodeFormat;
import com.google.zxing.RXingResult;
/**
* Parses strings of digits that represent a ISBN.
*
* @author jbreiden@google.com (Jeff Breidenbach)
*/
public final class ISBNRXingResultParser extends RXingResultParser {
/**
* See <a href="http://www.bisg.org/isbn-13/for.dummies.html">ISBN-13 For Dummies</a>
*/
@Override
public ISBNParsedRXingResult parse(RXingResult result) {
BarcodeFormat format = result.getBarcodeFormat();
if (format != BarcodeFormat.EAN_13) {
return null;
}
String rawText = getMassagedText(result);
int length = rawText.length();
if (length != 13) {
return null;
}
if (!rawText.startsWith("978") && !rawText.startsWith("979")) {
return null;
}
return new ISBNParsedRXingResult(rawText);
}
}

View File

@@ -0,0 +1,55 @@
/*
* 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.BarcodeFormat;
// import com.google.zxing.RXingResult;
use std::result;
use crate::BarcodeFormat;
use super::{RXingResultParser, ResultParser, ISBNParsedRXingResult, ParsedClientResult};
/**
* Parses strings of digits that represent a ISBN.
*
* @author jbreiden@google.com (Jeff Breidenbach)
*/
pub struct ISBNRXingResultParser {}
impl RXingResultParser for ISBNRXingResultParser {
/**
* See <a href="http://www.bisg.org/isbn-13/for.dummies.html">ISBN-13 For Dummies</a>
*/
fn parse(&self, theRXingResult: &crate::RXingResult) -> Option<super::ParsedClientResult> {
let format = theRXingResult.getBarcodeFormat();
if *format != BarcodeFormat::EAN_13 {
return None;
}
let rawText = ResultParser::getMassagedText(theRXingResult);
let length = rawText.len();
if length != 13 {
return None;
}
if !rawText.starts_with("978") && !rawText.starts_with("979") {
return None;
}
Some(ParsedClientResult::ISBNResult(ISBNParsedRXingResult::new(rawText)))
}
}

View File

@@ -33,7 +33,7 @@ use urlencoding::decode;
use crate::{exceptions::Exceptions, RXingResult};
use super::{ParsedClientResult, ParsedRXingResult, TextParsedRXingResult, TelRXingResultParser};
use super::{ParsedClientResult, ParsedRXingResult, TextParsedRXingResult, TelRXingResultParser, ISBNRXingResultParser, WifiRXingResultParser};
/**
* <p>Abstract class representing the result of decoding a barcode, as more than
@@ -98,7 +98,7 @@ pub fn getMassagedText(result: &RXingResult) -> String {
}
pub fn parseRXingResult(theRXingResult: &RXingResult) -> ParsedClientResult {
let PARSERS = [
let PARSERS:[&dyn RXingResultParser;3] = [
// new BookmarkDoCoMoRXingResultParser(),
// new AddressBookDoCoMoRXingResultParser(),
// new EmailDoCoMoRXingResultParser(),
@@ -108,14 +108,14 @@ let PARSERS = [
// new VEventRXingResultParser(),
// new EmailAddressRXingResultParser(),
// new SMTPRXingResultParser(),
TelRXingResultParser{},
&TelRXingResultParser{},
// new SMSMMSRXingResultParser(),
// new SMSTOMMSTORXingResultParser(),
// new GeoRXingResultParser(),
// new WifiRXingResultParser(),
&WifiRXingResultParser{},
// new URLTORXingResultParser(),
// new URIRXingResultParser(),
// new ISBNRXingResultParser(),
&ISBNRXingResultParser{},
// new ProductRXingResultParser(),
// new ExpandedProductRXingResultParser(),
// new VINRXingResultParser(),
@@ -139,7 +139,7 @@ let PARSERS = [
))
}
pub fn maybe_ppend_string(value: &str, result: &mut String) {
pub fn maybe_append_string(value: &str, result: &mut String) {
if !value.is_empty() {
result.push('\n');
result.push_str(value);
@@ -292,13 +292,13 @@ pub fn matchPrefixedField(
let start = i; // Found the start of a match here
let mut more = true;
while more {
let i = if let Some(loc) = rawText[i..].find(endChar) {
i = if let Some(loc) = rawText[i..].find(endChar) {
if countPrecedingBackslashes(rawText, i) % 2 != 0 {
// semicolon was escaped (odd count of preceding backslashes) so continue
i + 1
} else {
// found a match
let mut element = unescapeBackslash(&rawText[start..i + start]);
let mut element = unescapeBackslash(&rawText[start..loc+i]);
if trim {
element = element.to_string().trim().to_owned();
}

View File

@@ -49,9 +49,9 @@ fn doTest(contents: &str, number: &str, title: &str) {
let fakeRXingResult =
RXingResult::new(contents, Vec::new(), Vec::new(), BarcodeFormat::QR_CODE);
let result = ResultParser::parseRXingResult(&fakeRXingResult);
assert_eq!(ParsedRXingResultType::TEL, result.getType());
if let ParsedClientResult::TelResult(telRXingResult) = result {
assert_eq!(ParsedRXingResultType::TEL, telRXingResult.getType());
assert_eq!(number, telRXingResult.getNumber());
assert_eq!(title, telRXingResult.getTitle());
assert_eq!(format!("tel:{}", number), telRXingResult.getTelURI());

View File

@@ -1,104 +0,0 @@
/*
* Copyright 2010 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;
/**
* Represents a parsed result that encodes wifi network information, like SSID and password.
*
* @author Vikram Aggarwal
*/
public final class WifiParsedRXingResult extends ParsedRXingResult {
private final String ssid;
private final String networkEncryption;
private final String password;
private final boolean hidden;
private final String identity;
private final String anonymousIdentity;
private final String eapMethod;
private final String phase2Method;
public WifiParsedRXingResult(String networkEncryption, String ssid, String password) {
this(networkEncryption, ssid, password, false);
}
public WifiParsedRXingResult(String networkEncryption, String ssid, String password, boolean hidden) {
this(networkEncryption, ssid, password, hidden, null, null, null, null);
}
public WifiParsedRXingResult(String networkEncryption,
String ssid,
String password,
boolean hidden,
String identity,
String anonymousIdentity,
String eapMethod,
String phase2Method) {
super(ParsedRXingResultType.WIFI);
this.ssid = ssid;
this.networkEncryption = networkEncryption;
this.password = password;
this.hidden = hidden;
this.identity = identity;
this.anonymousIdentity = anonymousIdentity;
this.eapMethod = eapMethod;
this.phase2Method = phase2Method;
}
public String getSsid() {
return ssid;
}
public String getNetworkEncryption() {
return networkEncryption;
}
public String getPassword() {
return password;
}
public boolean isHidden() {
return hidden;
}
public String getIdentity() {
return identity;
}
public String getAnonymousIdentity() {
return anonymousIdentity;
}
public String getEapMethod() {
return eapMethod;
}
public String getPhase2Method() {
return phase2Method;
}
@Override
public String getDisplayRXingResult() {
StringBuilder result = new StringBuilder(80);
maybeAppend(ssid, result);
maybeAppend(networkEncryption, result);
maybeAppend(password, result);
maybeAppend(Boolean.toString(hidden), result);
return result.toString();
}
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2010 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;
use super::{ParsedRXingResult, ParsedRXingResultType, ResultParser};
/**
* Represents a parsed result that encodes wifi network information, like SSID and password.
*
* @author Vikram Aggarwal
*/
pub struct WifiParsedRXingResult {
ssid: String,
networkEncryption: String,
password: String,
hidden: bool,
identity: String,
anonymousIdentity: String,
eapMethod: String,
phase2Method: String,
}
impl ParsedRXingResult for WifiParsedRXingResult {
fn getType(&self) -> super::ParsedRXingResultType {
ParsedRXingResultType::WIFI
}
fn getDisplayRXingResult(&self) -> String {
let mut result = String::with_capacity(80);
ResultParser::maybe_append_string(&self.ssid, &mut result);
ResultParser::maybe_append_string(&self.networkEncryption, &mut result);
ResultParser::maybe_append_string(&self.password, &mut result);
ResultParser::maybe_append_string(&self.hidden.to_string(), &mut result);
result
}
}
impl WifiParsedRXingResult {
pub fn new(networkEncryption: String, ssid: String, password: String) -> Self {
Self::with_hidden(networkEncryption, ssid, password, false)
}
pub fn with_hidden(
networkEncryption: String,
ssid: String,
password: String,
hidden: bool,
) -> Self {
Self::with_details(
networkEncryption,
ssid,
password,
hidden,
String::from(""),
String::from(""),
String::from(""),
String::from(""),
)
}
pub fn with_details(
networkEncryption: String,
ssid: String,
password: String,
hidden: bool,
identity: String,
anonymousIdentity: String,
eapMethod: String,
phase2Method: String,
) -> Self {
Self {
ssid,
networkEncryption,
password,
hidden,
identity,
anonymousIdentity,
eapMethod,
phase2Method,
}
}
pub fn getSsid(&self) -> &str {
&self.ssid
}
pub fn getNetworkEncryption(&self) -> &str {
&self.networkEncryption
}
pub fn getPassword(&self) -> &str {
&self.password
}
pub fn isHidden(&self) -> bool {
self.hidden
}
pub fn getIdentity(&self) -> &str {
&self.identity
}
pub fn getAnonymousIdentity(&self) -> &str {
&self.anonymousIdentity
}
pub fn getEapMethod(&self) -> &str {
&self.eapMethod
}
pub fn getPhase2Method(&self) -> &str {
&self.phase2Method
}
}

View File

@@ -14,28 +14,32 @@
* limitations under the License.
*/
package com.google.zxing.client.result;
// package com.google.zxing.client.result;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.RXingResult;
import org.junit.Assert;
import org.junit.Test;
// import com.google.zxing.BarcodeFormat;
// import com.google.zxing.RXingResult;
// import org.junit.Assert;
// import org.junit.Test;
/**
* Tests {@link WifiParsedRXingResult}.
*
* @author Vikram Aggarwal
*/
public final class WifiParsedRXingResultTestCase extends Assert {
// public final class WifiParsedRXingResultTestCase extends Assert {
@Test
public void testNoPassword() {
doTest("WIFI:S:NoPassword;P:;T:;;", "NoPassword", null, "nopass");
doTest("WIFI:S:No Password;P:;T:;;", "No Password", null, "nopass");
use crate::{RXingResult, BarcodeFormat, client::result::{ParsedRXingResultType, ParsedRXingResult, ParsedClientResult}};
use super::ResultParser;
#[test]
fn testNoPassword() {
doTest("WIFI:S:NoPassword;P:;T:;;", "NoPassword", "", "nopass");
doTest("WIFI:S:No Password;P:;T:;;", "No Password", "", "nopass");
}
@Test
public void testWep() {
#[test]
fn testWep() {
doTest("WIFI:S:TenChars;P:0123456789;T:WEP;;", "TenChars", "0123456789", "WEP");
doTest("WIFI:S:TenChars;P:abcde56789;T:WEP;;", "TenChars", "abcde56789", "WEP");
// Non hex should not fail at this level
@@ -52,8 +56,8 @@ public final class WifiParsedRXingResultTestCase extends Assert {
/**
* Put in checks for the length of the password for wep.
*/
@Test
public void testWpa() {
#[test]
fn testWpa() {
doTest("WIFI:S:TenChars;P:wow;T:WPA;;", "TenChars", "wow", "WPA");
doTest("WIFI:S:TenChars;P:space is silent;T:WPA;;", "TenChars", "space is silent", "WPA");
doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP");
@@ -64,30 +68,33 @@ public final class WifiParsedRXingResultTestCase extends Assert {
doTest("WIFI:S:TenChars;P:hello\\:there;T:WEP;;", "TenChars", "hello:there", "WEP");
}
@Test
public void testEscape() {
#[test]
fn testEscape() {
doTest("WIFI:T:WPA;S:test;P:my_password\\\\;;", "test", "my_password\\", "WPA");
doTest("WIFI:T:WPA;S:My_WiFi_SSID;P:abc123/;;", "My_WiFi_SSID", "abc123/", "WPA");
doTest("WIFI:T:WPA;S:\"foo\\;bar\\\\baz\";;", "\"foo;bar\\baz\"", null, "WPA");
doTest("WIFI:T:WPA;S:\"foo\\;bar\\\\baz\";;", "\"foo;bar\\baz\"", "", "WPA");
doTest("WIFI:T:WPA;S:test;P:\\\"abcd\\\";;", "test", "\"abcd\"", "WPA");
}
/**
* Given the string contents for the barcode, check that it matches our expectations
*/
private static void doTest(String contents,
String ssid,
String password,
String type) {
RXingResult fakeRXingResult = new RXingResult(contents, null, null, BarcodeFormat.QR_CODE);
ParsedRXingResult result = RXingResultParser.parseRXingResult(fakeRXingResult);
fn doTest( contents:&str,
ssid:&str,
password:&str,
n_type:&str) {
let fakeRXingResult = RXingResult::new(contents, Vec::new(), Vec::new(), BarcodeFormat::QR_CODE);
let result = ResultParser::parseRXingResult(&fakeRXingResult);
// Ensure it is a wifi code
assertSame(ParsedRXingResultType.WIFI, result.getType());
WifiParsedRXingResult wifiRXingResult = (WifiParsedRXingResult) result;
assertEquals(ssid, wifiRXingResult.getSsid());
assertEquals(password, wifiRXingResult.getPassword());
assertEquals(type, wifiRXingResult.getNetworkEncryption());
assert_eq!(ParsedRXingResultType::WIFI, result.getType());
if let ParsedClientResult::WiFiResult(wifiRXingResult) = result{
assert_eq!(ssid, wifiRXingResult.getSsid());
assert_eq!(password, wifiRXingResult.getPassword());
assert_eq!(n_type, wifiRXingResult.getNetworkEncryption());
}else {
panic!("Expected WIFI");
}
}
}
// }

View File

@@ -1,79 +0,0 @@
/*
* Copyright 2010 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;
@SuppressWarnings("checkstyle:lineLength")
/**
* <p>Parses a WIFI configuration string. Strings will be of the form:</p>
*
* <p>{@code WIFI:T:[network type];S:[network SSID];P:[network password];H:[hidden?];;}</p>
*
* <p>For WPA2 enterprise (EAP), strings will be of the form:</p>
*
* <p>{@code WIFI:T:WPA2-EAP;S:[network SSID];H:[hidden?];E:[EAP method];PH2:[Phase 2 method];A:[anonymous identity];I:[username];P:[password];;}</p>
*
* <p>"EAP method" can e.g. be "TTLS" or "PWD" or one of the other fields in <a href="https://developer.android.com/reference/android/net/wifi/WifiEnterpriseConfig.Eap.html">WifiEnterpriseConfig.Eap</a> and "Phase 2 method" can e.g. be "MSCHAPV2" or any of the other fields in <a href="https://developer.android.com/reference/android/net/wifi/WifiEnterpriseConfig.Phase2.html">WifiEnterpriseConfig.Phase2</a></p>
*
* <p>The fields can appear in any order. Only "S:" is required.</p>
*
* @author Vikram Aggarwal
* @author Sean Owen
* @author Steffen Kieß
*/
public final class WifiRXingResultParser extends RXingResultParser {
@Override
public WifiParsedRXingResult parse(RXingResult result) {
String rawText = getMassagedText(result);
if (!rawText.startsWith("WIFI:")) {
return null;
}
rawText = rawText.substring("WIFI:".length());
String ssid = matchSinglePrefixedField("S:", rawText, ';', false);
if (ssid == null || ssid.isEmpty()) {
return null;
}
String pass = matchSinglePrefixedField("P:", rawText, ';', false);
String type = matchSinglePrefixedField("T:", rawText, ';', false);
if (type == null) {
type = "nopass";
}
// Unfortunately, in the past, H: was not just used for boolean 'hidden', but 'phase 2 method'.
// To try to retain backwards compatibility, we set one or the other based on whether the string
// is 'true' or 'false':
boolean hidden = false;
String phase2Method = matchSinglePrefixedField("PH2:", rawText, ';', false);
String hValue = matchSinglePrefixedField("H:", rawText, ';', false);
if (hValue != null) {
// If PH2 was specified separately, or if the value is clearly boolean, interpret it as 'hidden'
if (phase2Method != null || "true".equalsIgnoreCase(hValue) || "false".equalsIgnoreCase(hValue)) {
hidden = Boolean.parseBoolean(hValue);
} else {
phase2Method = hValue;
}
}
String identity = matchSinglePrefixedField("I:", rawText, ';', false);
String anonymousIdentity = matchSinglePrefixedField("A:", rawText, ';', false);
String eapMethod = matchSinglePrefixedField("E:", rawText, ';', false);
return new WifiParsedRXingResult(type, ssid, pass, hidden, identity, anonymousIdentity, eapMethod, phase2Method);
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2010 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;
use crate::client::result::{WifiParsedRXingResult, ParsedClientResult};
use super::{RXingResultParser, ResultParser};
// @SuppressWarnings("checkstyle:lineLength")
/**
* <p>Parses a WIFI configuration string. Strings will be of the form:</p>
*
* <p>{@code WIFI:T:[network type];S:[network SSID];P:[network password];H:[hidden?];;}</p>
*
* <p>For WPA2 enterprise (EAP), strings will be of the form:</p>
*
* <p>{@code WIFI:T:WPA2-EAP;S:[network SSID];H:[hidden?];E:[EAP method];PH2:[Phase 2 method];A:[anonymous identity];I:[username];P:[password];;}</p>
*
* <p>"EAP method" can e.g. be "TTLS" or "PWD" or one of the other fields in <a href="https://developer.android.com/reference/android/net/wifi/WifiEnterpriseConfig.Eap.html">WifiEnterpriseConfig.Eap</a> and "Phase 2 method" can e.g. be "MSCHAPV2" or any of the other fields in <a href="https://developer.android.com/reference/android/net/wifi/WifiEnterpriseConfig.Phase2.html">WifiEnterpriseConfig.Phase2</a></p>
*
* <p>The fields can appear in any order. Only "S:" is required.</p>
*
* @author Vikram Aggarwal
* @author Sean Owen
* @author Steffen Kieß
*/
pub struct WifiRXingResultParser {}
impl RXingResultParser for WifiRXingResultParser {
fn parse(&self, theRXingResult: &crate::RXingResult) -> Option<super::ParsedClientResult> {
const WIFI_TEST : &'static str = "WIFI:";
let rawText_unstripped = ResultParser::getMassagedText(theRXingResult);
if !rawText_unstripped.starts_with(WIFI_TEST) {
return None;
}
let rawText = rawText_unstripped[WIFI_TEST.len()..].to_owned();
let ssid = ResultParser::matchSinglePrefixedField("S:", &rawText, ';', false).unwrap_or(String::from(""));
if ssid.is_empty() {
return None;
}
let pass = ResultParser::matchSinglePrefixedField("P:", &rawText, ';', false).unwrap_or(String::from(""));
let n_type = if let Some(nt) = ResultParser::matchSinglePrefixedField("T:", &rawText, ';', false){
nt
}else {
String::from("nopass")
};
// Unfortunately, in the past, H: was not just used for boolean 'hidden', but 'phase 2 method'.
// To try to retain backwards compatibility, we set one or the other based on whether the string
// is 'true' or 'false':
let mut hidden = false;
let mut phase2Method = ResultParser::matchSinglePrefixedField("PH2:", &rawText, ';', false);
let hValue = if let Some(hv) = ResultParser::matchSinglePrefixedField("H:", &rawText, ';', false){
// If PH2 was specified separately, or if the value is clearly boolean, interpret it as 'hidden'
if phase2Method.is_some() || "true" == hv.to_lowercase() || "false" == hv.to_lowercase() {
hidden = hv.parse().unwrap();//Boolean.parseBoolean(hValue);
} else {
phase2Method = Some(hv.clone());
}
hv
}else {
String::from("")
};
let identity = ResultParser::matchSinglePrefixedField("I:", &rawText, ';', false).unwrap_or(String::from(""));
let anonymousIdentity = ResultParser::matchSinglePrefixedField("A:", &rawText, ';', false).unwrap_or(String::from(""));
let eapMethod = ResultParser::matchSinglePrefixedField("E:", &rawText, ';', false).unwrap_or(String::from(""));
Some(ParsedClientResult::WiFiResult(WifiParsedRXingResult::with_details(n_type, ssid, pass, hidden, identity, anonymousIdentity, eapMethod, phase2Method.unwrap_or(String::from("")))))
}
}

View File

@@ -4,6 +4,12 @@ mod TelParsedResult;
mod TextParsedResult;
mod ParsedResultType;
mod TelResultParser;
mod ISBNParsedResult;
mod ISBNResultParser;
mod WifiParsedResult;
mod WifiResultParser;
use std::fmt;
pub use ParsedResultType::*;
pub use ResultParser::*;
@@ -11,11 +17,52 @@ pub use TelParsedResult::*;
pub use TextParsedResult::*;
pub use ParsedResult::*;
pub use TelResultParser::*;
pub use ISBNParsedResult::*;
pub use ISBNResultParser::*;
pub use WifiParsedResult::*;
pub use WifiResultParser::*;
#[cfg(test)]
mod TelParsedResultTestCase;
#[cfg(test)]
mod ISBNParsedResultTestCase;
#[cfg(test)]
mod WifiParsedResultTestCase;
pub enum ParsedClientResult {
TextResult(TextParsedRXingResult),
TelResult(TelParsedRXingResult)
TelResult(TelParsedRXingResult),
ISBNResult(ISBNParsedRXingResult),
WiFiResult(WifiParsedRXingResult),
}
impl ParsedRXingResult for ParsedClientResult {
fn getType(&self) -> ParsedRXingResultType {
match self {
ParsedClientResult::TextResult(a) => a.getType(),
ParsedClientResult::TelResult(a) => a.getType(),
ParsedClientResult::ISBNResult(a) => a.getType(),
ParsedClientResult::WiFiResult(a) => a.getType(),
}
}
fn getDisplayRXingResult(&self) -> String {
match self {
ParsedClientResult::TextResult(a) => a.getDisplayRXingResult(),
ParsedClientResult::TelResult(a) => a.getDisplayRXingResult(),
ParsedClientResult::ISBNResult(a) => a.getDisplayRXingResult(),
ParsedClientResult::WiFiResult(a) => a.getDisplayRXingResult(),
}
}
}
impl fmt::Display for ParsedClientResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"{}", self.getDisplayRXingResult())
}
}

View File

@@ -44,7 +44,8 @@ mod RGBLuminanceSourceTestCase;
*
* @author Sean Owen
*/
pub enum BarcodeFormat {
#[derive(Debug,PartialEq, Eq,Hash)]
pub enum BarcodeFormat {
/** Aztec 2D barcode format. */
AZTEC,