use std::fmt::Write as _;
const MAX_INPUT_BYTES: usize = 7089;
const MAX_BORDER: usize = 256;
const MIN_VERSION: u8 = 1;
const MAX_VERSION: u8 = 40;
const PENALTY_N1: usize = 3;
const PENALTY_N2: usize = 3;
const PENALTY_N3: usize = 40;
const PENALTY_N4: usize = 10;
const SVG_PIXEL_SIZE: usize = 10;
const HEX_DIGITS: &[u8; 16] = b"0123456789ABCDEF";
const ECC_CODEWORDS_PER_BLOCK: [[u8; 41]; 4] = [
[
0, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28,
30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
],
[
0, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
],
[
0, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30,
30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
],
[
0, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
],
];
const NUM_ERROR_CORRECTION_BLOCKS: [[u8; 41]; 4] = [
[
0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13,
14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25,
],
[
0, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21,
23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49,
],
[
0, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29,
34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68,
],
[
0, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32,
35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81,
],
];
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum QrErrorCorrection {
#[default]
Low,
Medium,
Quartile,
High,
}
impl QrErrorCorrection {
const fn ordinal(self) -> usize {
match self {
Self::Low => 0,
Self::Medium => 1,
Self::Quartile => 2,
Self::High => 3,
}
}
const fn format_bits(self) -> u32 {
match self {
Self::Low => 1,
Self::Medium => 0,
Self::Quartile => 3,
Self::High => 2,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum QrEncodeError {
DataTooLong,
BorderTooLarge,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct QrMatrix {
pub(crate) size: usize,
pub(crate) version: u8,
pub(crate) mask: u8,
pub(crate) modules: Vec<Vec<bool>>,
}
pub(crate) fn encode_qr(
value: &str,
ecc: QrErrorCorrection,
border: usize,
) -> Result<QrMatrix, QrEncodeError> {
encode_text(value, ecc, false, border)
}
pub(crate) fn render_qr_svg(value: &str) -> Option<String> {
let matrix = encode_qr(value, QrErrorCorrection::Low, 1).ok()?;
let side = matrix.size * SVG_PIXEL_SIZE;
let mut svg = String::with_capacity(160 + matrix.size * matrix.size * 12);
write!(
svg,
r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {side} {side}"><rect fill="white" width="{side}" height="{side}"/><path fill="black" d=""#
)
.ok()?;
for (row, cells) in matrix.modules.iter().enumerate() {
for (column, &dark) in cells.iter().enumerate() {
if dark {
let x = column * SVG_PIXEL_SIZE;
let y = row * SVG_PIXEL_SIZE;
write!(
svg,
"M{x},{y}h{SVG_PIXEL_SIZE}v{SVG_PIXEL_SIZE}h-{SVG_PIXEL_SIZE}z"
)
.ok()?;
}
}
}
svg.push_str(r#""/></svg>"#);
Some(svg)
}
pub(crate) fn encode_uri_component(value: &str) -> String {
let mut encoded = String::with_capacity(value.len());
for &byte in value.as_bytes() {
if byte.is_ascii_alphanumeric()
|| matches!(
byte,
b'-' | b'_' | b'.' | b'!' | b'~' | b'*' | b'\'' | b'(' | b')'
)
{
encoded.push(char::from(byte));
} else {
encoded.push('%');
encoded.push(char::from(HEX_DIGITS[usize::from(byte >> 4)]));
encoded.push(char::from(HEX_DIGITS[usize::from(byte & 0x0F)]));
}
}
encoded
}
fn encode_text(
value: &str,
ecc: QrErrorCorrection,
boost_ecc: bool,
border: usize,
) -> Result<QrMatrix, QrEncodeError> {
if border > MAX_BORDER {
return Err(QrEncodeError::BorderTooLarge);
}
if value.len() > MAX_INPUT_BYTES {
return Err(QrEncodeError::DataTooLong);
}
let segment = make_segment(value);
let symbol = encode_segment(segment.as_ref(), ecc, boost_ecc)?;
Ok(add_border(&symbol, border))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Mode {
Numeric,
Alphanumeric,
Byte,
}
impl Mode {
const fn indicator(self) -> usize {
match self {
Self::Numeric => 1,
Self::Alphanumeric => 2,
Self::Byte => 4,
}
}
fn char_count_bits(self, version: u8) -> usize {
let widths: [usize; 3] = match self {
Self::Numeric => [10, 12, 14],
Self::Alphanumeric => [9, 11, 13],
Self::Byte => [8, 16, 16],
};
widths[(usize::from(version) + 7) / 17]
}
}
struct Segment {
mode: Mode,
num_chars: usize,
bits: Vec<bool>,
}
fn make_segment(text: &str) -> Option<Segment> {
let bytes = text.as_bytes();
if bytes.is_empty() {
None
} else if bytes.iter().all(u8::is_ascii_digit) {
Some(make_numeric(bytes))
} else if let Some(values) = bytes
.iter()
.map(|&byte| alphanumeric_value(byte))
.collect::<Option<Vec<usize>>>()
{
Some(make_alphanumeric(&values))
} else {
Some(make_bytes(bytes))
}
}
fn make_numeric(digits: &[u8]) -> Segment {
let mut bits = Vec::with_capacity(digits.len() / 3 * 10 + 7);
for group in digits.chunks(3) {
let value = group
.iter()
.fold(0, |total, &digit| total * 10 + usize::from(digit - b'0'));
append_bits(value, group.len() * 3 + 1, &mut bits);
}
Segment {
mode: Mode::Numeric,
num_chars: digits.len(),
bits,
}
}
fn make_alphanumeric(values: &[usize]) -> Segment {
let mut bits = Vec::with_capacity(values.len() / 2 * 11 + 6);
for pair in values.chunks(2) {
let value = pair
.iter()
.fold(0, |total, &character| total * 45 + character);
append_bits(value, pair.len() * 5 + 1, &mut bits);
}
Segment {
mode: Mode::Alphanumeric,
num_chars: values.len(),
bits,
}
}
fn make_bytes(bytes: &[u8]) -> Segment {
let mut bits = Vec::with_capacity(bytes.len() * 8);
for &byte in bytes {
append_bits(usize::from(byte), 8, &mut bits);
}
Segment {
mode: Mode::Byte,
num_chars: bytes.len(),
bits,
}
}
fn alphanumeric_value(byte: u8) -> Option<usize> {
let value = match byte {
b'0'..=b'9' => byte - b'0',
b'A'..=b'Z' => byte - b'A' + 10,
b' ' => 36,
b'$' => 37,
b'%' => 38,
b'*' => 39,
b'+' => 40,
b'-' => 41,
b'.' => 42,
b'/' => 43,
b':' => 44,
_ => return None,
};
Some(usize::from(value))
}
fn append_bits(value: usize, length: usize, bits: &mut Vec<bool>) {
for index in (0..length).rev() {
bits.push(((value >> index) & 1) != 0);
}
}
fn total_bits(segment: Option<&Segment>, version: u8) -> Option<usize> {
let Some(segment) = segment else {
return Some(0);
};
let count_bits = segment.mode.char_count_bits(version);
if segment.num_chars >= 1 << count_bits {
return None;
}
Some(4 + count_bits + segment.bits.len())
}
fn num_raw_data_modules(version: u8) -> usize {
let version = usize::from(version);
let mut result = (16 * version + 128) * version + 64;
if version >= 2 {
let num_align = version / 7 + 2;
result -= (25 * num_align - 10) * num_align - 55;
if version >= 7 {
result -= 36;
}
}
result
}
fn num_data_codewords(version: u8, ecc: QrErrorCorrection) -> usize {
let row = ecc.ordinal();
let column = usize::from(version);
num_raw_data_modules(version) / 8
- usize::from(ECC_CODEWORDS_PER_BLOCK[row][column])
* usize::from(NUM_ERROR_CORRECTION_BLOCKS[row][column])
}
fn encode_segment(
segment: Option<&Segment>,
ecc: QrErrorCorrection,
boost_ecc: bool,
) -> Result<Symbol, QrEncodeError> {
let (version, used_bits) = (MIN_VERSION..=MAX_VERSION)
.find_map(|version| {
total_bits(segment, version)
.filter(|&used| used <= num_data_codewords(version, ecc) * 8)
.map(|used| (version, used))
})
.ok_or(QrEncodeError::DataTooLong)?;
let mut ecc = ecc;
if boost_ecc {
for candidate in [
QrErrorCorrection::Medium,
QrErrorCorrection::Quartile,
QrErrorCorrection::High,
] {
if used_bits <= num_data_codewords(version, candidate) * 8 {
ecc = candidate;
}
}
}
let capacity = num_data_codewords(version, ecc) * 8;
let mut bits = Vec::with_capacity(capacity);
if let Some(segment) = segment {
append_bits(segment.mode.indicator(), 4, &mut bits);
append_bits(
segment.num_chars,
segment.mode.char_count_bits(version),
&mut bits,
);
bits.extend_from_slice(&segment.bits);
}
append_bits(0, 4.min(capacity - bits.len()), &mut bits);
append_bits(0, (8 - bits.len() % 8) % 8, &mut bits);
let mut pad = 0xEC;
while bits.len() < capacity {
append_bits(pad, 8, &mut bits);
pad ^= 0xEC ^ 0x11;
}
let codewords: Vec<u8> = bits
.chunks(8)
.map(|byte| {
byte.iter()
.fold(0, |total, &bit| (total << 1) | u8::from(bit))
})
.collect();
Ok(Symbol::new(version, ecc, &codewords))
}
fn add_border(symbol: &Symbol, border: usize) -> QrMatrix {
let size = symbol.size + border * 2;
let mut modules = vec![vec![false; size]; size];
for (target, source) in modules[border..]
.iter_mut()
.zip(symbol.modules.chunks(symbol.size))
{
target[border..border + symbol.size].copy_from_slice(source);
}
QrMatrix {
size,
version: symbol.version,
mask: symbol.mask,
modules,
}
}
struct Symbol {
version: u8,
ecc: QrErrorCorrection,
size: usize,
mask: u8,
modules: Vec<bool>,
is_function: Vec<bool>,
}
impl Symbol {
fn new(version: u8, ecc: QrErrorCorrection, data_codewords: &[u8]) -> Self {
let size = usize::from(version) * 4 + 17;
let mut symbol = Self {
version,
ecc,
size,
mask: 0,
modules: vec![false; size * size],
is_function: vec![false; size * size],
};
symbol.draw_function_patterns();
let codewords = symbol.add_ecc_and_interleave(data_codewords);
symbol.draw_codewords(&codewords);
let mut min_penalty = usize::MAX;
for mask in 0..8 {
symbol.apply_mask(mask);
symbol.draw_format_bits(mask);
let penalty = symbol.penalty_score();
if penalty < min_penalty {
symbol.mask = mask;
min_penalty = penalty;
}
symbol.apply_mask(mask);
}
symbol.apply_mask(symbol.mask);
symbol.draw_format_bits(symbol.mask);
symbol
}
fn module(&self, x: usize, y: usize) -> bool {
self.modules[y * self.size + x]
}
fn set_function_module(&mut self, x: usize, y: usize, dark: bool) {
let index = y * self.size + x;
self.modules[index] = dark;
self.is_function[index] = true;
}
fn draw_function_patterns(&mut self) {
for index in 0..self.size {
self.set_function_module(6, index, index % 2 == 0);
self.set_function_module(index, 6, index % 2 == 0);
}
self.draw_finder_pattern(3, 3);
self.draw_finder_pattern(self.size - 4, 3);
self.draw_finder_pattern(3, self.size - 4);
let positions = self.alignment_pattern_positions();
let last = positions.len().saturating_sub(1);
for (column_index, &x) in positions.iter().enumerate() {
for (row_index, &y) in positions.iter().enumerate() {
let overlaps_finder = (column_index == 0 && (row_index == 0 || row_index == last))
|| (column_index == last && row_index == 0);
if !overlaps_finder {
self.draw_alignment_pattern(x, y);
}
}
}
self.draw_format_bits(0);
self.draw_version();
}
fn draw_format_bits(&mut self, mask: u8) {
let data = (self.ecc.format_bits() << 3) | u32::from(mask);
let mut remainder = data;
for _ in 0..10 {
remainder = (remainder << 1) ^ ((remainder >> 9) * 0x537);
}
let bits = ((data << 10) | remainder) ^ 0x5412;
let size = self.size;
for index in 0..=5 {
self.set_function_module(8, index, bit(bits, index));
}
self.set_function_module(8, 7, bit(bits, 6));
self.set_function_module(8, 8, bit(bits, 7));
self.set_function_module(7, 8, bit(bits, 8));
for index in 9..15 {
self.set_function_module(14 - index, 8, bit(bits, index));
}
for index in 0..8 {
self.set_function_module(size - 1 - index, 8, bit(bits, index));
}
for index in 8..15 {
self.set_function_module(8, size - 15 + index, bit(bits, index));
}
self.set_function_module(8, size - 8, true);
}
fn draw_version(&mut self) {
if self.version < 7 {
return;
}
let version = u32::from(self.version);
let mut remainder = version;
for _ in 0..12 {
remainder = (remainder << 1) ^ ((remainder >> 11) * 0x1F25);
}
let bits = (version << 12) | remainder;
for index in 0..18 {
let dark = bit(bits, index);
let a = self.size - 11 + index % 3;
let b = index / 3;
self.set_function_module(a, b, dark);
self.set_function_module(b, a, dark);
}
}
fn draw_finder_pattern(&mut self, x: usize, y: usize) {
for row in y.saturating_sub(4)..=(y + 4).min(self.size - 1) {
for column in x.saturating_sub(4)..=(x + 4).min(self.size - 1) {
let distance = column.abs_diff(x).max(row.abs_diff(y));
self.set_function_module(column, row, distance != 2 && distance != 4);
}
}
}
fn draw_alignment_pattern(&mut self, x: usize, y: usize) {
for row in y - 2..=y + 2 {
for column in x - 2..=x + 2 {
let distance = column.abs_diff(x).max(row.abs_diff(y));
self.set_function_module(column, row, distance != 1);
}
}
}
fn alignment_pattern_positions(&self) -> Vec<usize> {
if self.version == 1 {
return Vec::new();
}
let version = usize::from(self.version);
let num_align = version / 7 + 2;
let step = if version == 32 {
26
} else {
(version * 4 + 4).div_ceil(num_align * 2 - 2) * 2
};
let mut positions = Vec::with_capacity(num_align);
positions.push(6);
for offset in (0..num_align - 1).rev() {
positions.push(self.size - 7 - offset * step);
}
positions
}
fn add_ecc_and_interleave(&self, data: &[u8]) -> Vec<u8> {
let row = self.ecc.ordinal();
let column = usize::from(self.version);
let num_blocks = usize::from(NUM_ERROR_CORRECTION_BLOCKS[row][column]);
let block_ecc_len = usize::from(ECC_CODEWORDS_PER_BLOCK[row][column]);
let raw_codewords = num_raw_data_modules(self.version) / 8;
let num_short_blocks = num_blocks - raw_codewords % num_blocks;
let short_block_len = raw_codewords / num_blocks;
let short_data_len = short_block_len - block_ecc_len;
let divisor = reed_solomon_divisor(block_ecc_len);
let mut blocks = Vec::with_capacity(num_blocks);
let mut remaining = data;
for index in 0..num_blocks {
let take = short_data_len + usize::from(index >= num_short_blocks);
let (block_data, rest) = remaining.split_at(take.min(remaining.len()));
let mut block = Vec::with_capacity(short_block_len + 1);
remaining = rest;
block.extend_from_slice(block_data);
if index < num_short_blocks {
block.push(0);
}
block.extend(reed_solomon_remainder(block_data, &divisor));
blocks.push(block);
}
let mut result = Vec::with_capacity(raw_codewords);
for index in 0..=short_block_len {
for (block_index, block) in blocks.iter().enumerate() {
if index != short_data_len || block_index >= num_short_blocks {
result.extend(block.get(index));
}
}
}
result
}
fn draw_codewords(&mut self, codewords: &[u8]) {
let total_bits = codewords.len() * 8;
let mut bit_index = 0;
let mut right = self.size - 1;
loop {
if right == 6 {
right = 5;
}
let upward = ((right + 1) & 2) == 0;
for vertical in 0..self.size {
let y = if upward {
self.size - 1 - vertical
} else {
vertical
};
for x in [right, right - 1] {
let index = y * self.size + x;
if !self.is_function[index] && bit_index < total_bits {
let byte = codewords[bit_index >> 3];
self.modules[index] = ((byte >> (7 - (bit_index & 7))) & 1) != 0;
bit_index += 1;
}
}
}
if right < 3 {
break;
}
right -= 2;
}
}
fn apply_mask(&mut self, mask: u8) {
for y in 0..self.size {
for x in 0..self.size {
let invert = match mask {
0 => (x + y) % 2 == 0,
1 => y % 2 == 0,
2 => x % 3 == 0,
3 => (x + y) % 3 == 0,
4 => (x / 3 + y / 2) % 2 == 0,
5 => x * y % 2 + x * y % 3 == 0,
6 => (x * y % 2 + x * y % 3) % 2 == 0,
_ => ((x + y) % 2 + x * y % 3) % 2 == 0,
};
let index = y * self.size + x;
if invert && !self.is_function[index] {
self.modules[index] = !self.modules[index];
}
}
}
}
fn penalty_score(&self) -> usize {
let size = self.size;
let mut result = 0;
for y in 0..size {
result += self.line_penalty((0..size).map(|x| self.module(x, y)));
}
for x in 0..size {
result += self.line_penalty((0..size).map(|y| self.module(x, y)));
}
for y in 0..size - 1 {
for x in 0..size - 1 {
let color = self.module(x, y);
if color == self.module(x + 1, y)
&& color == self.module(x, y + 1)
&& color == self.module(x + 1, y + 1)
{
result += PENALTY_N2;
}
}
}
let dark = self.modules.iter().filter(|&&dark| dark).count();
let total = size * size;
let steps = (dark * 20).abs_diff(total * 10).div_ceil(total);
result + steps.saturating_sub(1) * PENALTY_N4
}
fn line_penalty(&self, line: impl Iterator<Item = bool>) -> usize {
let mut result = 0;
let mut run_color = false;
let mut run_length = 0;
let mut history = [0; 7];
for color in line {
if color == run_color {
run_length += 1;
if run_length == 5 {
result += PENALTY_N1;
} else if run_length > 5 {
result += 1;
}
} else {
self.add_run_history(run_length, &mut history);
if !run_color {
result += count_finder_patterns(&history) * PENALTY_N3;
}
run_color = color;
run_length = 1;
}
}
result + self.terminate_run_history(run_color, run_length, &mut history) * PENALTY_N3
}
fn terminate_run_history(
&self,
run_color: bool,
mut run_length: usize,
history: &mut [usize; 7],
) -> usize {
if run_color {
self.add_run_history(run_length, history);
run_length = 0;
}
run_length += self.size;
self.add_run_history(run_length, history);
count_finder_patterns(history)
}
fn add_run_history(&self, mut run_length: usize, history: &mut [usize; 7]) {
if history[0] == 0 {
run_length += self.size;
}
history.rotate_right(1);
history[0] = run_length;
}
}
fn count_finder_patterns(history: &[usize; 7]) -> usize {
let n = history[1];
let core =
n > 0 && history[2] == n && history[3] == n * 3 && history[4] == n && history[5] == n;
usize::from(core && history[0] >= n * 4 && history[6] >= n)
+ usize::from(core && history[6] >= n * 4 && history[0] >= n)
}
const fn bit(value: u32, index: usize) -> bool {
((value >> index) & 1) != 0
}
fn reed_solomon_divisor(degree: usize) -> Vec<u8> {
let mut result = vec![0; degree];
let mut root = 1;
if let Some(last) = result.last_mut() {
*last = 1;
}
for _ in 0..degree {
for index in 0..degree {
let next = result.get(index + 1).copied().unwrap_or(0);
result[index] = reed_solomon_multiply(result[index], root) ^ next;
}
root = reed_solomon_multiply(root, 2);
}
result
}
fn reed_solomon_remainder(data: &[u8], divisor: &[u8]) -> Vec<u8> {
let mut result = vec![0; divisor.len()];
for &byte in data {
let factor = byte ^ result.first().copied().unwrap_or(0);
result.rotate_left(1);
if let Some(last) = result.last_mut() {
*last = 0;
}
for (value, &coefficient) in result.iter_mut().zip(divisor) {
*value ^= reed_solomon_multiply(coefficient, factor);
}
}
result
}
fn reed_solomon_multiply(x: u8, y: u8) -> u8 {
let mut product: u8 = 0;
for index in (0..8).rev() {
let carry = product & 0x80 != 0;
product <<= 1;
if carry {
product ^= 0x1D;
}
if ((y >> index) & 1) != 0 {
product ^= x;
}
}
product
}
#[cfg(test)]
mod tests {
use super::{
QrEncodeError, QrErrorCorrection, QrMatrix, encode_qr, encode_text, encode_uri_component,
render_qr_svg,
};
struct Expected {
value: &'static str,
ecc: QrErrorCorrection,
border: usize,
size: usize,
version: u8,
mask: u8,
rows: &'static [&'static str],
}
const NUMERIC_LOW_BORDER_1: Expected = Expected {
value: "0123456789",
ecc: QrErrorCorrection::Low,
border: 1,
size: 23,
version: 1,
mask: 3,
rows: &[
".......................",
".#######.#.###.#######.",
".#.....#...##..#.....#.",
".#.###.#.##.#..#.###.#.",
".#.###.#.##..#.#.###.#.",
".#.###.#.#..#..#.###.#.",
".#.....#..####.#.....#.",
".#######.#.#.#.#######.",
"............##.........",
".####..#.######..###.#.",
"..##.##...#.####..##...",
".#..#..#...##.......#..",
".######.#.###..####.#..",
".###.###...#.#..#......",
".........#.##..#.....#.",
".#######....##..#..###.",
".#.....#..##....##...#.",
".#.###.#...#.#######.#.",
".#.###.#.#.##..#.##.#..",
".#.###.#.###.#.##......",
".#.....#.##...#...####.",
".#######.#....#..##.#..",
".......................",
],
};
const ALPHANUMERIC_QUARTILE_BORDER_4: Expected = Expected {
value: "HELLO WORLD",
ecc: QrErrorCorrection::Quartile,
border: 4,
size: 29,
version: 1,
mask: 0,
rows: &[
".............................",
".............................",
".............................",
".............................",
"....#######.##....#######....",
"....#.....#.#..#..#.....#....",
"....#.###.#.#..##.#.###.#....",
"....#.###.#.#.....#.###.#....",
"....#.###.#.#.#...#.###.#....",
"....#.....#...#...#.....#....",
"....#######.#.#.#.#######....",
"............#................",
".....##.#.##....#.#.#####....",
".....#......####....#...#....",
"......##.###.##...#.##.......",
".....##.##.#..##.#.#.###.....",
"....#...#.#.#.###.###.#.#....",
"............##.#..#...#.#....",
"....#######.#.#....#.##......",
"....#.....#..#.##.##.#.......",
"....#.###.#.#.#...#######....",
"....#.###.#..#.#.#.#...#.....",
"....#.###.#.#..#.###.#..#....",
"....#.....#.#.####...#.##....",
"....#######....#.###....#....",
".............................",
".............................",
".............................",
".............................",
],
};
const UTF8_MEDIUM_BORDER_1: Expected = Expected {
value: "https://lumen.santi020k.com/日本語?name=Molina🌞",
ecc: QrErrorCorrection::Medium,
border: 1,
size: 35,
version: 4,
mask: 2,
rows: &[
"...................................",
".#######..###..##.#.#...##.#######.",
".#.....#...#......#....#.#.#.....#.",
".#.###.#.#..##.#.#.##.###..#.###.#.",
".#.###.#.#..##..#..#.#.###.#.###.#.",
".#.###.#.###...###.#....#..#.###.#.",
".#.....#.###..##...##..#...#.....#.",
".#######.#.#.#.#.#.#.#.#.#.#######.",
".........#.#####.#.#.##............",
".#.#####..#.#.#...#.##.###.#####...",
"....###.....#.#..####..##..##.##.#.",
"...#####.##..###.##...##..##.#.#.#.",
"...#.#...#....#..###..#....#.###...",
".##..#.#...##..#.#.....#.##.###.##.",
".##.#.#.#...#...#.##..#....#..#.##.",
"...##.####.....#######....##.#.##..",
".##......########...####..##.##....",
".###.###.#.##..##.####.####.##...#.",
"..#.##....##..##...##...#..##.###..",
".##..###..##..#.##.#.##.....##.#.#.",
"..#.....####...###....##..#.####.#.",
".#.###.##.####.....###.##.#..##.#..",
".##..##.......#####...#.##.##....#.",
".#...#.####.###...#.#.#...####.##..",
".#....#.#...#.##.#.#..##.....#..#..",
".#.#.####..#.#.#..#....#.#####..#..",
".........##...##.#####...#...#.#.#.",
".#######..##...####....###.#.#.###.",
".#.....#.#..#..#.###.##.##...####..",
".#.###.#.##.###..#...#.#.######.#..",
".#.###.#.#..#..##..###..###..#..##.",
".#.###.#.#..###.######.######......",
".#.....#..#.#..##.....#...##.#.....",
".#######.###.##.#.#..#.####.#.###..",
"...................................",
],
};
const UTF8_HIGH_BORDER_4: Expected = Expected {
value: "Molina🌞 日本",
ecc: QrErrorCorrection::High,
border: 4,
size: 37,
version: 3,
mask: 2,
rows: &[
".....................................",
".....................................",
".....................................",
".....................................",
"....#######.#.##..##......#######....",
"....#.....#.###.##.#..#.#.#.....#....",
"....#.###.#.###....#.##...#.###.#....",
"....#.###.#.....######....#.###.#....",
"....#.###.#..###..#..###..#.###.#....",
"....#.....#.##.#.#####....#.....#....",
"....#######.#.#.#.#.#.#.#.#######....",
"............#.#...#..####............",
"......###.#.#..###.##..#.###..###....",
"....#..##..#.##.####..#.##.#.#.##....",
".....##.####.#...#.#.#.#.#...#.......",
"....#..#.#..#.###..##.###..#.##......",
"......###.##.#.###.##.###.#...#.#....",
".....##..#...#.#.##.#.....#######....",
"....##....#.#.#..#..#..#.#...####....",
".....#.###..###.#..#....#..#.#..#....",
".....##.#.#.##.##.#...####..#.#......",
"....#.#..#....####...#......##.#.....",
"....#.##..##.#.####.###.##..#.#......",
"....#.#.##.##..##.#...#..#.#..#......",
"....#..##.#...#..#.#....######.#.....",
"............###..##...#.#...###......",
"....#######...###.##....#.#.#..##....",
"....#.....#..#....###...#...##.##....",
"....#.###.#.#...#...#########.#......",
"....#.###.#.####.....#.####..##......",
"....#.###.#.#..##....#.##..##..#.....",
"....#.....#...#.#..#..#...###.##.....",
"....#######...#####.#.#####...#......",
".....................................",
".....................................",
".....................................",
".....................................",
],
};
const EMPTY_LOW_BORDER_1: Expected = Expected {
value: "",
ecc: QrErrorCorrection::Low,
border: 1,
size: 23,
version: 1,
mask: 7,
rows: &[
".......................",
".#######..#.##.#######.",
".#.....#.##.#..#.....#.",
".#.###.#.##..#.#.###.#.",
".#.###.#..#.#..#.###.#.",
".#.###.#.#...#.#.###.#.",
".#.....#.#..##.#.....#.",
".#######.#.#.#.#######.",
".........#####.........",
".##.#..##.##...###.##..",
"..####..#.#....#...#...",
".#######.##..##.....##.",
"...##...##.##.....##.#.",
".......#.##..#.#.#.#.#.",
".........#..#...###....",
".#######.#.#..#.#..###.",
".#.....#..#.###.###.##.",
".#.###.#...##..#####...",
".#.###.#.####.....####.",
".#.###.#..##.#...#...#.",
".#.....#.#.#..##.#.#...",
".#######.#####...###...",
".......................",
],
};
const LUMEN_SVG: &str = concat!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 230 230\"><rect fill=\"white\"",
" width=\"230\" height=\"230\"/><path fill=\"black\" d=\"M10,10h10v10h-10zM20,10h10v10h-",
"10zM30,10h10v10h-10zM40,10h10v10h-10zM50,10h10v10h-10zM60,10h10v10h-10zM70,10h10",
"v10h-10zM100,10h10v10h-10zM120,10h10v10h-10zM130,10h10v10h-10zM150,10h10v10h-10z",
"M160,10h10v10h-10zM170,10h10v10h-10zM180,10h10v10h-10zM190,10h10v10h-10zM200,10h",
"10v10h-10zM210,10h10v10h-10zM10,20h10v10h-10zM70,20h10v10h-10zM90,20h10v10h-10zM",
"100,20h10v10h-10zM120,20h10v10h-10zM150,20h10v10h-10zM210,20h10v10h-10zM10,30h10",
"v10h-10zM30,30h10v10h-10zM40,30h10v10h-10zM50,30h10v10h-10zM70,30h10v10h-10zM90,",
"30h10v10h-10zM100,30h10v10h-10zM130,30h10v10h-10zM150,30h10v10h-10zM170,30h10v10",
"h-10zM180,30h10v10h-10zM190,30h10v10h-10zM210,30h10v10h-10zM10,40h10v10h-10zM30,",
"40h10v10h-10zM40,40h10v10h-10zM50,40h10v10h-10zM70,40h10v10h-10zM100,40h10v10h-1",
"0zM120,40h10v10h-10zM150,40h10v10h-10zM170,40h10v10h-10zM180,40h10v10h-10zM190,4",
"0h10v10h-10zM210,40h10v10h-10zM10,50h10v10h-10zM30,50h10v10h-10zM40,50h10v10h-10",
"zM50,50h10v10h-10zM70,50h10v10h-10zM90,50h10v10h-10zM130,50h10v10h-10zM150,50h10",
"v10h-10zM170,50h10v10h-10zM180,50h10v10h-10zM190,50h10v10h-10zM210,50h10v10h-10z",
"M10,60h10v10h-10zM70,60h10v10h-10zM90,60h10v10h-10zM120,60h10v10h-10zM130,60h10v",
"10h-10zM150,60h10v10h-10zM210,60h10v10h-10zM10,70h10v10h-10zM20,70h10v10h-10zM30",
",70h10v10h-10zM40,70h10v10h-10zM50,70h10v10h-10zM60,70h10v10h-10zM70,70h10v10h-1",
"0zM90,70h10v10h-10zM110,70h10v10h-10zM130,70h10v10h-10zM150,70h10v10h-10zM160,70",
"h10v10h-10zM170,70h10v10h-10zM180,70h10v10h-10zM190,70h10v10h-10zM200,70h10v10h-",
"10zM210,70h10v10h-10zM90,80h10v10h-10zM100,80h10v10h-10zM110,80h10v10h-10zM120,8",
"0h10v10h-10zM130,80h10v10h-10zM10,90h10v10h-10zM20,90h10v10h-10zM40,90h10v10h-10",
"zM70,90h10v10h-10zM80,90h10v10h-10zM100,90h10v10h-10zM110,90h10v10h-10zM150,90h1",
"0v10h-10zM160,90h10v10h-10zM170,90h10v10h-10zM190,90h10v10h-10zM200,90h10v10h-10",
"zM20,100h10v10h-10zM40,100h10v10h-10zM50,100h10v10h-10zM60,100h10v10h-10zM90,100",
"h10v10h-10zM110,100h10v10h-10zM150,100h10v10h-10zM210,100h10v10h-10zM10,110h10v1",
"0h-10zM30,110h10v10h-10zM50,110h10v10h-10zM70,110h10v10h-10zM80,110h10v10h-10zM9",
"0,110h10v10h-10zM100,110h10v10h-10zM110,110h10v10h-10zM130,110h10v10h-10zM140,11",
"0h10v10h-10zM180,110h10v10h-10zM190,110h10v10h-10zM210,110h10v10h-10zM20,120h10v",
"10h-10zM30,120h10v10h-10zM60,120h10v10h-10zM90,120h10v10h-10zM110,120h10v10h-10z",
"M120,120h10v10h-10zM180,120h10v10h-10zM200,120h10v10h-10zM210,120h10v10h-10zM10,",
"130h10v10h-10zM20,130h10v10h-10zM30,130h10v10h-10zM40,130h10v10h-10zM50,130h10v1",
"0h-10zM60,130h10v10h-10zM70,130h10v10h-10zM80,130h10v10h-10zM90,130h10v10h-10zM1",
"30,130h10v10h-10zM150,130h10v10h-10zM170,130h10v10h-10zM200,130h10v10h-10zM90,14",
"0h10v10h-10zM120,140h10v10h-10zM160,140h10v10h-10zM170,140h10v10h-10zM190,140h10",
"v10h-10zM10,150h10v10h-10zM20,150h10v10h-10zM30,150h10v10h-10zM40,150h10v10h-10z",
"M50,150h10v10h-10zM60,150h10v10h-10zM70,150h10v10h-10zM90,150h10v10h-10zM110,150",
"h10v10h-10zM140,150h10v10h-10zM160,150h10v10h-10zM190,150h10v10h-10zM200,150h10v",
"10h-10zM10,160h10v10h-10zM70,160h10v10h-10zM100,160h10v10h-10zM110,160h10v10h-10",
"zM120,160h10v10h-10zM130,160h10v10h-10zM140,160h10v10h-10zM160,160h10v10h-10zM17",
"0,160h10v10h-10zM10,170h10v10h-10zM30,170h10v10h-10zM40,170h10v10h-10zM50,170h10",
"v10h-10zM70,170h10v10h-10zM110,170h10v10h-10zM120,170h10v10h-10zM150,170h10v10h-",
"10zM160,170h10v10h-10zM170,170h10v10h-10zM210,170h10v10h-10zM10,180h10v10h-10zM3",
"0,180h10v10h-10zM40,180h10v10h-10zM50,180h10v10h-10zM70,180h10v10h-10zM90,180h10",
"v10h-10zM120,180h10v10h-10zM160,180h10v10h-10zM180,180h10v10h-10zM200,180h10v10h",
"-10zM210,180h10v10h-10zM10,190h10v10h-10zM30,190h10v10h-10zM40,190h10v10h-10zM50",
",190h10v10h-10zM70,190h10v10h-10zM100,190h10v10h-10zM110,190h10v10h-10zM130,190h",
"10v10h-10zM170,190h10v10h-10zM180,190h10v10h-10zM190,190h10v10h-10zM210,190h10v1",
"0h-10zM10,200h10v10h-10zM70,200h10v10h-10zM90,200h10v10h-10zM100,200h10v10h-10zM",
"140,200h10v10h-10zM150,200h10v10h-10zM160,200h10v10h-10zM10,210h10v10h-10zM20,21",
"0h10v10h-10zM30,210h10v10h-10zM40,210h10v10h-10zM50,210h10v10h-10zM60,210h10v10h",
"-10zM70,210h10v10h-10zM90,210h10v10h-10zM120,210h10v10h-10zM130,210h10v10h-10zM1",
"60,210h10v10h-10zM180,210h10v10h-10zM200,210h10v10h-10z\"/></svg>",
);
const LUMEN_DATA_URL: &str = concat!(
"data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg",
"%22%20viewBox%3D%220%200%20230%20230%22%3E%3Crect%20fill%3D%22white%22%20width%3",
"D%22230%22%20height%3D%22230%22%2F%3E%3Cpath%20fill%3D%22black%22%20d%3D%22M10%2",
"C10h10v10h-10zM20%2C10h10v10h-10zM30%2C10h10v10h-10zM40%2C10h10v10h-10zM50%2C10h",
"10v10h-10zM60%2C10h10v10h-10zM70%2C10h10v10h-10zM100%2C10h10v10h-10zM120%2C10h10",
"v10h-10zM130%2C10h10v10h-10zM150%2C10h10v10h-10zM160%2C10h10v10h-10zM170%2C10h10",
"v10h-10zM180%2C10h10v10h-10zM190%2C10h10v10h-10zM200%2C10h10v10h-10zM210%2C10h10",
"v10h-10zM10%2C20h10v10h-10zM70%2C20h10v10h-10zM90%2C20h10v10h-10zM100%2C20h10v10",
"h-10zM120%2C20h10v10h-10zM150%2C20h10v10h-10zM210%2C20h10v10h-10zM10%2C30h10v10h",
"-10zM30%2C30h10v10h-10zM40%2C30h10v10h-10zM50%2C30h10v10h-10zM70%2C30h10v10h-10z",
"M90%2C30h10v10h-10zM100%2C30h10v10h-10zM130%2C30h10v10h-10zM150%2C30h10v10h-10zM",
"170%2C30h10v10h-10zM180%2C30h10v10h-10zM190%2C30h10v10h-10zM210%2C30h10v10h-10zM",
"10%2C40h10v10h-10zM30%2C40h10v10h-10zM40%2C40h10v10h-10zM50%2C40h10v10h-10zM70%2",
"C40h10v10h-10zM100%2C40h10v10h-10zM120%2C40h10v10h-10zM150%2C40h10v10h-10zM170%2",
"C40h10v10h-10zM180%2C40h10v10h-10zM190%2C40h10v10h-10zM210%2C40h10v10h-10zM10%2C",
"50h10v10h-10zM30%2C50h10v10h-10zM40%2C50h10v10h-10zM50%2C50h10v10h-10zM70%2C50h1",
"0v10h-10zM90%2C50h10v10h-10zM130%2C50h10v10h-10zM150%2C50h10v10h-10zM170%2C50h10",
"v10h-10zM180%2C50h10v10h-10zM190%2C50h10v10h-10zM210%2C50h10v10h-10zM10%2C60h10v",
"10h-10zM70%2C60h10v10h-10zM90%2C60h10v10h-10zM120%2C60h10v10h-10zM130%2C60h10v10",
"h-10zM150%2C60h10v10h-10zM210%2C60h10v10h-10zM10%2C70h10v10h-10zM20%2C70h10v10h-",
"10zM30%2C70h10v10h-10zM40%2C70h10v10h-10zM50%2C70h10v10h-10zM60%2C70h10v10h-10zM",
"70%2C70h10v10h-10zM90%2C70h10v10h-10zM110%2C70h10v10h-10zM130%2C70h10v10h-10zM15",
"0%2C70h10v10h-10zM160%2C70h10v10h-10zM170%2C70h10v10h-10zM180%2C70h10v10h-10zM19",
"0%2C70h10v10h-10zM200%2C70h10v10h-10zM210%2C70h10v10h-10zM90%2C80h10v10h-10zM100",
"%2C80h10v10h-10zM110%2C80h10v10h-10zM120%2C80h10v10h-10zM130%2C80h10v10h-10zM10%",
"2C90h10v10h-10zM20%2C90h10v10h-10zM40%2C90h10v10h-10zM70%2C90h10v10h-10zM80%2C90",
"h10v10h-10zM100%2C90h10v10h-10zM110%2C90h10v10h-10zM150%2C90h10v10h-10zM160%2C90",
"h10v10h-10zM170%2C90h10v10h-10zM190%2C90h10v10h-10zM200%2C90h10v10h-10zM20%2C100",
"h10v10h-10zM40%2C100h10v10h-10zM50%2C100h10v10h-10zM60%2C100h10v10h-10zM90%2C100",
"h10v10h-10zM110%2C100h10v10h-10zM150%2C100h10v10h-10zM210%2C100h10v10h-10zM10%2C",
"110h10v10h-10zM30%2C110h10v10h-10zM50%2C110h10v10h-10zM70%2C110h10v10h-10zM80%2C",
"110h10v10h-10zM90%2C110h10v10h-10zM100%2C110h10v10h-10zM110%2C110h10v10h-10zM130",
"%2C110h10v10h-10zM140%2C110h10v10h-10zM180%2C110h10v10h-10zM190%2C110h10v10h-10z",
"M210%2C110h10v10h-10zM20%2C120h10v10h-10zM30%2C120h10v10h-10zM60%2C120h10v10h-10",
"zM90%2C120h10v10h-10zM110%2C120h10v10h-10zM120%2C120h10v10h-10zM180%2C120h10v10h",
"-10zM200%2C120h10v10h-10zM210%2C120h10v10h-10zM10%2C130h10v10h-10zM20%2C130h10v1",
"0h-10zM30%2C130h10v10h-10zM40%2C130h10v10h-10zM50%2C130h10v10h-10zM60%2C130h10v1",
"0h-10zM70%2C130h10v10h-10zM80%2C130h10v10h-10zM90%2C130h10v10h-10zM130%2C130h10v",
"10h-10zM150%2C130h10v10h-10zM170%2C130h10v10h-10zM200%2C130h10v10h-10zM90%2C140h",
"10v10h-10zM120%2C140h10v10h-10zM160%2C140h10v10h-10zM170%2C140h10v10h-10zM190%2C",
"140h10v10h-10zM10%2C150h10v10h-10zM20%2C150h10v10h-10zM30%2C150h10v10h-10zM40%2C",
"150h10v10h-10zM50%2C150h10v10h-10zM60%2C150h10v10h-10zM70%2C150h10v10h-10zM90%2C",
"150h10v10h-10zM110%2C150h10v10h-10zM140%2C150h10v10h-10zM160%2C150h10v10h-10zM19",
"0%2C150h10v10h-10zM200%2C150h10v10h-10zM10%2C160h10v10h-10zM70%2C160h10v10h-10zM",
"100%2C160h10v10h-10zM110%2C160h10v10h-10zM120%2C160h10v10h-10zM130%2C160h10v10h-",
"10zM140%2C160h10v10h-10zM160%2C160h10v10h-10zM170%2C160h10v10h-10zM10%2C170h10v1",
"0h-10zM30%2C170h10v10h-10zM40%2C170h10v10h-10zM50%2C170h10v10h-10zM70%2C170h10v1",
"0h-10zM110%2C170h10v10h-10zM120%2C170h10v10h-10zM150%2C170h10v10h-10zM160%2C170h",
"10v10h-10zM170%2C170h10v10h-10zM210%2C170h10v10h-10zM10%2C180h10v10h-10zM30%2C18",
"0h10v10h-10zM40%2C180h10v10h-10zM50%2C180h10v10h-10zM70%2C180h10v10h-10zM90%2C18",
"0h10v10h-10zM120%2C180h10v10h-10zM160%2C180h10v10h-10zM180%2C180h10v10h-10zM200%",
"2C180h10v10h-10zM210%2C180h10v10h-10zM10%2C190h10v10h-10zM30%2C190h10v10h-10zM40",
"%2C190h10v10h-10zM50%2C190h10v10h-10zM70%2C190h10v10h-10zM100%2C190h10v10h-10zM1",
"10%2C190h10v10h-10zM130%2C190h10v10h-10zM170%2C190h10v10h-10zM180%2C190h10v10h-1",
"0zM190%2C190h10v10h-10zM210%2C190h10v10h-10zM10%2C200h10v10h-10zM70%2C200h10v10h",
"-10zM90%2C200h10v10h-10zM100%2C200h10v10h-10zM140%2C200h10v10h-10zM150%2C200h10v",
"10h-10zM160%2C200h10v10h-10zM10%2C210h10v10h-10zM20%2C210h10v10h-10zM30%2C210h10",
"v10h-10zM40%2C210h10v10h-10zM50%2C210h10v10h-10zM60%2C210h10v10h-10zM70%2C210h10",
"v10h-10zM90%2C210h10v10h-10zM120%2C210h10v10h-10zM130%2C210h10v10h-10zM160%2C210",
"h10v10h-10zM180%2C210h10v10h-10zM200%2C210h10v10h-10z%22%2F%3E%3C%2Fsvg%3E",
);
fn draw(matrix: &QrMatrix) -> Vec<String> {
matrix
.modules
.iter()
.map(|row| {
row.iter()
.map(|&dark| if dark { '#' } else { '.' })
.collect()
})
.collect()
}
fn assert_matches_uqr(expected: &Expected) {
let actual = encode_qr(expected.value, expected.ecc, expected.border)
.map(|matrix| (matrix.size, matrix.version, matrix.mask, draw(&matrix)));
let rows = expected.rows.iter().map(|&row| row.to_owned()).collect();
assert_eq!(
actual,
Ok((expected.size, expected.version, expected.mask, rows)),
"{:?}",
expected.value
);
}
#[test]
fn numeric_text_matches_uqr() {
assert_matches_uqr(&NUMERIC_LOW_BORDER_1);
}
#[test]
fn alphanumeric_text_matches_uqr() {
assert_matches_uqr(&ALPHANUMERIC_QUARTILE_BORDER_4);
}
#[test]
fn utf8_text_matches_uqr() {
assert_matches_uqr(&UTF8_MEDIUM_BORDER_1);
assert_matches_uqr(&UTF8_HIGH_BORDER_4);
}
#[test]
fn empty_text_matches_uqr() {
assert_matches_uqr(&EMPTY_LOW_BORDER_1);
assert_eq!(render_qr_svg("").map(|svg| svg.len()), Some(4267));
}
#[test]
fn svg_matches_uqr() {
assert_eq!(render_qr_svg("Lumen").as_deref(), Some(LUMEN_SVG));
}
#[test]
fn data_url_matches_node() {
let url = render_qr_svg("Lumen")
.map(|svg| format!("data:image/svg+xml;utf8,{}", encode_uri_component(&svg)));
assert_eq!(url.as_deref(), Some(LUMEN_DATA_URL));
}
#[test]
fn uri_component_encoding_matches_javascript() {
let cases = [
("", ""),
("a b&c/d?e=f#g", "a%20b%26c%2Fd%3Fe%3Df%23g"),
("-_.!~*'()", "-_.!~*'()"),
("日本語🌞", "%E6%97%A5%E6%9C%AC%E8%AA%9E%F0%9F%8C%9E"),
(
"<b title=\"x\">%,;:@$+=\n\t",
"%3Cb%20title%3D%22x%22%3E%25%2C%3B%3A%40%24%2B%3D%0A%09",
),
];
for (value, expected) in cases {
assert_eq!(encode_uri_component(value), expected, "{value:?}");
}
}
#[test]
fn boosted_error_correction_matches_uqr() {
let cases = [
(
"HELLO WORLD",
QrErrorCorrection::Low,
QrErrorCorrection::Quartile,
1,
0,
),
(
"Molina🌞 日本",
QrErrorCorrection::Medium,
QrErrorCorrection::Quartile,
2,
1,
),
(
"0123456789",
QrErrorCorrection::Medium,
QrErrorCorrection::High,
1,
6,
),
(
"https://lumen.santi020k.com/日本語?name=Molina🌞",
QrErrorCorrection::Low,
QrErrorCorrection::Low,
3,
7,
),
];
for (value, requested, boosted, version, mask) in cases {
let actual = encode_text(value, requested, true, 1);
assert_eq!(actual, encode_qr(value, boosted, 1), "{value:?}");
assert_eq!(
actual.map(|matrix| (matrix.version, matrix.mask)),
Ok((version, mask)),
"{value:?}"
);
}
}
#[test]
fn zero_border_returns_bare_symbol() {
let bare = encode_qr("HELLO WORLD", QrErrorCorrection::Quartile, 0).map(|matrix| {
let rows = draw(&matrix);
(matrix.size, rows)
});
let inner = ALPHANUMERIC_QUARTILE_BORDER_4.rows[4..25]
.iter()
.map(|row| row[4..25].to_owned())
.collect();
assert_eq!(bare, Ok((21, inner)));
}
#[test]
fn largest_inputs_encode_like_uqr() {
let cases = [
("1".repeat(3000), QrErrorCorrection::Low, 25, 2),
("1".repeat(7089), QrErrorCorrection::Low, 40, 2),
("1".repeat(3057), QrErrorCorrection::High, 40, 2),
("A".repeat(4296), QrErrorCorrection::Low, 40, 2),
("a".repeat(2953), QrErrorCorrection::Low, 40, 1),
];
for (value, ecc, version, mask) in cases {
let actual = encode_qr(&value, ecc, 1).map(|matrix| (matrix.version, matrix.mask));
assert_eq!(actual, Ok((version, mask)), "{} x {ecc:?}", value.len());
}
}
#[test]
fn inputs_past_capacity_fail_like_uqr() {
let cases = [
("1".repeat(7090), QrErrorCorrection::Low),
("1".repeat(3058), QrErrorCorrection::High),
("A".repeat(4297), QrErrorCorrection::Low),
("a".repeat(2954), QrErrorCorrection::Low),
("🌞".repeat(739), QrErrorCorrection::Low),
];
for (value, ecc) in cases {
assert_eq!(
encode_qr(&value, ecc, 1),
Err(QrEncodeError::DataTooLong),
"{} bytes",
value.len()
);
}
assert_eq!(render_qr_svg(&"a".repeat(2954)), None);
}
#[test]
fn adversarial_lengths_are_rejected() {
for value in [
"x".repeat(100_000),
"🌞".repeat(3000),
"1".repeat(1_000_000),
] {
assert_eq!(
encode_qr(&value, QrErrorCorrection::Low, 1),
Err(QrEncodeError::DataTooLong)
);
assert_eq!(render_qr_svg(&value), None);
}
}
#[test]
fn border_width_is_bounded() {
assert_eq!(
encode_qr("A", QrErrorCorrection::Low, 256).map(|matrix| matrix.size),
Ok(533)
);
assert_eq!(
encode_qr("A", QrErrorCorrection::Low, 257),
Err(QrEncodeError::BorderTooLarge)
);
assert_eq!(
encode_qr("A", QrErrorCorrection::Low, usize::MAX),
Err(QrEncodeError::BorderTooLarge)
);
}
#[test]
fn default_error_correction_is_low() {
assert_eq!(QrErrorCorrection::default(), QrErrorCorrection::Low);
}
}