-
Notifications
You must be signed in to change notification settings - Fork 318
/
lib.rs
55 lines (47 loc) · 1.3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use candid::{CandidType, Deserialize};
use std::include_bytes;
mod core;
const IMAGE_SIZE_IN_PIXELS: usize = 1024;
const LOGO_TRANSPARENT: &[u8] = include_bytes!("../assets/logo_transparent.png");
const LOGO_WHITE: &[u8] = include_bytes!("../assets/logo_white.png");
#[derive(CandidType, Deserialize)]
struct Options {
add_logo: bool,
add_gradient: bool,
add_transparency: Option<bool>,
}
#[derive(CandidType, Deserialize)]
struct QrError {
message: String,
}
#[derive(CandidType, Deserialize)]
enum QrResult {
Image(Vec<u8>),
Err(QrError),
}
fn qrcode_impl(input: String, options: Options) -> QrResult {
let logo = if options.add_transparency == Some(true) {
LOGO_TRANSPARENT
} else {
LOGO_WHITE
};
let result = match core::generate(input, options, logo, IMAGE_SIZE_IN_PIXELS) {
Ok(blob) => QrResult::Image(blob),
Err(err) => QrResult::Err(QrError {
message: err.to_string(),
}),
};
ic_cdk::println!(
"Executed instructions: {}",
ic_cdk::api::performance_counter(0)
);
result
}
#[ic_cdk::update]
fn qrcode(input: String, options: Options) -> QrResult {
qrcode_impl(input, options)
}
#[ic_cdk::query]
fn qrcode_query(input: String, options: Options) -> QrResult {
qrcode_impl(input, options)
}