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
#![allow(unused_imports)]

#[macro_use]
extern crate failure;

#[macro_use]
extern crate shippai_derive;

pub use shippai_derive::*;

#[macro_export]
macro_rules! shippai_export {
    () => {
        pub struct ShippaiError {
            pub error: ::failure::Error,
        }

        impl From<::failure::Error> for ShippaiError {
            fn from(e: ::failure::Error) -> ShippaiError {
                ShippaiError { error: e }
            }
        }

        impl From<ShippaiError> for ::failure::Error {
            fn from(e: ShippaiError) -> ::failure::Error {
                e.error
            }
        }

        #[no_mangle]
        pub unsafe extern "C" fn shippai_get_display(
            t: *mut ShippaiError,
        ) -> *const ::std::os::raw::c_char {
            use std::ffi::CString;
            CString::new(format!("{}", (*t).error)).unwrap().into_raw()
        }

        #[no_mangle]
        pub unsafe extern "C" fn shippai_get_debug(
            t: *mut ShippaiError,
        ) -> *const ::std::os::raw::c_char {
            use std::ffi::CString;
            CString::new(format!("{:?}", (*t).error))
                .unwrap()
                .into_raw()
        }

        #[no_mangle]
        pub unsafe extern "C" fn shippai_free_failure(t: *mut ShippaiError) {
            let _ = Box::from_raw(t);
        }

        #[no_mangle]
        pub unsafe extern "C" fn shippai_free_str(t: *mut ::std::os::raw::c_char) {
            use std::ffi::CString;
            CString::from_raw(t);
        }
    };
}