rxing/oned/rss/expanded/decoders/
ai_013x0x_decoder.rs1use crate::common::{BitArray, Result};
28
29use super::{AI01decoder, AI01weightDecoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
30
31pub 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 (self.addWeightCodeFunction)(buf, weight)
44 }
45
46 fn checkWeight(&self, weight: u32) -> u32 {
47 (self.checkWeightFunction)(weight)
48 }
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}