diff --git a/src/client/result/ProductParsedResult.java b/src/client/result/ProductParsedResult.java deleted file mode 100644 index cf6293e..0000000 --- a/src/client/result/ProductParsedResult.java +++ /dev/null @@ -1,52 +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; - -/** - * Represents a parsed result that encodes a product by an identifier of some kind. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public final class ProductParsedRXingResult extends ParsedRXingResult { - - private final String productID; - private final String normalizedProductID; - - ProductParsedRXingResult(String productID) { - this(productID, productID); - } - - ProductParsedRXingResult(String productID, String normalizedProductID) { - super(ParsedRXingResultType.PRODUCT); - this.productID = productID; - this.normalizedProductID = normalizedProductID; - } - - public String getProductID() { - return productID; - } - - public String getNormalizedProductID() { - return normalizedProductID; - } - - @Override - public String getDisplayRXingResult() { - return productID; - } - -} diff --git a/src/client/result/ProductParsedResult.rs b/src/client/result/ProductParsedResult.rs new file mode 100644 index 0000000..2a661cf --- /dev/null +++ b/src/client/result/ProductParsedResult.rs @@ -0,0 +1,61 @@ +/* + * 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; + +use super::{ParsedRXingResult, ParsedRXingResultType}; + +/** + * Represents a parsed result that encodes a product by an identifier of some kind. + * + * @author dswitkin@google.com (Daniel Switkin) + */ +pub struct ProductParsedRXingResult { + + product_id:String, + normalized_product_id:String, + +} +impl ParsedRXingResult for ProductParsedRXingResult { + fn getType(&self) -> super::ParsedRXingResultType { + ParsedRXingResultType::PRODUCT + } + + fn getDisplayRXingResult(&self) -> String { + self.product_id.clone() + } +} +impl ProductParsedRXingResult { + pub fn new(product_id:String) -> Self { + Self::with_normalized_id(product_id.clone(), product_id) + } + + pub fn with_normalized_id( product_id: String, normalized_product_id:String) -> Self { + Self{ + product_id, + normalized_product_id, + } + } + + pub fn getProductID(&self) -> &str{ + &self.product_id + } + + pub fn getNormalizedProductID(&self) -> &str { + &self.normalized_product_id + } + +} diff --git a/src/client/result/ProductParsedResultTestCase.java b/src/client/result/ProductParsedResultTestCase.java deleted file mode 100644 index a1928b1..0000000 --- a/src/client/result/ProductParsedResultTestCase.java +++ /dev/null @@ -1,48 +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 ProductParsedRXingResult}. - * - * @author Sean Owen - */ -public final class ProductParsedRXingResultTestCase extends Assert { - - @Test - public void testProduct() { - doTest("123456789012", "123456789012", BarcodeFormat.UPC_A); - doTest("00393157", "00393157", BarcodeFormat.EAN_8); - doTest("5051140178499", "5051140178499", BarcodeFormat.EAN_13); - doTest("01234565", "012345000065", BarcodeFormat.UPC_E); - } - - private static void doTest(String contents, String normalized, BarcodeFormat format) { - RXingResult fakeRXingResult = new RXingResult(contents, null, null, format); - ParsedRXingResult result = RXingResultParser.parseRXingResult(fakeRXingResult); - assertSame(ParsedRXingResultType.PRODUCT, result.getType()); - ProductParsedRXingResult productRXingResult = (ProductParsedRXingResult) result; - assertEquals(contents, productRXingResult.getProductID()); - assertEquals(normalized, productRXingResult.getNormalizedProductID()); - } - -} \ No newline at end of file diff --git a/src/client/result/ProductParsedResultTestCase.rs b/src/client/result/ProductParsedResultTestCase.rs new file mode 100644 index 0000000..b1b4919 --- /dev/null +++ b/src/client/result/ProductParsedResultTestCase.rs @@ -0,0 +1,57 @@ +/* + * 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 ProductParsedRXingResult}. + * + * @author Sean Owen + */ +// public final class ProductParsedRXingResultTestCase extends Assert { + +use crate::{BarcodeFormat, RXingResult, client::result::{ParsedRXingResult, ParsedRXingResultType, ProductParsedResult, ParsedClientResult}}; + +use super::ResultParser; + + #[test] + fn testProduct() { + doTest("123456789012", "123456789012", BarcodeFormat::UPC_A); + doTest("00393157", "00393157", BarcodeFormat::EAN_8); + doTest("5051140178499", "5051140178499", BarcodeFormat::EAN_13); + doTest("01234565", "012345000065", BarcodeFormat::UPC_E); + } + + fn doTest( contents:&str, normalized:&str, format:BarcodeFormat) { + let fakeRXingResult = RXingResult::new(contents, Vec::new(), Vec::new(), format); + let result = ResultParser::parseRXingResult(&fakeRXingResult); + assert_eq!(ParsedRXingResultType::PRODUCT, result.getType()); + + if let ParsedClientResult::ProductResult(productRXingResult) = result { + assert_eq!(contents, productRXingResult.getProductID()); + assert_eq!(normalized, productRXingResult.getNormalizedProductID()); + }else{ + panic!("Expected ParsedClientResult::ProductResult") + } + + } + +// } \ No newline at end of file diff --git a/src/client/result/ProductResultParser.java b/src/client/result/ProductResultParser.java deleted file mode 100644 index 947079b..0000000 --- a/src/client/result/ProductResultParser.java +++ /dev/null @@ -1,55 +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 com.google.zxing.oned.UPCEReader; - -/** - * Parses strings of digits that represent a UPC code. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public final class ProductRXingResultParser extends RXingResultParser { - - // Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes. - @Override - public ProductParsedRXingResult parse(RXingResult result) { - BarcodeFormat format = result.getBarcodeFormat(); - if (!(format == BarcodeFormat.UPC_A || format == BarcodeFormat.UPC_E || - format == BarcodeFormat.EAN_8 || format == BarcodeFormat.EAN_13)) { - return null; - } - String rawText = getMassagedText(result); - if (!isStringOfDigits(rawText, rawText.length())) { - return null; - } - // Not actually checking the checksum again here - - String normalizedProductID; - // Expand UPC-E for purposes of searching - if (format == BarcodeFormat.UPC_E && rawText.length() == 8) { - normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText); - } else { - normalizedProductID = rawText; - } - - return new ProductParsedRXingResult(rawText, normalizedProductID); - } - -} \ No newline at end of file diff --git a/src/client/result/ProductResultParser.rs b/src/client/result/ProductResultParser.rs new file mode 100644 index 0000000..1159f12 --- /dev/null +++ b/src/client/result/ProductResultParser.rs @@ -0,0 +1,57 @@ +/* + * 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 com.google.zxing.oned.UPCEReader; + +use crate::{RXingResult, BarcodeFormat}; + +use super::{ParsedClientResult, ProductParsedRXingResult, ResultParser}; + +/** + * Parses strings of digits that represent a UPC code. + * + * @author dswitkin@google.com (Daniel Switkin) + */ +pub fn parse(result: &RXingResult) -> Option { +// Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes. + + let format = result.getBarcodeFormat(); + if !(format == &BarcodeFormat::UPC_A || format == &BarcodeFormat::UPC_E || + format == &BarcodeFormat::EAN_8 || format == &BarcodeFormat::EAN_13) { + return None; + } + let rawText = ResultParser::getMassagedText(result); + if !ResultParser::isStringOfDigits(&rawText, rawText.len()) { + return None; + } + // Not actually checking the checksum again here + + let normalizedProductID; + // Expand UPC-E for purposes of searching + if format == &BarcodeFormat::UPC_E && rawText.len() == 8 { + unimplemented!("UPCEReader is required to parse this"); + //normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText); + } else { + normalizedProductID = rawText.clone(); + } + + Some(ParsedClientResult::ProductResult(ProductParsedRXingResult::with_normalized_id(rawText, normalizedProductID))) + +} \ No newline at end of file diff --git a/src/client/result/ResultParser.rs b/src/client/result/ResultParser.rs index d295f7d..829913e 100644 --- a/src/client/result/ResultParser.rs +++ b/src/client/result/ResultParser.rs @@ -35,7 +35,7 @@ use crate::{exceptions::Exceptions, RXingResult}; use super::{ GeoResultParser, ISBNResultParser, ParsedClientResult, ParsedRXingResult, SMSMMSResultParser, - TelResultParser, TextParsedRXingResult, WifiResultParser, + TelResultParser, TextParsedRXingResult, WifiResultParser, ProductResultParser, }; /** @@ -102,7 +102,7 @@ pub fn getMassagedText(result: &RXingResult) -> String { } pub fn parseRXingResult(theRXingResult: &RXingResult) -> ParsedClientResult { - let PARSERS: [&ParserFunction; 5] = [ + let PARSERS: [&ParserFunction; 6] = [ // new BookmarkDoCoMoRXingResultParser(), // new AddressBookDoCoMoRXingResultParser(), // new EmailDoCoMoRXingResultParser(), @@ -120,7 +120,7 @@ pub fn parseRXingResult(theRXingResult: &RXingResult) -> ParsedClientResult { // new URLTORXingResultParser(), // new URIRXingResultParser(), &ISBNResultParser::parse, - // new ProductRXingResultParser(), + &ProductResultParser::parse, // new ExpandedProductRXingResultParser(), // new VINRXingResultParser(), ]; diff --git a/src/client/result/mod.rs b/src/client/result/mod.rs index 2b4570e..53b01b3 100644 --- a/src/client/result/mod.rs +++ b/src/client/result/mod.rs @@ -12,6 +12,8 @@ mod GeoResultParser; mod GeoParsedResult; mod SMSParsedResult; mod SMSMMSResultParser; +mod ProductParsedResult; +mod ProductResultParser; use std::fmt; @@ -28,6 +30,7 @@ pub use WifiParsedResult::*; pub use GeoParsedResult::*; // pub use GeoResultParser::*; pub use SMSParsedResult::*; +pub use ProductParsedResult::*; #[cfg(test)] @@ -40,6 +43,8 @@ mod WifiParsedResultTestCase; mod GeoParsedResultTestCase; #[cfg(test)] mod SMSMMSParsedResultTestCase; +#[cfg(test)] +mod ProductParsedResultTestCase; pub enum ParsedClientResult { TextResult(TextParsedRXingResult), @@ -48,6 +53,7 @@ pub enum ParsedClientResult { WiFiResult(WifiParsedRXingResult), GeoResult(GeoParsedRXingResult), SMSResult(SMSParsedRXingResult), + ProductResult(ProductParsedRXingResult), } impl ParsedRXingResult for ParsedClientResult { @@ -59,6 +65,7 @@ impl ParsedRXingResult for ParsedClientResult { ParsedClientResult::WiFiResult(a) => a.getType(), ParsedClientResult::GeoResult(a) => a.getType(), ParsedClientResult::SMSResult(a) => a.getType(), + ParsedClientResult::ProductResult(a) => a.getType(), } @@ -72,6 +79,7 @@ impl ParsedRXingResult for ParsedClientResult { ParsedClientResult::WiFiResult(a) => a.getDisplayRXingResult(), ParsedClientResult::GeoResult(a) => a.getDisplayRXingResult(), ParsedClientResult::SMSResult(a) => a.getDisplayRXingResult(), + ParsedClientResult::ProductResult(a) => a.getDisplayRXingResult(),