scanning 2.1.0

A barcode-encoding library
Documentation
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Encoder for Code128 barcodes.
//!
//! Code128 is a popular, high-density symbology that allows for the encoding of alphanumeric
//! data along with many special characters by utilising three separate character-sets.
//!
//! Code128 also offers double-density encoding of digits.
//!
//! ## Character sets
//!
//! Barcoders provides special Unicode syntax for specifying the character set(s) which should be
//! used in the barcode:
//!
//! <ul><li>\u{00C0} = Switch to character-set A (À)</li>
//! <li>\u{0181} = Switch to character-set B (Ɓ)</li>
//! <li>\u{0106} = Switch to character-set C (Ć)</li></ul>
//!
//! You must provide both the starting character-set along with any changes during the data. This
//! means all Code128 barcodes must start with either "À", "Ɓ" or "Ć". Simple alphanumeric data
//! can generally use character-set A solely.
//!
//! As an example, this barcode uses character-set B:
//!
//! <ul><li>\u{0181}HE1234A*1</li></ul>
//!
//! Or:
//!
//! <ul><li>ƁHE1234A*1</li></ul>
//!
//! And this one starts at character-set A (the default) and then switches to C to encode the digits more
//! effectively:
//!
//! <ul><li>\u{00C0}HE@$A\u{0106}123456</li></ul>
//!
//! Or:
//!
//! <ul><li>ÀHE@$AĆ123456</li></ul>
//!
//! ## Unicode characters
//!
//! The invisible unicode characters that are available in character set A should be represented as
//! their Unicode sequences. For example, to represent the 'ACK' character:
//!
//! <ul><li>À\u{0006}</li></ul>
//!
//! ## Special-purpose function characters (FNC1 - 4)
//!
//! The function sequences can be represented via the following unicode characters:
//!
//! - FNC1: ```Ź``` (```\u{0179}```)
//! - FNC2: ```ź``` (```\u{017A}```)
//! - FNC3: ```Ż``` (```\u{017B}```)
//! - FNC4: ```ż``` (```\u{017C}```)
//! - SHIFT: ```Ž``` (```\u{017D}```)

