rxing/oned/rss/expanded/decoders/
ai_013x0x_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, AI01weightDecoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
30
31/**
32 * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
33 */
34pub struct AI013x0xDecoder<'a> {
35    information: &'a BitArray,
36    decoder: GeneralAppIdDecoder<'a>,
37    addWeightCodeFunction: fn(&mut String, u32),
38    checkWeightFunction: fn(u32) -> u32,
39}
40impl AI01weightDecoder for AI013x0xDecoder<'_> {
41    fn addWeightCode(&self, buf: &mut String, weight: u32) {
42        //unimplemented!("no java implementation exists")
43        (self.addWeightCodeFunction)(buf, weight)
44    }
45
46    fn checkWeight(&self, weight: u32) -> u32 {
47        (self.checkWeightFunction)(weight)
48        // unimplemented!("no java implementation exists")
49    }
50}
51impl AbstractExpandedDecoder for AI013x0xDecoder<'_> {
52    fn parseInformation(&mut self) -> Result<String> {
53        if self.information.get_size()
54            != Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE
55        {
56            return Err(crate::Exceptions::NOT_FOUND);
57        }
58
59        let mut buf = String::new();
60
61        self.encodeCompressedGtin(&mut buf, Self::HEADER_SIZE);
62        self.encodeCompressedWeight(
63            &mut buf,
64            Self::HEADER_SIZE + Self::GTIN_SIZE as usize,
65            Self::WEIGHT_SIZE as u32,
66        );
67
68        Ok(buf)
69    }
70
71    fn getGeneralDecoder(&'_ self) -> &'_ GeneralAppIdDecoder<'_> {
72        &self.decoder
73    }
74}
75impl AI01decoder for AI013x0xDecoder<'_> {}
76
77impl<'a> AI013x0xDecoder<'_> {
78    const HEADER_SIZE: usize = 4 + 1;
79    const WEIGHT_SIZE: usize = 15;
80
81    pub fn new(
82        information: &'a BitArray,
83        addWeightCodeFunction: fn(&mut String, u32),
84        checkWeightFunction: fn(u32) -> u32,
85    ) -> AI013x0xDecoder<'a> {
86        AI013x0xDecoder {
87            information,
88            decoder: GeneralAppIdDecoder::new(information),
89            addWeightCodeFunction,
90            checkWeightFunction,
91        }
92    }
93}