use std::ops::Index;
use std::string::FromUtf8Error;
use thiserror::Error;
use crate::util::Point;
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[error("Error decoding QR Code: {}", msg)]
pub struct QRError {
pub msg: String,
}
impl From<FromUtf8Error> for QRError {
fn from(error: FromUtf8Error) -> Self {
QRError {
msg: format!(
"Unable to convert result to UTF-8, raw bytes: {:?}",
error.into_bytes()
),
}
}
}
#[derive(Debug)]
pub struct QRData {
pub data: Vec<u8>,
pub version: u32,
pub side: u32,
}
impl QRData {
pub fn new(data: Vec<u8>, version: u32) -> QRData {
QRData {
data,
version,
side: 4 * version + 17,
}
}
}
impl Index<[u32; 2]> for QRData {
type Output = u8;
fn index(&self, index: [u32; 2]) -> &u8 {
let pixel = self.data[index[1] as usize * self.side as usize + index[0] as usize];
if pixel == 0 {
&1
} else {
&0
}
}
}
#[derive(Debug)]
pub struct QRLocation {
pub top_left: Point,
pub top_right: Point,
pub bottom_left: Point,
pub module_size: f64,
pub version: u32,
}
#[derive(Debug, PartialEq, Eq)]
pub struct QRInfo {
pub version: u32,
pub ec_level: ECLevel,
pub total_data: u32,
pub errors: u32,
}
#[derive(Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum ECLevel {
LOW,
MEDIUM,
QUARTILE,
HIGH,
}