use rxing_one_d_proc_derive::OneDWriter;
use crate::{
common::Result,
oned::{EAN8Reader, UPCEANReader},
BarcodeFormat,
};
use super::{upc_ean_reader, OneDimensionalCodeWriter, UPCEANWriter};
#[derive(OneDWriter, Default)]
pub struct EAN8Writer;
const CODE_WIDTH: usize = 3 + (7 * 4) + 5 + (7 * 4) + 3;
impl UPCEANWriter for EAN8Writer {}
impl OneDimensionalCodeWriter for EAN8Writer {
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
let length = contents.chars().count();
let reader = EAN8Reader;
let mut contents = contents.to_owned();
match length {
7 => {
let check =
reader.getStandardUPCEANChecksum(&contents.chars().collect::<Vec<_>>())?;
contents.push_str(&check.to_string());
}
8 => {
if !EAN8Reader.checkStandardUPCEANChecksum(&contents)? {
return Err(Exceptions::illegal_argument_with(
"Contents do not pass checksum",
));
}
}
_ => {
return Err(Exceptions::illegal_argument_with(format!(
"Requested contents should be 7 or 8 digits long, but got {length}"
)))
}
}
Self::checkNumeric(&contents)?;
let mut result = [false; CODE_WIDTH];
let mut pos = 0;
pos += Self::appendPattern(&mut result, pos, &upc_ean_reader::START_END_PATTERN, true)
as usize;
for i in 0..=3 {
let digit = contents
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_digit(10)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)? as usize;
pos += Self::appendPattern(&mut result, pos, &upc_ean_reader::L_PATTERNS[digit], false)
as usize;
}
pos +=
Self::appendPattern(&mut result, pos, &upc_ean_reader::MIDDLE_PATTERN, false) as usize;
for i in 4..=7 {
let digit = contents
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_digit(10)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)? as usize;
pos += Self::appendPattern(&mut result, pos, &upc_ean_reader::L_PATTERNS[digit], true)
as usize;
}
Self::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_8])
}
fn getDefaultMargin(&self) -> u32 {
Self::DEFAULT_MARGIN
}
}
#[cfg(test)]
mod EAN8WriterTestCase {
use crate::{common::bit_matrix_test_case, BarcodeFormat, Writer};
use super::EAN8Writer;
#[test]
fn testEncode() {
let testStr =
"0000001010001011010111101111010110111010101001110111001010001001011100101000000";
let result = EAN8Writer
.encode(
"96385074",
&BarcodeFormat::EAN_8,
testStr.chars().count() as i32,
0,
)
.expect("ok");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
fn testAddChecksumAndEncode() {
let testStr =
"0000001010001011010111101111010110111010101001110111001010001001011100101000000";
let result = EAN8Writer
.encode(
"9638507",
&BarcodeFormat::EAN_8,
testStr.chars().count() as i32,
0,
)
.expect("ok");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
#[should_panic]
fn testEncodeIllegalCharacters() {
EAN8Writer
.encode("96385abc", &BarcodeFormat::EAN_8, 0, 0)
.expect("ok");
}
}