style: cargo fmt + clippy fix 全项目格式化

This commit is contained in:
2026-06-16 23:58:15 +08:00
parent 9201cd820c
commit 30af4ff607
16 changed files with 1298 additions and 165 deletions
+10 -5
View File
@@ -61,8 +61,8 @@ fn main() -> anyhow::Result<()> {
margin: args.margin,
};
let qr = QrCode::encode(&args.content, config)
.map_err(|e| anyhow::anyhow!("编码失败: {}", e))?;
let qr =
QrCode::encode(&args.content, config).map_err(|e| anyhow::anyhow!("编码失败: {}", e))?;
match args.output {
Some(path) => {
@@ -76,14 +76,19 @@ fn main() -> anyhow::Result<()> {
"png" => {
let bytes = qr.to_png_bytes(args.size);
std::fs::write(&path, bytes)?;
println!("已生成: {} (版本 {}, {}×{} 模块, {} 级纠错)",
path, qr.version.0, qr.size(), qr.size(),
println!(
"已生成: {} (版本 {}, {}×{} 模块, {} 级纠错)",
path,
qr.version.0,
qr.size(),
qr.size(),
match qr.level {
EcLevel::L => "L",
EcLevel::M => "M",
EcLevel::Q => "Q",
EcLevel::H => "H",
});
}
);
}
"svg" => {
let svg = qr.to_svg();
+18 -8
View File
@@ -111,8 +111,7 @@ mod tests {
fn test_mul_commutative() {
for a in 0..=255u8 {
for b in (a..=255u8).step_by(17) {
assert_eq!(mul(a, b), mul(b, a),
"交换律失败: {:02X} * {:02X}", a, b);
assert_eq!(mul(a, b), mul(b, a), "交换律失败: {:02X} * {:02X}", a, b);
}
}
}
@@ -121,8 +120,14 @@ mod tests {
fn test_mul_associative() {
let cases = [(3, 5, 7), (0xFF, 2, 4), (1, 1, 1), (0x80, 0x40, 0x20)];
for (a, b, c) in cases {
assert_eq!(mul(mul(a, b), c), mul(a, mul(b, c)),
"结合律失败: {:02X} * {:02X} * {:02X}", a, b, c);
assert_eq!(
mul(mul(a, b), c),
mul(a, mul(b, c)),
"结合律失败: {:02X} * {:02X} * {:02X}",
a,
b,
c
);
}
}
@@ -130,8 +135,7 @@ mod tests {
fn test_div_inverse() {
for a in 1..=255u8 {
let inv = div(1, a);
assert_eq!(mul(a, inv), 1,
"逆元失败: {:02X} * {:02X} != 1", a, inv);
assert_eq!(mul(a, inv), 1, "逆元失败: {:02X} * {:02X} != 1", a, inv);
}
}
@@ -140,8 +144,14 @@ mod tests {
for a in 1..=255u8 {
for b in (1..=255u8).step_by(17) {
let q = div(a, b);
assert_eq!(mul(q, b), a,
"除乘一致性失败: {:02X} / {:02X} = {:02X}", a, q, b);
assert_eq!(
mul(q, b),
a,
"除乘一致性失败: {:02X} / {:02X} = {:02X}",
a,
q,
b
);
}
}
}
+1 -4
View File
@@ -64,10 +64,7 @@ pub fn interleave(blocks: &[Vec<u8>], ec_count: u8) -> Vec<u8> {
}
// 对每个块计算 EC
let ec_blocks: Vec<Vec<u8>> = blocks
.iter()
.map(|b| compute_ec(b, ec_count))
.collect();
let ec_blocks: Vec<Vec<u8>> = blocks.iter().map(|b| compute_ec(b, ec_count)).collect();
// EC 码字交错
for i in 0..ec_count as usize {
+1 -3
View File
@@ -1,6 +1,4 @@
use crate::encoder::mode::{
encode_alphanumeric, encode_byte, encode_kanji, encode_numeric, Mode,
};
use crate::encoder::mode::{encode_alphanumeric, encode_byte, encode_kanji, encode_numeric, Mode};
use crate::encoder::segment::segment_text;
use crate::version::{get_data_capacity, EcLevel, Version};
+1 -1
View File
@@ -1,3 +1,3 @@
pub mod bitstream;
pub mod mode;
pub mod segment;
pub mod bitstream;
+32 -7
View File
@@ -22,16 +22,38 @@ impl Mode {
pub fn count_bits(self, version: u8) -> u8 {
match self {
Mode::Numeric => {
if version <= 9 { 10 } else if version <= 26 { 12 } else { 14 }
if version <= 9 {
10
} else if version <= 26 {
12
} else {
14
}
}
Mode::Alphanumeric => {
if version <= 9 { 9 } else if version <= 26 { 11 } else { 13 }
if version <= 9 {
9
} else if version <= 26 {
11
} else {
13
}
}
Mode::Byte => {
if version <= 9 { 8 } else { 16 }
if version <= 9 {
8
} else {
16
}
}
Mode::Kanji => {
if version <= 9 { 8 } else if version <= 26 { 10 } else { 12 }
if version <= 9 {
8
} else if version <= 26 {
10
} else {
12
}
}
}
}
@@ -40,7 +62,8 @@ impl Mode {
/// 数字模式编码: 每 3 位数字 → 10 bit
pub fn encode_numeric(input: &str) -> Vec<bool> {
let mut bits = Vec::new();
let chars: Vec<u8> = input.chars()
let chars: Vec<u8> = input
.chars()
.filter_map(|c| c.to_digit(10).map(|d| d as u8))
.collect();
@@ -65,9 +88,11 @@ const ALPHANUMERIC_CHARS: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./
/// 字母数字模式编码: 每 2 个字符 → 11 bit
pub fn encode_alphanumeric(input: &str) -> Vec<bool> {
let values: Vec<u8> = input.chars()
let values: Vec<u8> = input
.chars()
.filter_map(|c| {
ALPHANUMERIC_CHARS.iter()
ALPHANUMERIC_CHARS
.iter()
.position(|&x| x == c as u8)
.map(|i| i as u8)
})
+1 -1
View File
@@ -1,6 +1,6 @@
pub mod ecc;
pub mod encoder;
pub mod matrix;
pub mod qr;
pub mod render;
pub mod version;
pub mod qr;
+5 -1
View File
@@ -10,7 +10,11 @@ impl Matrix {
pub fn new(size: u8) -> Self {
let modules = vec![vec![false; size as usize]; size as usize];
let reserved = vec![vec![false; size as usize]; size as usize];
Matrix { size, modules, reserved }
Matrix {
size,
modules,
reserved,
}
}
pub fn get(&self, x: u8, y: u8) -> bool {
+1 -1
View File
@@ -1,4 +1,4 @@
pub mod grid;
pub mod mask;
pub mod patterns;
pub mod placement;
pub mod mask;
+51 -13
View File
@@ -78,11 +78,17 @@ fn is_near_finder(x: u8, y: u8, size: u8) -> bool {
let x = x as i16;
let y = y as i16;
// 左上角
if x - 2 < 7 && y - 2 < 7 { return true; }
if x - 2 < 7 && y - 2 < 7 {
return true;
}
// 右上角
if x + 2 >= s - 7 && y - 2 < 7 { return true; }
if x + 2 >= s - 7 && y - 2 < 7 {
return true;
}
// 左下角
if x - 2 < 7 && y + 2 >= s - 7 { return true; }
if x - 2 < 7 && y + 2 >= s - 7 {
return true;
}
false
}
@@ -92,13 +98,21 @@ pub fn reserve_format_areas(matrix: &mut Matrix) {
// 定位图案旁的格式信息条
for i in 0..9u8 {
// 左上水平
if i != 6 { matrix.reserve(i, 8); }
if i != 6 {
matrix.reserve(i, 8);
}
// 左上垂直
if i != 6 { matrix.reserve(8, i); }
if i != 6 {
matrix.reserve(8, i);
}
// 右上垂直
if i + size - 8 < size { matrix.reserve(size - 1 - i, 8); }
if i + size - 8 < size {
matrix.reserve(size - 1 - i, 8);
}
// 左下水平
if i + size - 8 < size { matrix.reserve(8, size - 1 - i); }
if i + size - 8 < size {
matrix.reserve(8, size - 1 - i);
}
}
// 暗模块位置
if size > 8 {
@@ -144,16 +158,40 @@ pub fn place_format_info(matrix: &mut Matrix, format: u16) {
// 第一组:左上角定位图案旁
let coords1: [(u8, u8); 15] = [
(0, 8), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (7, 8), (8, 8),
(8, 7), (8, 5), (8, 4), (8, 3), (8, 2), (8, 1), (8, 0),
(0, 8),
(1, 8),
(2, 8),
(3, 8),
(4, 8),
(5, 8),
(7, 8),
(8, 8),
(8, 7),
(8, 5),
(8, 4),
(8, 3),
(8, 2),
(8, 1),
(8, 0),
];
// 第二组:右下角,拆分为右上角旁 + 左下角旁
let coords2: [(u8, u8); 15] = [
(size - 1, 8), (size - 2, 8), (size - 3, 8), (size - 4, 8),
(size - 5, 8), (size - 6, 8), (size - 7, 8), (size - 8, 8),
(8, size - 7), (8, size - 6), (8, size - 5), (8, size - 4),
(8, size - 3), (8, size - 2), (8, size - 1),
(size - 1, 8),
(size - 2, 8),
(size - 3, 8),
(size - 4, 8),
(size - 5, 8),
(size - 6, 8),
(size - 7, 8),
(size - 8, 8),
(8, size - 7),
(8, size - 6),
(8, size - 5),
(8, size - 4),
(8, size - 3),
(8, size - 2),
(8, size - 1),
];
for i in 0..15 {
+8 -11
View File
@@ -1,15 +1,15 @@
use crate::version::{Version, EcLevel, get_data_capacity};
use crate::encoder::bitstream::build_codewords;
use crate::encoder::segment::{segment_text, segment_bit_length};
use crate::ecc::reed_solomon;
use crate::encoder::bitstream::build_codewords;
use crate::encoder::segment::{segment_bit_length, segment_text};
use crate::matrix::grid::Matrix;
use crate::matrix::mask::best_mask;
use crate::matrix::patterns::{
place_finder_patterns, place_timing_patterns, place_alignment_patterns,
reserve_format_areas, reserve_version_areas,
encode_format_info, encode_version_info, place_format_info, place_version_info,
encode_format_info, encode_version_info, place_alignment_patterns, place_finder_patterns,
place_format_info, place_timing_patterns, place_version_info, reserve_format_areas,
reserve_version_areas,
};
use crate::matrix::placement::place_data;
use crate::matrix::mask::best_mask;
use crate::version::{get_data_capacity, EcLevel, Version};
/// 版本选择模式
#[derive(Debug, Clone)]
@@ -61,10 +61,7 @@ impl QrCode {
let mut selected = None;
for v in 1..=40 {
let ver = Version(v);
let total_bits: u16 = segments
.iter()
.map(|s| segment_bit_length(s, v))
.sum();
let total_bits: u16 = segments.iter().map(|s| segment_bit_length(s, v)).sum();
let cap = get_data_capacity(ver, config.level) as u32 * 8;
if cap >= total_bits as u32 {
selected = Some(ver);
+1 -1
View File
@@ -1,3 +1,3 @@
pub mod ascii;
pub mod png;
pub mod svg;
pub mod ascii;
+1 -4
View File
@@ -34,10 +34,7 @@ pub fn render_png(qr: &QrCode, module_size: u8) -> Vec<u8> {
}
let mut buf = Vec::new();
img.write_to(
&mut std::io::Cursor::new(&mut buf),
image::ImageFormat::Png,
)
img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
.expect("PNG 编码失败");
buf
}
+1110 -54
View File
File diff suppressed because it is too large Load Diff
+8 -2
View File
@@ -12,7 +12,10 @@ fn test_encode_simple_text() {
#[test]
fn test_all_levels() {
for level in [EcLevel::L, EcLevel::M, EcLevel::Q, EcLevel::H] {
let config = QrConfig { level, ..Default::default() };
let config = QrConfig {
level,
..Default::default()
};
let qr = QrCode::encode("TEST", config).unwrap();
assert!(qr.size() >= 21);
assert!(qr.size() <= 177);
@@ -113,7 +116,10 @@ fn test_margin_is_included_in_dimensions() {
let ascii = qr.to_ascii(false);
let first_line = ascii.lines().next().unwrap();
let chars_per_module = 2; // ██ 是两个字符
assert_eq!(first_line.chars().count(), expected_total as usize * chars_per_module);
assert_eq!(
first_line.chars().count(),
expected_total as usize * chars_per_module
);
}
#[test]