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
use rxing_one_d_proc_derive::OneDWriter;
use crate::{
oned::{upc_ean_reader, EAN13Reader},
BarcodeFormat,
};
use super::{OneDimensionalCodeWriter, UPCEANReader, UPCEANWriter};
#[derive(OneDWriter, Default)]
pub struct EAN13Writer;
impl UPCEANWriter for EAN13Writer {}
impl OneDimensionalCodeWriter for EAN13Writer {
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>, crate::Exceptions> {
let reader: EAN13Reader = EAN13Reader::default();
let mut contents = contents.to_owned();
let length = contents.chars().count();
match length {
12 => {
let check = reader.getStandardUPCEANChecksum(&contents)?;
contents.push_str(&check.to_string());
}
13 => {
if !reader.checkStandardUPCEANChecksum(&contents)? {
return Err(Exceptions::IllegalArgumentException(Some(
"Contents do not pass checksum".to_owned(),
)));
}
}
_ => {
return Err(Exceptions::IllegalArgumentException(Some(format!(
"Requested contents should be 12 or 13 digits long, but got {}",
length
))))
}
}
EAN13Writer::checkNumeric(&contents)?;
let firstDigit = contents.chars().next().unwrap().to_digit(10).unwrap() as usize; let parities = EAN13Reader::FIRST_DIGIT_ENCODINGS[firstDigit];
let mut result = [false; CODE_WIDTH];
let mut pos = 0;
pos +=
EAN13Writer::appendPattern(&mut result, pos, &upc_ean_reader::START_END_PATTERN, true)
as usize;
for i in 1..=6 {
let mut digit = contents.chars().nth(i).unwrap().to_digit(10).unwrap() as usize; if (parities >> (6 - i) & 1) == 1 {
digit += 10;
}
pos += EAN13Writer::appendPattern(
&mut result,
pos,
&upc_ean_reader::L_AND_G_PATTERNS[digit],
false,
) as usize;
}
pos += EAN13Writer::appendPattern(&mut result, pos, &upc_ean_reader::MIDDLE_PATTERN, false)
as usize;
for i in 7..=12 {
let digit = contents.chars().nth(i).unwrap().to_digit(10).unwrap() as usize; pos += EAN13Writer::appendPattern(
&mut result,
pos,
&upc_ean_reader::L_PATTERNS[digit],
true,
) as usize;
}
EAN13Writer::appendPattern(&mut result, pos, &upc_ean_reader::START_END_PATTERN, true);
Ok(result.to_vec())
}
fn getSupportedWriteFormats(&self) -> Option<Vec<crate::BarcodeFormat>> {
Some(vec![BarcodeFormat::EAN_13])
}
fn getDefaultMargin(&self) -> u32 {
Self::DEFAULT_MARGIN
}
}
const CODE_WIDTH: usize = 3 + (7 * 6) + 5 + (7 * 6) + 3; #[cfg(test)]
mod EAN13WriterTestCase {
use crate::{common::bit_matrix_test_case, BarcodeFormat, Writer};
use super::EAN13Writer;
#[test]
fn testEncode() {
let testStr =
"00001010001011010011101100110010011011110100111010101011001101101100100001010111001001110100010010100000";
let result = EAN13Writer::default()
.encode(
"5901234123457",
&BarcodeFormat::EAN_13,
testStr.chars().count() as i32,
0,
)
.expect("exist");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
fn testAddChecksumAndEncode() {
let testStr =
"00001010001011010011101100110010011011110100111010101011001101101100100001010111001001110100010010100000";
let result = EAN13Writer::default()
.encode(
"590123412345",
&BarcodeFormat::EAN_13,
testStr.chars().count() as i32,
0,
)
.expect("exist");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
#[should_panic]
fn testEncodeIllegalCharacters() {
EAN13Writer::default()
.encode("5901234123abc", &BarcodeFormat::EAN_13, 0, 0)
.expect("encode");
}
}