rxing/oned/rss/expanded/decoders/
ai_01392x_decoder.rs

1/*
2 * Copyright (C) 2010 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * These authors would like to acknowledge the Spanish Ministry of Industry,
19 * Tourism and Trade, for the support in the project TSI020301-2008-2
20 * "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
21 * Mobile Dynamic Environments", led by Treelogic
22 * ( http://www.treelogic.com/ ):
23 *
24 *   http://www.piramidepse.com/
25 */
26
27use crate::common::{BitArray, Result};
28
29use super::{AI01decoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
30
31/**
32 * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
33 */
34pub struct AI01392xDecoder<'a> {
35    information: &'a BitArray,
36    general_decoder: GeneralAppIdDecoder<'a>,
37}
38impl AI01decoder for AI01392xDecoder<'_> {}
39impl AbstractExpandedDecoder for AI01392xDecoder<'_> {
40    fn parseInformation(&mut self) -> Result<String> {
41        if self.information.get_size() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
42            return Err(crate::Exceptions::NOT_FOUND);
43        }
44
45        let mut buf = String::new();
46
47        self.encodeCompressedGtin(&mut buf, Self::HEADER_SIZE);
48
49        let lastAIdigit = self.getGeneralDecoder().extractNumericValueFromBitArray(
50            Self::HEADER_SIZE + Self::GTIN_SIZE as usize,
51            Self::LAST_DIGIT_SIZE as u32,
52        );
53        buf.push_str("(392");
54        buf.push_str(&lastAIdigit.to_string());
55        buf.push(')');
56
57        let decodedInformation = self.general_decoder.decodeGeneralPurposeField(
58            Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::LAST_DIGIT_SIZE,
59            "",
60        )?;
61        buf.push_str(decodedInformation.getNewString());
62
63        Ok(buf)
64    }
65
66    fn getGeneralDecoder(&'_ self) -> &'_ super::GeneralAppIdDecoder<'_> {
67        &self.general_decoder
68    }
69}
70impl<'a> AI01392xDecoder<'_> {
71    const HEADER_SIZE: usize = 5 + 1 + 2;
72    const LAST_DIGIT_SIZE: usize = 2;
73
74    pub fn new(information: &'a BitArray) -> AI01392xDecoder<'a> {
75        AI01392xDecoder {
76            information,
77            general_decoder: GeneralAppIdDecoder::new(information),
78        }
79    }
80}