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
use std::collections::HashMap;
use crate::{BarcodeFormat, Exceptions, Writer};
use super::EAN13Writer;
#[derive(Default)]
pub struct UPCAWriter(EAN13Writer);
impl Writer for UPCAWriter {
fn encode(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
self.encode_with_hints(contents, format, width, height, &HashMap::new())
}
fn encode_with_hints(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
hints: &crate::EncodingHintDictionary,
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
if format != &BarcodeFormat::UPC_A {
return Err(Exceptions::IllegalArgumentException(Some(format!(
"Can only encode UPC-A, but got {:?}",
format
))));
}
self.0.encode_with_hints(
&format!("0{}", contents),
&BarcodeFormat::EAN_13,
width,
height,
hints,
)
}
}
#[cfg(test)]
mod UPCAWriterTestCase {
use crate::{common::bit_matrix_test_case, BarcodeFormat, Writer};
use super::UPCAWriter;
#[test]
fn testEncode() {
let testStr =
"00001010100011011011101100010001011010111101111010101011100101110100100111011001101101100101110010100000";
let result = UPCAWriter::default()
.encode(
"485963095124",
&BarcodeFormat::UPC_A,
testStr.chars().count() as i32,
0,
)
.expect("ok");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
fn testAddChecksumAndEncode() {
let testStr =
"00001010011001001001101111010100011011000101011110101010001001001000111010011100101100110110110010100000";
let result = UPCAWriter::default()
.encode(
"12345678901",
&BarcodeFormat::UPC_A,
testStr.chars().count() as i32,
0,
)
.expect("ok");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
}