Run cargo fmt

This commit is contained in:
kennytm 2020-02-11 16:32:29 +08:00
parent 7f9c7688b9
commit a5df5f4784

View file

@ -1,26 +1,31 @@
//! UTF-8 rendering, with 2 pixels per symbol. //! UTF-8 rendering, with 2 pixels per symbol.
use crate::render::{Canvas as RenderCanvas, Pixel, Color}; use crate::render::{Canvas as RenderCanvas, Color, Pixel};
const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"]; const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"];
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub enum Dense1x2 { pub enum Dense1x2 {
Dark, Light Dark,
Light,
} }
impl Pixel for Dense1x2 { impl Pixel for Dense1x2 {
type Image = String; type Image = String;
type Canvas = Canvas1x2; type Canvas = Canvas1x2;
fn default_color(color: Color) -> Dense1x2 { color.select(Dense1x2::Dark, Dense1x2::Light) } fn default_color(color: Color) -> Dense1x2 {
fn default_unit_size() -> (u32, u32) { (1, 1) } color.select(Dense1x2::Dark, Dense1x2::Light)
}
fn default_unit_size() -> (u32, u32) {
(1, 1)
}
} }
impl Dense1x2 { impl Dense1x2 {
fn value(&self) -> u8 { fn value(&self) -> u8 {
match self { match self {
Dense1x2::Dark => {1} Dense1x2::Dark => 1,
Dense1x2::Light => {0} Dense1x2::Light => 0,
} }
} }
fn parse_2_bits(sym: &u8) -> &'static str { fn parse_2_bits(sym: &u8) -> &'static str {
@ -31,22 +36,16 @@ impl Dense1x2 {
pub struct Canvas1x2 { pub struct Canvas1x2 {
canvas: Vec<u8>, canvas: Vec<u8>,
width: u32, width: u32,
dark_pixel: u8 dark_pixel: u8,
} }
impl RenderCanvas for Canvas1x2 { impl RenderCanvas for Canvas1x2 {
type Pixel = Dense1x2; type Pixel = Dense1x2;
type Image = String; type Image = String;
fn new(width: u32, height: u32, dark_pixel: Dense1x2, light_pixel: Dense1x2) -> Self { fn new(width: u32, height: u32, dark_pixel: Dense1x2, light_pixel: Dense1x2) -> Self {
let a = vec![light_pixel.value(); (width * height) as usize]; let a = vec![light_pixel.value(); (width * height) as usize];
Canvas1x2 { Canvas1x2 { width, canvas: a, dark_pixel: dark_pixel.value() }
width: width,
canvas: a,
dark_pixel: dark_pixel.value()
}
} }
fn draw_dark_pixel(&mut self, x: u32, y: u32) { fn draw_dark_pixel(&mut self, x: u32, y: u32) {
@ -60,7 +59,7 @@ impl RenderCanvas for Canvas1x2 {
.collect::<Vec<&[u8]>>() .collect::<Vec<&[u8]>>()
// And then glueing every 2 lines. // And then glueing every 2 lines.
.chunks(2) .chunks(2)
.map(|rows| .map(|rows| {
{ {
// Then zipping those 2 lines together into a single 2-bit number list. // Then zipping those 2 lines together into a single 2-bit number list.
if rows.len() == 2 { if rows.len() == 2 {
@ -74,7 +73,7 @@ impl RenderCanvas for Canvas1x2 {
.map(Dense1x2::parse_2_bits) .map(Dense1x2::parse_2_bits)
.collect::<Vec<&str>>() .collect::<Vec<&str>>()
.concat() .concat()
) })
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n") .join("\n")
} }
@ -95,38 +94,40 @@ fn test_render_to_utf8_string() {
#[test] #[test]
fn integration_render_utf8_1x2() { fn integration_render_utf8_1x2() {
use crate::{QrCode, Version, EcLevel};
use crate::render::unicode::Dense1x2; use crate::render::unicode::Dense1x2;
use crate::{EcLevel, QrCode, Version};
let code = QrCode::with_version(b"09876542", Version::Micro(2), EcLevel::L).unwrap(); let code = QrCode::with_version(b"09876542", Version::Micro(2), EcLevel::L).unwrap();
let image = code.render::<Dense1x2>() let image = code.render::<Dense1x2>().module_dimensions(1, 1).build();
.module_dimensions(1, 1) assert_eq!(
.build(); image,
assert_eq!(image, String::new()
" \n".to_owned() + + " \n"
" █▀▀▀▀▀█ ▀ █ ▀ \n" + + " █▀▀▀▀▀█ ▀ █ ▀ \n"
" █ ███ █ ▀ █ \n" + + " █ ███ █ ▀ █ \n"
" █ ▀▀▀ █ ▀█ █ \n" + + " █ ▀▀▀ █ ▀█ █ \n"
" ▀▀▀▀▀▀▀ ▄▀▀ █ \n" + + " ▀▀▀▀▀▀▀ ▄▀▀ █ \n"
" ▀█ ▀▀▀▀▀██▀▀▄ \n" + + " ▀█ ▀▀▀▀▀██▀▀▄ \n"
" ▀███▄ ▀▀ █ ██ \n" + + " ▀███▄ ▀▀ █ ██ \n"
" ▀▀▀ ▀ ▀▀ ▀ ▀ \n" + + " ▀▀▀ ▀ ▀▀ ▀ ▀ \n"
" ") + " "
)
} }
#[test] #[test]
fn integration_render_utf8_1x2_inverted() { fn integration_render_utf8_1x2_inverted() {
use crate::{QrCode, Version, EcLevel};
use crate::render::unicode::Dense1x2; use crate::render::unicode::Dense1x2;
use crate::{EcLevel, QrCode, Version};
let code = QrCode::with_version(b"12345678", Version::Micro(2), EcLevel::L).unwrap(); let code = QrCode::with_version(b"12345678", Version::Micro(2), EcLevel::L).unwrap();
let image = code.render::<Dense1x2>() let image = code
.render::<Dense1x2>()
.dark_color(Dense1x2::Light) .dark_color(Dense1x2::Light)
.light_color(Dense1x2::Dark) .light_color(Dense1x2::Dark)
.module_dimensions(1, 1) .module_dimensions(1, 1)
.build(); .build();
assert_eq!(image, assert_eq!(
image,
"█████████████████\n\ "█████████████████\n\
\n\ \n\
\n\ \n\
@ -135,6 +136,6 @@ fn integration_render_utf8_1x2_inverted() {
\n\ \n\
\n\ \n\
\n\ \n\
"); "
);
} }