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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
pub(crate) mod aux;
pub(crate) mod emit;
pub(crate) mod model;
pub(crate) mod parse;

pub use model::{BrCode, Info, Label, MerchantInfo, Template};
pub use parse::Data;
pub use aux::crc16_ccitt;

pub fn from_str(code: &str) -> Vec<(usize, parse::Data)> {
    parse::parse(code, 99)
}

pub fn to_string(code: &[(usize, parse::Data)]) -> String {
    emit::emit(&code)
}

pub fn brcode_to_string(code: BrCode) -> String {
    code.encode()
}

pub fn str_to_brcode(code: &str) -> BrCode {
    let codes = parse::parse(code, 99);
    BrCode::from(codes)
}

// FFI
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::c_char;
use std::str;

fn chars_to_string(pointer: *const c_char) -> String {
    let slice = unsafe { CStr::from_ptr(pointer).to_bytes() };
    str::from_utf8(slice).unwrap().to_string()
}

fn to_c_char(s: &str) -> *const c_char {
    let cs = CString::new(s.as_bytes()).unwrap();
    let ptr = cs.as_ptr();
    mem::forget(cs);
    ptr
}

// Edn
#[no_mangle]
pub extern "C" fn edn_from_brcode(edn: *const c_char) -> *const c_char {
    let edn_str = chars_to_string(edn);
    let brcode = str_to_brcode(&edn_str);
    to_c_char(&edn_rs::to_string(brcode))
}

#[no_mangle]
pub extern "C" fn edn_to_brcode(edn: *const c_char) -> *const c_char {
    let edn_str = chars_to_string(edn);
    let brcode: BrCode = edn_rs::from_str(&edn_str).unwrap();

    to_c_char(&brcode_to_string(brcode))
}

// Json
#[no_mangle]
pub extern "C" fn json_from_brcode(json: *const c_char) -> *const c_char {
    let json_str = chars_to_string(json);
    let brcode = str_to_brcode(&json_str);
    to_c_char(&serde_json::to_string(&brcode).unwrap_or_else(|_| "error".to_string()))
}

#[no_mangle]
pub extern "C" fn json_to_brcode(edn: *const c_char) -> *const c_char {
    let edn_str = chars_to_string(edn);
    let brcode: BrCode = serde_json::from_str(&edn_str).unwrap();

    to_c_char(&brcode_to_string(brcode))
}