1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Encoder for 2-of-5 barcodes.
//!
//! 2-of-5 barcodes are often used by Airlines and in some industrial settings.
//!
//! They also make an appearance in retail where they are sometimes used for the outer cartons on
//! groups of products (cartons of Cola, etc).
//!
//! Most of the time you will want to use the interleaved barcode over the standard option.

use crate::error::Result;
use crate::sym::helpers;
use crate::sym::Parse;
use core::char;
use core::ops::Range;
use helpers::{vec, Vec};

#[rustfmt::skip]
const WIDTHS: [&str; 10] = [
    "NNWWN", "WNNNW", "NWNNW",
    "WWNNN", "NNWNW", "WNWNN",
    "NWWNN", "NNNWW", "WNNWN",
    "NWNWN",
];

const ITF_START: [u8; 4] = [1, 0, 1, 0];
const ITF_STOP: [u8; 4] = [1, 1, 0, 1];
const STF_START: [u8; 8] = [1, 1, 0, 1, 1, 0, 1, 0];
const STF_STOP: [u8; 8] = [1, 1, 0, 1, 0, 1, 1, 0];

/// The 2-of-5 barcode type.
#[derive(Debug)]
pub enum TF {
    /// The standard 2-of-5 barcode type.
    Standard(Vec<u8>),
    /// The interleaved 2-of-5 barcode type.
    Interleaved(Vec<u8>),
}

impl TF {
    /// Creates a new ITF barcode.
    /// If the length of the given data is odd, a checksum value will be computed and appended to
    /// the data for encoding.
    ///
    /// Returns Result<TF::Interleaved, Error> indicating parse success.
    pub fn interleaved<T: AsRef<str>>(data: T) -> Result<TF> {
        TF::parse(data.as_ref()).map(|d| {
            let mut digits: Vec<u8> = d
                .chars()
                .map(|c| c.to_digit(10).expect("Unknown character") as u8)
                .collect();
            let checksum_required = digits.len() % 2 == 1;

            if checksum_required {
                let check_digit = helpers::modulo_10_checksum(&digits[..], false);
                digits.push(check_digit);
            }

            TF::Interleaved(digits)
        })
    }

    /// Creates a new STF barcode.
    ///
    /// Returns Result<TF::Standard, Error> indicating parse success.
    pub fn standard<T: AsRef<str>>(data: T) -> Result<TF> {
        TF::parse(data.as_ref()).map(|d| {
            let digits: Vec<u8> = d
                .chars()
                .map(|c| c.to_digit(10).expect("Unknown character") as u8)
                .collect();
            TF::Standard(digits)
        })
    }

    fn raw_data(&self) -> &[u8] {
        match *self {
            TF::Standard(ref d) | TF::Interleaved(ref d) => &d[..],
        }
    }

    fn interleave(&self, bars: u8, spaces: u8) -> Vec<u8> {
        let bwidths = WIDTHS[bars as usize].chars();
        let swidths = WIDTHS[spaces as usize].chars();
        let mut encoding: Vec<u8> = vec![];

        for (b, s) in bwidths.zip(swidths) {
            for &(c, i) in &[(b, 1), (s, 0)] {
                match c {
                    'W' => encoding.extend([i; 3].iter().cloned()),
                    _ => encoding.push(i),
                }
            }
        }

        encoding
    }

    fn char_encoding(&self, d: u8) -> Vec<u8> {
        let bars: Vec<Vec<u8>> = self
            .char_widths(d)
            .chars()
            .map(|c| match c {
                'W' => vec![1, 1, 1, 0],
                _ => vec![1, 0],
            })
            .collect();

        helpers::join_iters(bars.iter())
    }

    fn char_widths(&self, d: u8) -> &'static str {
        WIDTHS[d as usize]
    }

    fn stf_payload(&self) -> Vec<u8> {
        let mut encodings = vec![];

        for d in self.raw_data() {
            encodings.extend(self.char_encoding(*d).iter().cloned());
        }

        encodings
    }

    fn itf_payload(&self) -> Vec<u8> {
        let weaves: Vec<Vec<u8>> = self
            .raw_data()
            .chunks(2)
            .map(|c| self.interleave(c[0], c[1]))
            .collect();

        helpers::join_iters(weaves.iter())
    }

    /// Encodes the barcode.
    /// Returns a Vec<u8> of binary digits.
    pub fn encode(&self) -> Vec<u8> {
        match *self {
            TF::Standard(_) => {
                helpers::join_slices(&[&STF_START[..], &self.stf_payload()[..], &STF_STOP[..]][..])
            }
            TF::Interleaved(_) => {
                helpers::join_slices(&[&ITF_START[..], &self.itf_payload()[..], &ITF_STOP[..]][..])
            }
        }
    }
}

impl Parse for TF {
    /// Returns the valid length of data acceptable in this type of barcode.
    /// 2-of-5 barcodes are variable-length.
    fn valid_len() -> Range<u32> {
        1..256
    }

    /// Returns the set of valid characters allowed in this type of barcode.
    fn valid_chars() -> Vec<char> {
        (0..10).map(|i| char::from_digit(i, 10).unwrap()).collect()
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Error;
    use crate::sym::tf::*;
    #[cfg(not(feature = "std"))]
    pub(crate) use alloc::string::{String, ToString};
    use core::char;

    fn collapse_vec(v: Vec<u8>) -> String {
        let chars = v.iter().map(|d| char::from_digit(*d as u32, 10).unwrap());
        chars.collect()
    }

    #[test]
    fn new_itf() {
        let itf = TF::interleaved("12345679".to_string());

        assert!(itf.is_ok());
    }

    #[test]
    fn new_stf() {
        let stf = TF::standard("12345".to_string());

        assert!(stf.is_ok());
    }

    #[test]
    fn invalid_data_itf() {
        let itf = TF::interleaved("1234er123412".to_string());

        assert_eq!(itf.err().unwrap(), Error::Character);
    }

    #[test]
    fn invalid_data_stf() {
        let stf = TF::standard("WORDUP".to_string());

        assert_eq!(stf.err().unwrap(), Error::Character);
    }

    #[test]
    fn itf_raw_data() {
        let itf = TF::interleaved("12345679".to_string()).unwrap();

        assert_eq!(itf.raw_data(), &[1, 2, 3, 4, 5, 6, 7, 9]);
    }

    #[test]
    fn itf_encode() {
        let itf = TF::interleaved("1234567".to_string()).unwrap(); // Check digit: 0

        assert_eq!(
            collapse_vec(itf.encode()),
            "10101110100010101110001110111010001010001110100011100010101010100011100011101101"
                .to_string()
        );
    }

    #[test]
    fn stf_encode() {
        let stf = TF::standard("1234567".to_string()).unwrap();

        assert_eq!(collapse_vec(stf.encode()), "110110101110101010111010111010101110111011101010101010111010111011101011101010101110111010101010101110111011010110".to_string());
    }
}