use crate::error::{Error, Result};
use crate::sym::helpers;
#[cfg(not(feature = "std"))]
use alloc::{format, string::ToString};
use core::cmp;
use helpers::{vec, Vec};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Unit {
    kind: UnitKind,
    index: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum UnitKind {
    /// Represents character set A in Code128 barcodes.
    A,
    /// Represents character set B in Code128 barcodes.
    B,
    /// Represents character set C in Code128 barcodes.
    C,
}

type Encoding = [u8; 11];

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Represents the character sets available in Code128 barcodes.
/// Using special characters to switch between character sets is still supported.
///
/// - `A`: Character set A, which includes ASCII characters 00 to 95.
/// - `B`: Character set B, which includes ASCII characters 32 to 127.
/// - `C`: Character set C, which encodes pairs of digits (00–99).
/// - `None`: No character set specified.
pub enum CharacterSet {
    /// Character set A
    A,
    /// Character set B
    B,
    /// Character set C
    C,
    /// No character set specified, will error if you don't use a special character to switch.
    None,
}

// Character -> Binary mappings for each of the allowable characters in each character-set.
const CHARS: [([&str; 3], Encoding); 106] = [
    ([" ", " ", "00"], [1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0]),
    (["!", "!", "01"], [1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0]),
    (["\"", "\"", "02"], [1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0]),
    (["#", "#", "03"], [1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0]),
    (["$", "$", "04"], [1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0]),
    (["%", "%", "05"], [1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0]),
    (["&", "&", "06"], [1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0]),
    (["'", "'", "07"], [1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0]),
    (["(", "(", "08"], [1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]),
    ([")", ")", "09"], [1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0]),
    (["*", "*", "10"], [1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0]),
    (["+", "+", "11"], [1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0]),
    ([",", ",", "12"], [1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]),
    (["-", "-", "13"], [1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0]),
    ([".", ".", "14"], [1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0]),
    (["/", "/", "15"], [1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0]),
    (["0", "0", "16"], [1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0]),
    (["1", "1", "17"], [1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0]),
    (["2", "2", "18"], [1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0]),
    (["3", "3", "19"], [1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0]),
    (["4", "4", "20"], [1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0]),
    (["5", "5", "21"], [1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0]),
    (["6", "6", "22"], [1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0]),
    (["7", "7", "23"], [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0]),
    (["8", "8", "24"], [1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0]),
    (["9", "9", "25"], [1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0]),
    ([":", ":", "26"], [1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0]),
    ([";", ";", "27"], [1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0]),
    (["<", "<", "28"], [1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0]),
    (["=", "=", "29"], [1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0]),
    ([">", ">", "30"], [1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0]),
    (["?", "?", "31"], [1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0]),
    (["@", "@", "32"], [1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]),
    (["A", "A", "33"], [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0]),
    (["B", "B", "34"], [1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0]),
    (["C", "C", "35"], [1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0]),
    (["D", "D", "36"], [1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0]),
    (["E", "E", "37"], [1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0]),
    (["F", "F", "38"], [1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0]),
    (["G", "G", "39"], [1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0]),
    (["H", "H", "40"], [1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0]),
    (["I", "I", "41"], [1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0]),
    (["J", "J", "42"], [1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0]),
    (["K", "K", "43"], [1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0]),
    (["L", "L", "44"], [1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0]),
    (["M", "M", "45"], [1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0]),
    (["N", "N", "46"], [1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0]),
    (["O", "O", "47"], [1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0]),
    (["P", "P", "48"], [1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]),
    (["Q", "Q", "49"], [1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0]),
    (["R", "R", "50"], [1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0]),
    (["S", "S", "51"], [1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0]),
    (["T", "T", "52"], [1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0]),
    (["U", "U", "53"], [1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0]),
    (["V", "V", "54"], [1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0]),
    (["W", "W", "55"], [1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0]),
    (["X", "X", "56"], [1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0]),
    (["Y", "Y", "57"], [1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0]),
    (["Z", "Z", "58"], [1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0]),
    (["[", "[", "59"], [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0]),
    (["\\", "\\", "60"], [1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0]),
    (["]", "]", "61"], [1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0]),
    (["^", "^", "62"], [1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0]),
    (["_", "_", "63"], [1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]),
    (["\u{0000}", "`", "64"], [1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0]),
    (["\u{0001}", "a", "65"], [1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0]),
    (["\u{0002}", "b", "66"], [1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0]),
    (["\u{0003}", "c", "67"], [1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0]),
    (["\u{0004}", "d", "68"], [1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0]),
    (["\u{0005}", "e", "69"], [1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0]),
    (["\u{0006}", "f", "70"], [1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0]),
    (["\u{0007}", "g", "71"], [1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0]),
    (["\u{0008}", "h", "72"], [1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0]),
    (["\u{0009}", "i", "73"], [1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0]),
    (["\u{000A}", "j", "74"], [1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0]),
    (["\u{000B}", "k", "75"], [1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0]),
    (["\u{000C}", "l", "76"], [1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0]),
    (["\u{000D}", "m", "77"], [1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0]),
    (["\u{000E}", "n", "78"], [1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0]),
    (["\u{000F}", "o", "79"], [1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]),
    (["\u{0010}", "p", "80"], [1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0]),
    (["\u{0011}", "q", "81"], [1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0]),
    (["\u{0012}", "r", "82"], [1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0]),
    (["\u{0013}", "s", "83"], [1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0]),
    (["\u{0014}", "t", "84"], [1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0]),
    (["\u{0015}", "u", "85"], [1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0]),
    (["\u{0016}", "v", "86"], [1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0]),
    (["\u{0017}", "w", "87"], [1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0]),
    (["\u{0018}", "x", "88"], [1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0]),
    (["\u{0019}", "y", "89"], [1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0]),
    (["\u{001A}", "z", "90"], [1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0]),
    (["\u{001B}", "{", "91"], [1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0]),
    (["\u{001C}", "|", "92"], [1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0]),
    (["\u{001D}", "}", "93"], [1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0]),
    (["\u{001E}", "~", "94"], [1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0]),
    (
        ["\u{001F}", "\u{00F7}", "95"],
        [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0],
    ),
    (
        ["\u{017B}", "\u{017B}", "96"],
        [1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0],
    ),
    (
        ["\u{017A}", "\u{017A}", "97"],
        [1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0],
    ),
    (
        ["\u{017D}", "\u{017D}", "98"],
        [1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0],
    ),
    (["Ć", "Ć", "99"], [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0]),
    (["Ɓ", "\u{017C}", "Ɓ"], [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0]),
    (["\u{017C}", "À", "À"], [1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0]),
    (
        ["\u{0179}", "\u{0179}", "\u{0179}"],
        [1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0],
    ),
    (
        ["START-À", "START-À", "START-À"],
        [1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0],
    ),
    (
        ["START-Ɓ", "START-Ɓ", "START-Ɓ"],
        [1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0],
    ),
    (
        ["START-Ć", "START-Ć", "START-Ć"],
        [1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0],
    ),
];

// Stop sequence.
const STOP: Encoding = [1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0];

// Termination sequence.
const TERM: [u8; 2] = [1, 1];

/// The Code128 barcode type.
///
/// # Character sets
///
/// * 128A (Code Set A) – ASCII characters 00 to 95 (0–9, A–Z and control
///   codes), special characters, and FNC 1–4
/// * 128B (Code Set B) – ASCII characters 32 to 127 (0–9, A–Z, a–z), special
///   characters, and FNC 1–4
/// * 128C (Code Set C) – 00–99 (encodes two digits with a single code point)
///   and FNC1
///
/// See [module] docs for additional information.
///
/// [module]: crate::sym::code128
#[derive(Debug)]
pub struct Code128(Vec<Unit>);

impl Unit {
    const fn index(&self) -> usize {
        self.index
    }
}

impl CharacterSet {
    const fn from_char(c: char) -> Result<Self> {
        match c {
            'À' => Ok(Self::A),
            'Ɓ' => Ok(Self::B),
            'Ć' => Ok(Self::C),
            _ => Err(Error::Character),
        }
    }

    const fn unit(self, n: usize) -> Result<Unit> {
        let kind = match self {
            Self::A => UnitKind::A,
            Self::B => UnitKind::B,
            Self::C => UnitKind::C,
            Self::None => return Err(Error::Character),
        };
        Ok(Unit { kind, index: n })
    }

    const fn index(self) -> Result<usize> {
        match self {
            Self::A => Ok(0),
            Self::B => Ok(1),
            Self::C => Ok(2),
            Self::None => Err(Error::Character),
        }
    }

    fn lookup(self, s: &str) -> Result<Unit> {
        let p = self.index()?;

        // Handle FNC characters explicitly - they work in any character set
        if let Some(index) = match s {
            "Ź" => Some(102), // FNC1 - maps to CHARS[102]
            "ź" => Some(97),  // FNC2 - maps to CHARS[97] 
            "Ż" => Some(96),  // FNC3 - maps to CHARS[96]
            "ż" => Some(100), // FNC4 - maps to CHARS[100] (was 101, should be 100)
            _ => None,
        } {
            return Ok(Unit { 
                kind: match self {
                    Self::A => UnitKind::A,
                    Self::B => UnitKind::B, 
                    Self::C => UnitKind::C,
                    Self::None => return Err(Error::Character),
                }, 
                index 
            });
        }

        CHARS
            .iter()
            .position(|&c| c.0[p] == s)
            .map_or(Err(Error::Character), |i| self.unit(i))
    }
}

impl Code128 {
    /// Creates a new barcode.
    ///
    /// # Errors
    ///
    /// Returns an `Error::Length` if the input data is too short.
    /// Returns an `Error::Character` if the input data contains invalid characters or an invalid character set.
    ///
    /// # Returns
    ///
    /// A `Result` containing the `Code128` barcode on success.
    pub fn new<T: AsRef<str>>(data: T, character_set: CharacterSet) -> Result<Self> {
        let data = data.as_ref();

        if data.len() < 2 {
            return Err(Error::Length);
        }

        // Append a letter depending on the character-set, or nothing for CharacterSet::None.
        let data = match character_set {
            CharacterSet::A => format!("À{data}"), // Character set A
            CharacterSet::B => format!("Ɓ{data}"), // Character set B
            CharacterSet::C => format!("Ć{data}"), // Character set C
            CharacterSet::None => data.to_string(), // No character set
        };

        Self::parse(data.chars().collect()).map(Code128)
    }

    // Tokenizes and collects the data into the appropriate character-sets.
    fn parse(chars: Vec<char>) -> Result<Vec<Unit>> {
        let mut units: Vec<Unit> = vec![];
        let mut char_set = CharacterSet::None;
        let mut carry: Option<char> = None;

        for ch in chars {
            match ch {
                // Handle longhand Unicode sequences for character set switches and special FNC characters
                '\u{00C0}' | '\u{0181}' | '\u{0106}' if units.is_empty() => {
                    char_set = CharacterSet::from_char(ch)?;

                    let c = format!("START-{ch}");
                    let u = char_set.lookup(&c)?;
                    units.push(u);
                }
                '\u{00C0}' | '\u{0181}' | '\u{0106}' => {
                    if char_set == CharacterSet::C && carry.is_some() {
                        return Err(Error::Character);
                    }
                    let u = char_set.lookup(&ch.to_string())?;
                    units.push(u);

                    char_set = CharacterSet::from_char(ch)?;
                }
                d if d.is_ascii_digit() && char_set == CharacterSet::C => match carry {
                    None => carry = Some(d),
                    Some(n) => {
                        let num = format!("{n}{d}");
                        let u = char_set.lookup(&num)?;
                        units.push(u);
                        carry = None;
                    }
                },
                // Handle FNC characters explicitly - these can be used in any character set
                'Ź' | 'ź' | 'Ż' | 'ż' => {
                    // If no character set is set yet, we need to reject the input
                    if char_set == CharacterSet::None {
                        return Err(Error::Character);
                    }
                    
                    // FNC characters can be used in any character set
                    let index = match ch {
                        'Ź' => 102, // FNC1
                        'ź' => 97,  // FNC2
                        'Ż' => 96,  // FNC3 
                        'ż' => 100, // FNC4 - corrected from 101 to 100
                        _ => unreachable!(),
                    };
                    
                    let kind = match char_set {
                        CharacterSet::A => UnitKind::A,
                        CharacterSet::B => UnitKind::B,
                        CharacterSet::C => UnitKind::C,
                        CharacterSet::None => return Err(Error::Character),
                    };
                    
                    units.push(Unit { kind, index });
                }
                _ => {
                    if char_set == CharacterSet::None {
                        return Err(Error::Character);
                    }
                    let u = char_set.lookup(&ch.to_string())?;
                    units.push(u);
                }
            }
        }

        match carry {
            Some(_) => Err(Error::Character),
            None => Ok(units),
        }
    }

    /// Calculates the checksum index using a modulo-103 algorithm.
    fn checksum_value(&self) -> u8 {
        let sum: usize = self
            .0
            .iter()
            .zip(0..self.0.len())
            .fold(0, |t, (u, i)| t + (u.index() * cmp::max(1, i)));

        (sum % 103)
            .try_into()
            .expect("Checksum value should always be non-negative and fit in u8")
    }

    fn checksum_encoding(&self) -> Encoding {
        let v = self.checksum_value();
        Self::unit_encoding(&Unit {
            kind: UnitKind::A,
            index: v as usize,
        })
    }

    const fn unit_encoding(c: &Unit) -> Encoding {
        CHARS[c.index()].1
    }

    fn payload(&self) -> Vec<u8> {
        let slices: Vec<Encoding> = self.0.iter().map(Self::unit_encoding).collect();

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

    /// Encodes the barcode.
    /// Returns a Vec<u8> of binary digits.
    #[must_use]
    pub fn encode(&self) -> Vec<u8> {
        helpers::join_slices(
            &[
                &self.payload()[..],
                &self.checksum_encoding()[..],
                &STOP[..],
                &TERM[..],
            ][..],
        )
    }
}

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

    fn collapse_vec(v: &[u8]) -> String {
        let chars = v.iter().map(|d| {
            char::from_digit(u32::from(*d), 10).expect("Failed to convert digit to character")
        });
        chars.collect()
    }

    #[test]
    fn new_code128() {
        let code128_a = Code128::new(" !! Ć0201", CharacterSet::A);
        let code128_b = Code128::new("!!  \" ", CharacterSet::A);

        assert!(code128_a.is_ok());
        assert!(code128_b.is_ok());
    }

    #[test]
    fn invalid_length_code128() {
        let code128_a = Code128::new("", CharacterSet::None);

        assert_eq!(
            code128_a.expect_err("Expected Error::Length but got None"),
            Error::Length
        );
    }

    #[test]
    fn invalid_data_code128() {
        let code128_a = Code128::new("", CharacterSet::A); // Unknown character.
        let code128_b = Code128::new("HELLOĆ12352", CharacterSet::A); // Trailing carry at the end.
        let code128_c = Code128::new("HELLO", CharacterSet::None); // No Character-Set specified.

        assert_eq!(
            code128_a.expect_err("Expected Error::Character but got None"),
            Error::Character
        );
        assert_eq!(
            code128_b.expect_err("Expected Error::Character but got None"),
            Error::Character
        );
        assert_eq!(
            code128_c.expect_err("Expected Error::Character but got None"),
            Error::Character
        );
    }

    #[test]
    fn code128_encode() {
        let code128_a = Code128::new("HELLO", CharacterSet::A)
            .expect("Failed to create Code128 barcode with CharacterSet A");
        let code128_b = Code128::new("XYĆ2199", CharacterSet::A)
            .expect("Failed to create Code128 barcode with CharacterSet A");
        let code128_c = Code128::new("xyZÀ199!*1", CharacterSet::B)
            .expect("Failed to create Code128 barcode with CharacterSet B");

        assert_eq!(collapse_vec(&code128_a.encode()), "110100001001100010100010001101000100011011101000110111010001110110110100010001100011101011");
        assert_eq!(collapse_vec(&code128_b.encode()), "110100001001110001011011101101000101110111101101110010010111011110100111011001100011101011");
        assert_eq!(collapse_vec(&code128_c.encode()), "1101001000011110010010110110111101110110001011101011110100111001101110010110011100101100110011011001100100010010011100110100101111001100011101011");
    }

    #[test]
    fn code128_encode_special_chars() {
        let code128_a = Code128::new("B\u{0006}", CharacterSet::A)
            .expect("Failed to create Code128 barcode with special character");

        assert_eq!(
            collapse_vec(&code128_a.encode()),
            "110100001001000101100010110000100100110100001100011101011"
        );
    }

    #[test]
    fn code128_encode_fnc_chars() {
        let code128_a = Code128::new("Ź4218402050À0", CharacterSet::C)
            .expect("Failed to create Code128 barcode with FNC characters");

        assert_eq!(collapse_vec(&code128_a.encode()), "110100111001111010111010110111000110011100101100010100011001001110110001011101110101111010011101100101011110001100011101011");
    }

    #[test]
    fn code128_encode_longhand() {
        let code128_a = Code128::new("\u{00C0}HELLO", CharacterSet::None)
            .expect("Failed to create Code128 barcode with longhand syntax");
        let code128_b = Code128::new("\u{00C0}XY\u{0106}2199", CharacterSet::None)
            .expect("Failed to create Code128 barcode with longhand syntax");
        let code128_c = Code128::new("\u{0181}xyZ\u{00C0}199!*1", CharacterSet::None)
            .expect("Failed to create Code128 barcode with longhand syntax");

        assert_eq!(collapse_vec(&code128_a.encode()), "110100001001100010100010001101000100011011101000110111010001110110110100010001100011101011");
        assert_eq!(collapse_vec(&code128_b.encode()), "110100001001110001011011101101000101110111101101110010010111011110100111011001100011101011");
        assert_eq!(collapse_vec(&code128_c.encode()), "1101001000011110010010110110111101110110001011101011110100111001101110010110011100101100110011011001100100010010011100110100101111001100011101011");
    }
}