Skip to main content

onepassword_sys/
util.rs

1use preinterpret::preinterpret;
2
3#[cfg_attr(windows, link(name = "op_uniffi_core", kind = "raw-dylib"))]
4#[cfg_attr(not(windows), link(name = "op_uniffi_core"))]
5unsafe extern "C" {
6    #[link_name = "ffi_op_uniffi_core_uniffi_contract_version"]
7    safe fn uniffi_contract_version() -> u32;
8}
9
10macro_rules! rust_call {
11    ($fn:ident -> $conv:ty, $($val:expr),*) => {{
12        use $crate::errors::{CallStatus, CallStatusCode, check_call_status};
13
14        let mut call_status = CallStatus {
15            code: CallStatusCode::Success,
16            error_buf: RustBuffer::default(),
17        };
18        let result = $fn($($val),*, &mut call_status);
19        check_call_status::<$conv>(call_status).and(Ok(result))
20    }};
21
22    ($fn:ident, $($val:expr),*) => {{
23        use $crate::errors::{CallStatus, CallStatusCode, NoConverter, check_call_status};
24
25        let mut call_status = CallStatus {
26            code: CallStatusCode::Success,
27            error_buf: RustBuffer::default(),
28        };
29        let result = $fn($($val),*, &mut call_status);
30        check_call_status::<NoConverter>(call_status).and(Ok(result))
31    }}
32}
33
34pub(crate) use rust_call;
35
36macro_rules! link_checksum_fns {
37    ($($fn:ident: $checksum:literal),+) => {preinterpret! {
38        #[cfg_attr(windows, link(name = "op_uniffi_core", kind = "raw-dylib"))]
39        #[cfg_attr(not(windows), link(name = "op_uniffi_core"))]
40        unsafe extern "C" {
41            $(
42                #[link_name = concat!("uniffi_op_uniffi_core_checksum_func_", stringify!($fn))]
43                safe fn [!ident_snake! uniffi_checksum_ $fn]() -> u16;
44            )+
45        }
46
47        static CHECKSUMS: &[(extern "C" fn() -> u16, u16)] = &[
48            $(
49                ([!ident_snake! uniffi_checksum_ $fn], $checksum)
50            ),+
51        ];
52    }};
53}
54
55link_checksum_fns! {
56    init_client: 45066,
57    release_client: 57155,
58    invoke: 29143,
59    invoke_sync: 49373
60}
61
62pub fn validate_checksums() {
63    for (checksum_fn, expected) in CHECKSUMS {
64        assert_eq!(checksum_fn(), *expected);
65    }
66}
67
68pub fn version() -> u32 {
69    uniffi_contract_version()
70}