use std::str::{ self, Utf8Error };
use std::cmp::{ min, max };
use std::hash::{ Hash, Hasher };
use quirc_sys::{ quirc_data, QUIRC_MAX_PAYLOAD };
use quirc_sys::QuircEccLevel::*;
use quirc_sys::QuircDataType::*;
#[derive(Debug, Clone, Copy)]
pub struct Info(quirc_data);
impl Info {
#[doc(hidden)]
pub fn from_raw(raw: quirc_data) -> Self {
Info(raw)
}
#[cfg_attr(feature = "cargo-clippy", allow(cast_possible_truncation, cast_possible_wrap))]
pub fn version(&self) -> u8 {
max(1, min(self.0.version, 40)) as _
}
#[cfg_attr(feature = "cargo-clippy", allow(cast_possible_truncation, cast_possible_wrap))]
pub fn mask_id(&self) -> u8 {
max(0, min(self.0.mask, 7)) as _
}
#[cfg_attr(feature = "cargo-clippy", allow(cast_possible_truncation, cast_possible_wrap))]
pub fn eci(&self) -> u8 {
max(0, min(self.0.eci, 30)) as _
}
pub fn ecc_level(&self) -> EccLevel {
let ecc = self.0.ecc_level;
if ecc == QUIRC_ECC_LEVEL_L as _ {
EccLevel::L
} else if ecc == QUIRC_ECC_LEVEL_M as _ {
EccLevel::M
} else if ecc == QUIRC_ECC_LEVEL_Q as _ {
EccLevel::Q
} else if ecc == QUIRC_ECC_LEVEL_H as _ {
EccLevel::H
} else {
EccLevel::L }
}
pub fn data_type(&self) -> DataType {
let dtype = self.0.data_type;
if dtype == QUIRC_DATA_TYPE_NUMERIC as _ {
DataType::Numeric
} else if dtype == QUIRC_DATA_TYPE_ALPHA as _ {
DataType::Alphanumeric
} else if dtype == QUIRC_DATA_TYPE_BYTE as _ {
DataType::Byte
} else if dtype == QUIRC_DATA_TYPE_KANJI as _ {
DataType::Kanji
} else {
DataType::Byte }
}
pub fn payload(&self) -> &[u8] {
let len = max(0, min(self.0.payload_len as _, QUIRC_MAX_PAYLOAD));
&self.0.payload[..len]
}
pub fn as_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(self.payload())
}
}
impl PartialEq<Info> for Info {
fn eq(&self, other: &Info) -> bool {
self.version() == other.version() &&
self.mask_id() == other.mask_id() &&
self.eci() == other.eci() &&
self.ecc_level() == other.ecc_level() &&
self.data_type() == other.data_type() &&
self.payload() == other.payload()
}
}
impl Hash for Info {
fn hash<H: Hasher>(&self, state: &mut H) {
self.version().hash(state);
self.mask_id().hash(state);
self.eci().hash(state);
self.ecc_level().hash(state);
self.data_type().hash(state);
self.payload().hash(state);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EccLevel {
L = QUIRC_ECC_LEVEL_L as _,
M = QUIRC_ECC_LEVEL_M as _,
Q = QUIRC_ECC_LEVEL_Q as _,
H = QUIRC_ECC_LEVEL_H as _,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DataType {
Numeric = QUIRC_DATA_TYPE_NUMERIC as _,
Alphanumeric = QUIRC_DATA_TYPE_ALPHA as _,
Byte = QUIRC_DATA_TYPE_BYTE as _,
Kanji = QUIRC_DATA_TYPE_KANJI as _,
}