diff --git a/src/client/result/ISBNParsedResult.java b/src/client/result/ISBNParsedResult.rs similarity index 59% rename from src/client/result/ISBNParsedResult.java rename to src/client/result/ISBNParsedResult.rs index 293e0ea..183d5ca 100644 --- a/src/client/result/ISBNParsedResult.java +++ b/src/client/result/ISBNParsedResult.rs @@ -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 } } diff --git a/src/client/result/ISBNParsedResultTestCase.java b/src/client/result/ISBNParsedResultTestCase.java deleted file mode 100644 index 47c99d5..0000000 --- a/src/client/result/ISBNParsedResultTestCase.java +++ /dev/null @@ -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()); - } - -} \ No newline at end of file diff --git a/src/client/result/ISBNParsedResultTestCase.rs b/src/client/result/ISBNParsedResultTestCase.rs new file mode 100644 index 0000000..a132c65 --- /dev/null +++ b/src/client/result/ISBNParsedResultTestCase.rs @@ -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; +} + +// } diff --git a/src/client/result/ISBNResultParser.java b/src/client/result/ISBNResultParser.java deleted file mode 100644 index ce1b3da..0000000 --- a/src/client/result/ISBNResultParser.java +++ /dev/null @@ -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 ISBN-13 For Dummies - */ - @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); - } - -} diff --git a/src/client/result/ISBNResultParser.rs b/src/client/result/ISBNResultParser.rs new file mode 100644 index 0000000..69532e3 --- /dev/null +++ b/src/client/result/ISBNResultParser.rs @@ -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 ISBN-13 For Dummies + */ + fn parse(&self, theRXingResult: &crate::RXingResult) -> Option { + 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))) + } +} diff --git a/src/client/result/ResultParser.rs b/src/client/result/ResultParser.rs index ea36c61..8cd3609 100644 --- a/src/client/result/ResultParser.rs +++ b/src/client/result/ResultParser.rs @@ -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}; /** *

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 { + 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(""))))) + } +} diff --git a/src/client/result/mod.rs b/src/client/result/mod.rs index a18b5bc..8ee7f42 100644 --- a/src/client/result/mod.rs +++ b/src/client/result/mod.rs @@ -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()) + } } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 55d7ccd..9218d5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,