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
use sym::Parse;
use sym::helpers;
use error::Result;
use std::ops::Range;
const CODE39_CHARS: [(char, [u8; 12]); 43] = [
('0', [1,0,1,0,0,1,1,0,1,1,0,1]), ('1', [1,1,0,1,0,0,1,0,1,0,1,1]), ('2', [1,0,1,1,0,0,1,0,1,0,1,1]),
('3', [1,1,0,1,1,0,0,1,0,1,0,1]), ('4', [1,0,1,0,0,1,1,0,1,0,1,1]), ('5', [1,1,0,1,0,0,1,1,0,1,0,1]),
('6', [1,0,1,1,0,0,1,1,0,1,0,1]), ('7', [1,0,1,0,0,1,0,1,1,0,1,1]), ('8', [1,1,0,1,0,0,1,0,1,1,0,1]),
('9', [1,0,1,1,0,0,1,0,1,1,0,1]), ('A', [1,1,0,1,0,1,0,0,1,0,1,1]), ('B', [1,0,1,1,0,1,0,0,1,0,1,1]),
('C', [1,1,0,1,1,0,1,0,0,1,0,1]), ('D', [1,0,1,0,1,1,0,0,1,0,1,1]), ('E', [1,1,0,1,0,1,1,0,0,1,0,1]),
('F', [1,0,1,1,0,1,1,0,0,1,0,1]), ('G', [1,0,1,0,1,0,0,1,1,0,1,1]), ('H', [1,1,0,1,0,1,0,0,1,1,0,1]),
('I', [1,0,1,1,0,1,0,0,1,1,0,1]), ('J', [1,0,1,0,1,1,0,0,1,1,0,1]), ('K', [1,1,0,1,0,1,0,1,0,0,1,1]),
('L', [1,0,1,1,0,1,0,1,0,0,1,1]), ('M', [1,1,0,1,1,0,1,0,1,0,0,1]), ('N', [1,0,1,0,1,1,0,1,0,0,1,1]),
('O', [1,1,0,1,0,1,1,0,1,0,0,1]), ('P', [1,0,1,1,0,1,1,0,1,0,0,1]), ('Q', [1,0,1,0,1,0,1,1,0,0,1,1]),
('R', [1,1,0,1,0,1,0,1,1,0,0,1]), ('S', [1,0,1,1,0,1,0,1,1,0,0,1]), ('T', [1,0,1,0,1,1,0,1,1,0,0,1]),
('U', [1,1,0,0,1,0,1,0,1,0,1,1]), ('V', [1,0,0,1,1,0,1,0,1,0,1,1]), ('W', [1,1,0,0,1,1,0,1,0,1,0,1]),
('X', [1,0,0,1,0,1,1,0,1,0,1,1]), ('Y', [1,1,0,0,1,0,1,1,0,1,0,1]), ('Z', [1,0,0,1,1,0,1,1,0,1,0,1]),
('-', [1,0,0,1,0,1,0,1,1,0,1,1]), ('.', [1,1,0,0,1,0,1,0,1,1,0,1]), (' ', [1,0,0,1,1,0,1,0,1,1,0,1]),
('$', [1,0,0,1,0,0,1,0,0,1,0,1]), ('/', [1,0,0,1,0,0,1,0,1,0,0,1]), ('+', [1,0,0,1,0,1,0,0,1,0,0,1]),
('%', [1,0,1,0,0,1,0,0,1,0,0,1]),
];
const CODE39_GUARD: [u8; 12] = [1,0,0,1,0,1,1,0,1,1,0,1];
#[derive(Debug)]
pub struct Code39 {
data: Vec<char>,
pub checksum: bool,
}
impl Code39 {
fn init(data: &str, checksum: bool) -> Result<Code39> {
match Code39::parse(data) {
Ok(d) => {
Ok(Code39 {
data: d.chars().collect(),
checksum: checksum,
})
}
Err(e) => Err(e),
}
}
pub fn new<T: AsRef<str>>(data: T) -> Result<Code39> {
Code39::init(data.as_ref(), false)
}
pub fn with_checksum<T: AsRef<str>>(data: T) -> Result<Code39> {
Code39::init(data.as_ref(), true)
}
pub fn checksum_char(&self) -> Option<char> {
let get_char_pos = |&c| CODE39_CHARS.iter().position(|t| t.0 == c).unwrap();
let indices = self.data.iter().map(&get_char_pos);
let index = indices.fold(0, |acc, i| acc + i) % CODE39_CHARS.len();
match CODE39_CHARS.get(index) {
Some(&(c, _)) => Some(c),
None => None,
}
}
fn checksum_encoding(&self) -> [u8; 12] {
match self.checksum_char() {
Some(c) => self.char_encoding(&c),
None => panic!("Cannot compute checksum"),
}
}
fn char_encoding(&self, c: &char) -> [u8; 12] {
match CODE39_CHARS.iter().find(|&ch| ch.0 == *c) {
Some(&(_, enc)) => enc,
None => panic!(format!("Unknown char: {}", c)),
}
}
fn push_encoding(&self, into: &mut Vec<u8>, from: [u8; 12]) {
into.extend(from.iter().cloned());
into.push(0);
}
fn payload(&self) -> Vec<u8> {
let mut enc = vec![0];
for c in &self.data {
self.push_encoding(&mut enc, self.char_encoding(&c));
}
if self.checksum {
self.push_encoding(&mut enc, self.checksum_encoding());
}
enc
}
pub fn encode(&self) -> Vec<u8> {
let guard = &CODE39_GUARD[..];
helpers::join_slices(&[guard, &self.payload()[..], guard][..])
}
}
impl Parse for Code39 {
fn valid_len() -> Range<u32> {
1..256
}
fn valid_chars() -> Vec<char> {
let (chars, _): (Vec<_>, Vec<_>) = CODE39_CHARS.iter().cloned().unzip();
chars
}
}
#[cfg(test)]
mod tests {
use sym::code39::*;
use error::Error;
use std::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_code39() {
let code39 = Code39::new("12345".to_owned());
assert!(code39.is_ok());
}
#[test]
fn invalid_data_code39() {
let code39 = Code39::new("1212s".to_owned());
assert_eq!(code39.err().unwrap(), Error::Character);
}
#[test]
fn invalid_len_code39() {
let code39 = Code39::new("".to_owned());
assert_eq!(code39.err().unwrap(), Error::Length);
}
#[test]
fn code39_encode() {
let code391 = Code39::new("1234".to_owned()).unwrap();
let code392 = Code39::new("983RD512".to_owned()).unwrap();
let code393 = Code39::new("TEST8052".to_owned()).unwrap();
assert_eq!(collapse_vec(code391.encode()), "10010110110101101001010110101100101011011011001010101010011010110100101101101".to_owned());
assert_eq!(collapse_vec(code392.encode()), "100101101101010110010110101101001011010110110010101011010101100101010110010110110100110101011010010101101011001010110100101101101".to_owned());
assert_eq!(collapse_vec(code393.encode()), "100101101101010101101100101101011001010101101011001010101101100101101001011010101001101101011010011010101011001010110100101101101".to_owned());
}
#[test]
fn code39_encode_with_checksum() {
let code391 = Code39::with_checksum("1234".to_owned()).unwrap();
let code392 = Code39::with_checksum("983RD512".to_owned()).unwrap();
assert_eq!(collapse_vec(code391.encode()), "100101101101011010010101101011001010110110110010101010100110101101101010010110100101101101".to_owned());
assert_eq!(collapse_vec(code392.encode()), "1001011011010101100101101011010010110101101100101010110101011001010101100101101101001101010110100101011010110010101101011011010010100101101101".to_owned());
}
#[test]
fn code39_checksum_calculation() {
let code391 = Code39::new("1234".to_owned()).unwrap();
let code392 = Code39::new("159AZ".to_owned()).unwrap();
assert_eq!(code391.checksum_char().unwrap(), 'A');
assert_eq!(code392.checksum_char().unwrap(), 'H');
}
}