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(); } diff --git a/src/client/result/TelParsedResultTestCase.rs b/src/client/result/TelParsedResultTestCase.rs index 487bd27..4010d09 100644 --- a/src/client/result/TelParsedResultTestCase.rs +++ b/src/client/result/TelParsedResultTestCase.rs @@ -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()); diff --git a/src/client/result/WifiParsedResult.java b/src/client/result/WifiParsedResult.java deleted file mode 100644 index 4677fc0..0000000 --- a/src/client/result/WifiParsedResult.java +++ /dev/null @@ -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(); - } - -} diff --git a/src/client/result/WifiParsedResult.rs b/src/client/result/WifiParsedResult.rs new file mode 100644 index 0000000..4e0d8d1 --- /dev/null +++ b/src/client/result/WifiParsedResult.rs @@ -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 + } +} diff --git a/src/client/result/WifiParsedResultTestCase.java b/src/client/result/WifiParsedResultTestCase.rs similarity index 62% rename from src/client/result/WifiParsedResultTestCase.java rename to src/client/result/WifiParsedResultTestCase.rs index bda48fb..369540d 100644 --- a/src/client/result/WifiParsedResultTestCase.java +++ b/src/client/result/WifiParsedResultTestCase.rs @@ -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"); + } } -} +// } diff --git a/src/client/result/WifiResultParser.java b/src/client/result/WifiResultParser.java deleted file mode 100644 index 89df370..0000000 --- a/src/client/result/WifiResultParser.java +++ /dev/null @@ -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") -/** - *
Parses a WIFI configuration string. Strings will be of the form:
- * - *{@code WIFI:T:[network type];S:[network SSID];P:[network password];H:[hidden?];;}
- * - *For WPA2 enterprise (EAP), strings will be of the form:
- * - *{@code WIFI:T:WPA2-EAP;S:[network SSID];H:[hidden?];E:[EAP method];PH2:[Phase 2 method];A:[anonymous identity];I:[username];P:[password];;}
- * - *"EAP method" can e.g. be "TTLS" or "PWD" or one of the other fields in WifiEnterpriseConfig.Eap and "Phase 2 method" can e.g. be "MSCHAPV2" or any of the other fields in WifiEnterpriseConfig.Phase2
- * - *The fields can appear in any order. Only "S:" is required.
- * - * @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); - } -} diff --git a/src/client/result/WifiResultParser.rs b/src/client/result/WifiResultParser.rs new file mode 100644 index 0000000..c633957 --- /dev/null +++ b/src/client/result/WifiResultParser.rs @@ -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") +/** + *Parses a WIFI configuration string. Strings will be of the form:
+ * + *{@code WIFI:T:[network type];S:[network SSID];P:[network password];H:[hidden?];;}
+ * + *For WPA2 enterprise (EAP), strings will be of the form:
+ * + *{@code WIFI:T:WPA2-EAP;S:[network SSID];H:[hidden?];E:[EAP method];PH2:[Phase 2 method];A:[anonymous identity];I:[username];P:[password];;}
+ * + *"EAP method" can e.g. be "TTLS" or "PWD" or one of the other fields in WifiEnterpriseConfig.Eap and "Phase 2 method" can e.g. be "MSCHAPV2" or any of the other fields in WifiEnterpriseConfig.Phase2
+ * + *The fields can appear in any order. Only "S:" is required.
+ * + * @author Vikram Aggarwal + * @author Sean Owen + * @author Steffen Kieß + */ +pub struct WifiRXingResultParser {} + +impl RXingResultParser for WifiRXingResultParser { + fn parse(&self, theRXingResult: &crate::RXingResult) -> Option