use std::fmt;
use crate::qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef};
use super::ByteMatrix;
#[derive(Debug, Clone)]
pub struct QRCode {
mode: Option<Mode>,
ecLevel: Option<ErrorCorrectionLevel>,
version: Option<VersionRef>,
maskPattern: i32,
matrix: Option<ByteMatrix>,
}
impl QRCode {
pub const NUM_MASK_PATTERNS: i32 = 8;
pub fn new() -> Self {
Self {
mode: None,
ecLevel: None,
version: None,
maskPattern: -1,
matrix: None,
}
}
pub fn getMode(&self) -> &Option<Mode> {
&self.mode
}
pub fn getECLevel(&self) -> &Option<ErrorCorrectionLevel> {
&self.ecLevel
}
pub fn getVersion(&self) -> &Option<&'static Version> {
&self.version
}
pub fn getMaskPattern(&self) -> i32 {
self.maskPattern
}
pub fn getMatrix(&self) -> &Option<ByteMatrix> {
&self.matrix
}
pub fn setMode(&mut self, value: Mode) {
self.mode = Some(value);
}
pub fn setECLevel(&mut self, value: ErrorCorrectionLevel) {
self.ecLevel = Some(value);
}
pub fn setVersion(&mut self, version: &'static Version) {
self.version = Some(version);
}
pub fn setMaskPattern(&mut self, value: i32) {
self.maskPattern = value;
}
pub fn setMatrix(&mut self, value: ByteMatrix) {
self.matrix = Some(value);
}
pub fn isValidMaskPattern(maskPattern: i32) -> bool {
(0..Self::NUM_MASK_PATTERNS).contains(&maskPattern)
}
}
impl fmt::Display for QRCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::with_capacity(200);
result.push_str("<<\n");
result.push_str(" mode: ");
if self.mode.is_some() {
result.push_str(&format!("{:?}", self.mode.as_ref().ok_or(fmt::Error)?));
} else {
result.push_str("null");
}
result.push_str("\n ecLevel: ");
if self.ecLevel.is_some() {
result.push_str(&format!("{:?}", self.ecLevel.as_ref().ok_or(fmt::Error)?));
} else {
result.push_str("null");
}
result.push_str("\n version: ");
if self.version.is_some() {
result.push_str(&format!("{}", self.version.as_ref().ok_or(fmt::Error)?));
} else {
result.push_str("null");
}
result.push_str("\n maskPattern: ");
result.push_str(&format!("{}", self.maskPattern));
if self.matrix.is_none() {
result.push_str("\n matrix: null\n");
} else {
result.push_str("\n matrix:\n");
if self.matrix.is_some() {
result.push_str(&format!("{}", self.matrix.as_ref().ok_or(fmt::Error)?));
} else {
result.push_str("null");
}
}
result.push_str(">>\n");
write!(f, "{result}")
}
}
impl Default for QRCode {
fn default() -> Self {
Self::new()
}
}