diff --git a/Cargo.toml b/Cargo.toml index a399e55..49ee2f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,10 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -regex = "1" +regex = "1.6" encoding = "0.2" rand = "0.8.5" +urlencoding = "2.1.2" image = {version = "0.24.3", optional = true} imageproc = {version = "0.23.0", optional = true} diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 0000000..a1b8547 --- /dev/null +++ b/src/client/mod.rs @@ -0,0 +1 @@ +mod result; \ No newline at end of file diff --git a/src/client/result/ParsedResult.java b/src/client/result/ParsedResult.rs similarity index 60% rename from src/client/result/ParsedResult.java rename to src/client/result/ParsedResult.rs index 6f56dfb..97477d1 100644 --- a/src/client/result/ParsedResult.java +++ b/src/client/result/ParsedResult.rs @@ -14,7 +14,9 @@ * limitations under the License. */ -package com.google.zxing.client.result; +// package com.google.zxing.client.result; + +use super::ParsedRXingResultType; /** *
Abstract class representing the result of decoding a barcode, as more than @@ -27,39 +29,33 @@ package com.google.zxing.client.result; * * @author Sean Owen */ -public abstract class ParsedRXingResult { +pub trait ParsedRXingResult { - private final ParsedRXingResultType type; + // private final ParsedRXingResultType type; - protected ParsedRXingResult(ParsedRXingResultType type) { - this.type = type; - } + // protected ParsedRXingResult(ParsedRXingResultType type) { + // this.type = type; + // } - public final ParsedRXingResultType getType() { - return type; - } + fn getType(&self) -> ParsedRXingResultType; - public abstract String getDisplayRXingResult(); + fn getDisplayRXingResult(&self) -> String; - @Override - public final String toString() { - return getDisplayRXingResult(); - } - - public static void maybeAppend(String value, StringBuilder result) { - if (value != null && !value.isEmpty()) { + fn maybe_append(&self, value :&str, result:&mut String) { + if !value.is_empty() { // Don't add a newline before the first value - if (result.length() > 0) { - result.append('\n'); + if result.len() > 0 { + result.push('\n'); } - result.append(value); + result.push_str(value); } } - public static void maybeAppend(String[] values, StringBuilder result) { - if (values != null) { - for (String value : values) { - maybeAppend(value, result); + fn maybe_append_multiple(&self, values:&[&str], result:&mut String) { + if !values.is_empty() { + for value in values { + // for (String value : values) { + self.maybe_append(value, result); } } } diff --git a/src/client/result/ParsedResultType.java b/src/client/result/ParsedResultType.rs similarity index 91% rename from src/client/result/ParsedResultType.java rename to src/client/result/ParsedResultType.rs index 7fba320..53c9f87 100644 --- a/src/client/result/ParsedResultType.java +++ b/src/client/result/ParsedResultType.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.zxing.client.result; +// package com.google.zxing.client.result; /** * Represents the type of data encoded by a barcode -- from plain text, to a @@ -22,7 +22,7 @@ package com.google.zxing.client.result; * * @author Sean Owen */ -public enum ParsedRXingResultType { +pub enum ParsedRXingResultType { ADDRESSBOOK, EMAIL_ADDRESS, diff --git a/src/client/result/ResultParser.java b/src/client/result/ResultParser.java deleted file mode 100644 index d755d0c..0000000 --- a/src/client/result/ResultParser.java +++ /dev/null @@ -1,261 +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.RXingResult; - -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -/** - *
Abstract class representing the result of decoding a barcode, as more than - * a String -- as some type of structured data. This might be a subclass which represents - * a URL, or an e-mail address. {@link #parseRXingResult(RXingResult)} will turn a raw - * decoded string into the most appropriate type of structured representation.
- * - *Thanks to Jeff Griffin for proposing rewrite of these classes that relies less - * on exception-based mechanisms during parsing.
- * - * @author Sean Owen - */ -public abstract class RXingResultParser { - - private static final RXingResultParser[] PARSERS = { - new BookmarkDoCoMoRXingResultParser(), - new AddressBookDoCoMoRXingResultParser(), - new EmailDoCoMoRXingResultParser(), - new AddressBookAURXingResultParser(), - new VCardRXingResultParser(), - new BizcardRXingResultParser(), - new VEventRXingResultParser(), - new EmailAddressRXingResultParser(), - new SMTPRXingResultParser(), - new TelRXingResultParser(), - new SMSMMSRXingResultParser(), - new SMSTOMMSTORXingResultParser(), - new GeoRXingResultParser(), - new WifiRXingResultParser(), - new URLTORXingResultParser(), - new URIRXingResultParser(), - new ISBNRXingResultParser(), - new ProductRXingResultParser(), - new ExpandedProductRXingResultParser(), - new VINRXingResultParser(), - }; - - private static final Pattern DIGITS = Pattern.compile("\\d+"); - private static final Pattern AMPERSAND = Pattern.compile("&"); - private static final Pattern EQUALS = Pattern.compile("="); - private static final String BYTE_ORDER_MARK = "\ufeff"; - - static final String[] EMPTY_STR_ARRAY = new String[0]; - - /** - * Attempts to parse the raw {@link RXingResult}'s contents as a particular type - * of information (email, URL, etc.) and return a {@link ParsedRXingResult} encapsulating - * the result of parsing. - * - * @param theRXingResult the raw {@link RXingResult} to parse - * @return {@link ParsedRXingResult} encapsulating the parsing result - */ - public abstract ParsedRXingResult parse(RXingResult theRXingResult); - - protected static String getMassagedText(RXingResult result) { - String text = result.getText(); - if (text.startsWith(BYTE_ORDER_MARK)) { - text = text.substring(1); - } - return text; - } - - public static ParsedRXingResult parseRXingResult(RXingResult theRXingResult) { - for (RXingResultParser parser : PARSERS) { - ParsedRXingResult result = parser.parse(theRXingResult); - if (result != null) { - return result; - } - } - return new TextParsedRXingResult(theRXingResult.getText(), null); - } - - protected static void maybeAppend(String value, StringBuilder result) { - if (value != null) { - result.append('\n'); - result.append(value); - } - } - - protected static void maybeAppend(String[] value, StringBuilder result) { - if (value != null) { - for (String s : value) { - result.append('\n'); - result.append(s); - } - } - } - - protected static String[] maybeWrap(String value) { - return value == null ? null : new String[] { value }; - } - - protected static String unescapeBackslash(String escaped) { - int backslash = escaped.indexOf('\\'); - if (backslash < 0) { - return escaped; - } - int max = escaped.length(); - StringBuilder unescaped = new StringBuilder(max - 1); - unescaped.append(escaped.toCharArray(), 0, backslash); - boolean nextIsEscaped = false; - for (int i = backslash; i < max; i++) { - char c = escaped.charAt(i); - if (nextIsEscaped || c != '\\') { - unescaped.append(c); - nextIsEscaped = false; - } else { - nextIsEscaped = true; - } - } - return unescaped.toString(); - } - - protected static int parseHexDigit(char c) { - if (c >= '0' && c <= '9') { - return c - '0'; - } - if (c >= 'a' && c <= 'f') { - return 10 + (c - 'a'); - } - if (c >= 'A' && c <= 'F') { - return 10 + (c - 'A'); - } - return -1; - } - - protected static boolean isStringOfDigits(CharSequence value, int length) { - return value != null && length > 0 && length == value.length() && DIGITS.matcher(value).matches(); - } - - protected static boolean isSubstringOfDigits(CharSequence value, int offset, int length) { - if (value == null || length <= 0) { - return false; - } - int max = offset + length; - return value.length() >= max && DIGITS.matcher(value.subSequence(offset, max)).matches(); - } - - static MapAbstract class representing the result of decoding a barcode, as more than + * a String -- as some type of structured data. This might be a subclass which represents + * a URL, or an e-mail address. {@link #parseRXingResult(RXingResult)} will turn a raw + * decoded string into the most appropriate type of structured representation.
+ * + *Thanks to Jeff Griffin for proposing rewrite of these classes that relies less + * on exception-based mechanisms during parsing.
+ * + * @author Sean Owen + */ +pub trait RXingResultParser { + // private static final RXingResultParser[] PARSERS = { + // new BookmarkDoCoMoRXingResultParser(), + // new AddressBookDoCoMoRXingResultParser(), + // new EmailDoCoMoRXingResultParser(), + // new AddressBookAURXingResultParser(), + // new VCardRXingResultParser(), + // new BizcardRXingResultParser(), + // new VEventRXingResultParser(), + // new EmailAddressRXingResultParser(), + // new SMTPRXingResultParser(), + // new TelRXingResultParser(), + // new SMSMMSRXingResultParser(), + // new SMSTOMMSTORXingResultParser(), + // new GeoRXingResultParser(), + // new WifiRXingResultParser(), + // new URLTORXingResultParser(), + // new URIRXingResultParser(), + // new ISBNRXingResultParser(), + // new ProductRXingResultParser(), + // new ExpandedProductRXingResultParser(), + // new VINRXingResultParser(), + // }; + + const DIGITS: &'static str = "\\d+"; //= Pattern.compile("\\d+"); + const AMPERSAND: &'static str = "&"; // private static final Pattern AMPERSAND = Pattern.compile("&"); + const EQUALS: &'static str = "="; //private static final Pattern EQUALS = Pattern.compile("="); + const BYTE_ORDER_MARK: &'static str = "\u{feff}"; //private static final String BYTE_ORDER_MARK = "\ufeff"; + + const EMPTY_STR_ARRAY: &'static str = ""; + + const PARSERS: [&'static str; 20] = [ + "BookmarkDoCoMoRXingResultParser", + "AddressBookDoCoMoRXingResultParser", + "EmailDoCoMoRXingResultParser", + "AddressBookAURXingResultParser", + "VCardRXingResultParser", + "BizcardRXingResultParser", + "VEventRXingResultParser", + "EmailAddressRXingResultParser", + "SMTPRXingResultParser", + "TelRXingResultParser", + "SMSMMSRXingResultParser", + "SMSTOMMSTORXingResultParser", + "GeoRXingResultParser", + "WifiRXingResultParser", + "URLTORXingResultParser", + "URIRXingResultParser", + "ISBNRXingResultParser", + "ProductRXingResultParser", + "ExpandedProductRXingResultParser", + "VINRXingResultParser", + ]; + + /** + * Attempts to parse the raw {@link RXingResult}'s contents as a particular type + * of information (email, URL, etc.) and return a {@link ParsedRXingResult} encapsulating + * the result of parsing. + * + * @param theRXingResult the raw {@link RXingResult} to parse + * @return {@link ParsedRXingResult} encapsulating the parsing result + */ + fn parse(&self, theRXingResult: &RXingResult) -> Self; + + fn getMassagedText(result: &RXingResult) -> String { + let mut text = result.getText().clone(); + if text.starts_with(Self::BYTE_ORDER_MARK) { + text = text[1..].to_owned(); + } + return text; + } + + fn parseRXingResult(theRXingResult: &RXingResult) -> Box