use wasm_bindgen::prelude::*;
use crate::{EcLevel, ModuleShape, QRCode};
#[wasm_bindgen]
#[derive(Debug)]
pub struct WasmQRCode {
inner: QRCode,
}
#[wasm_bindgen]
impl WasmQRCode {
#[wasm_bindgen(constructor)]
pub fn new(data: &str) -> Self {
Self {
inner: QRCode::from_string(data.to_string()),
}
}
#[wasm_bindgen(js_name = "setEcLevel")]
pub fn set_ec_level(&mut self, level: &str) {
let ec = match level {
"L" => EcLevel::L,
"Q" => EcLevel::Q,
"H" => EcLevel::H,
_ => EcLevel::M,
};
self.inner.ec_level = ec;
}
#[wasm_bindgen(js_name = "setShape")]
pub fn set_shape(&mut self, shape: &str) {
let s = match shape {
"rounded" => ModuleShape::RoundedSquare,
"circle" => ModuleShape::Circle,
"diamond" => ModuleShape::Diamond,
_ => ModuleShape::Square,
};
self.inner.shape = s;
}
#[wasm_bindgen(js_name = "toSvg")]
pub fn to_svg(&self, width: u32) -> String {
self.inner.to_svg(width)
}
#[wasm_bindgen(js_name = "toPngBytes")]
pub fn to_png_bytes(&self, width: u32) -> Vec<u8> {
self.inner.to_png_bytes(width).unwrap_or_default()
}
#[wasm_bindgen(js_name = "toJpg")]
pub fn to_jpg(&self, width: u32) -> Vec<u8> {
self.inner.to_jpg(width).unwrap_or_default()
}
}