use rxing_one_d_proc_derive::OneDWriter;
use crate::common::Result;
use crate::BarcodeFormat;
use super::OneDimensionalCodeWriter;
#[derive(OneDWriter, Default)]
pub struct ITFWriter;
impl OneDimensionalCodeWriter for ITFWriter {
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
let length = contents.chars().count();
if length % 2 != 0 {
return Err(Exceptions::illegal_argument_with(
"The length of the input should be even",
));
}
if length > 80 {
return Err(Exceptions::illegal_argument_with(format!(
"Requested contents should be less than 80 digits long, but got {length}"
)));
}
Self::checkNumeric(contents)?;
let mut result = vec![false; 9 + 9 * length];
let mut pos = Self::appendPattern(&mut result, 0, &START_PATTERN, true) as usize;
let mut i = 0;
while i < length {
let one = contents
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_digit(10)
.ok_or(Exceptions::PARSE)? as usize;
let two = contents
.chars()
.nth(i + 1)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_digit(10)
.ok_or(Exceptions::PARSE)? as usize;
let mut encoding = [0; 10];
for j in 0..5 {
encoding[2 * j] = PATTERNS[one][j];
encoding[2 * j + 1] = PATTERNS[two][j];
}
pos += Self::appendPattern(&mut result, pos, &encoding, true) as usize;
i += 2;
}
Self::appendPattern(&mut result, pos, &END_PATTERN, true);
Ok(result)
}
fn getSupportedWriteFormats(&self) -> Option<Vec<crate::BarcodeFormat>> {
Some(vec![BarcodeFormat::ITF])
}
}
const START_PATTERN: [usize; 4] = [1, 1, 1, 1];
const END_PATTERN: [usize; 3] = [3, 1, 1];
const W: usize = 3; const N: usize = 1;
const PATTERNS: [[usize; 5]; 10] = [
[N, N, W, W, N], [W, N, N, N, W], [N, W, N, N, W], [W, W, N, N, N], [N, N, W, N, W], [W, N, W, N, N], [N, W, W, N, N], [N, N, N, W, W], [W, N, N, W, N], [N, W, N, W, N], ];
#[cfg(test)]
mod ITFWriterTestCase {
use crate::{common::bit_matrix_test_case, BarcodeFormat, Writer};
use super::ITFWriter;
#[test]
fn testEncode() {
doTest(
"00123456789012",
"0000010101010111000111000101110100010101110001110111010001010001110100011\
100010101000101011100011101011101000111000101110100010101110001110100000",
);
}
fn doTest(input: &str, expected: &str) {
let result = ITFWriter
.encode(input, &BarcodeFormat::ITF, 0, 0)
.expect("encode");
assert_eq!(expected, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
#[should_panic]
fn testEncodeIllegalCharacters() {
ITFWriter
.encode("00123456789abc", &BarcodeFormat::ITF, 0, 0)
.expect("should fail");
}
}