Skip to main content

onepassword_sys/
lib.rs

1use crate::{
2    errors::{CallStatus, ErrorTypeConverter, FfiResult},
3    util::rust_call,
4};
5
6mod buffer;
7mod errors;
8mod futures;
9mod util;
10
11pub use {buffer::RustBuffer, errors::Error, util::validate_checksums, util::version};
12
13#[cfg_attr(windows, link(name = "op_uniffi_core", kind = "raw-dylib"))]
14#[cfg_attr(not(windows), link(name = "op_uniffi_core"))]
15unsafe extern "C" {
16    #[link_name = "uniffi_op_uniffi_core_fn_func_init_client"]
17    unsafe fn uniffi_init_client(buffer: RustBuffer) -> futures::FfiFutureHandle<RustBuffer>;
18    #[link_name = "uniffi_op_uniffi_core_fn_func_release_client"]
19    unsafe fn uniffi_release_client(buffer: RustBuffer, status: *mut CallStatus);
20}
21
22#[cfg(feature = "sync")]
23#[cfg_attr(windows, link(name = "op_uniffi_core", kind = "raw-dylib"))]
24#[cfg_attr(not(windows), link(name = "op_uniffi_core"))]
25unsafe extern "C" {
26    #[link_name = "uniffi_op_uniffi_core_fn_func_invoke_sync"]
27    unsafe fn uniffi_invoke_sync(buffer: RustBuffer, status: *mut CallStatus) -> RustBuffer;
28}
29
30#[cfg(feature = "async")]
31#[cfg_attr(windows, link(name = "op_uniffi_core", kind = "raw-dylib"))]
32#[cfg_attr(not(windows), link(name = "op_uniffi_core"))]
33unsafe extern "C" {
34    #[link_name = "uniffi_op_uniffi_core_fn_func_invoke"]
35    unsafe fn uniffi_invoke(buffer: RustBuffer) -> futures::FfiFutureHandle<RustBuffer>;
36}
37
38#[cfg(feature = "async")]
39pub async fn invoke(payload: &str) -> Result<RustBuffer, Error> {
40    let buffer: RustBuffer = payload.into();
41
42    let result = unsafe { uniffi_invoke(buffer) }
43        .into_future::<ErrorTypeConverter>()
44        .await?;
45
46    Ok(result)
47}
48
49#[cfg(feature = "sync")]
50pub fn invoke_sync(payload: &str) -> Result<RustBuffer, Error> {
51    let buffer: RustBuffer = payload.into();
52
53    unsafe { rust_call!(uniffi_invoke_sync -> ErrorTypeConverter, buffer) }
54}
55
56#[cfg(feature = "async")]
57pub async fn get_client_id_buffer(client_config: &str) -> FfiResult<RustBuffer> {
58    let buffer: RustBuffer = client_config.into();
59
60    unsafe { uniffi_init_client(buffer) }
61        .into_future::<ErrorTypeConverter>()
62        .await
63}
64
65#[cfg(feature = "sync")]
66pub fn get_client_id_buffer_sync(client_config: &str) -> FfiResult<RustBuffer> {
67    let buffer: RustBuffer = client_config.into();
68
69    pollster::block_on(unsafe { uniffi_init_client(buffer) }.into_future::<ErrorTypeConverter>())
70}
71
72pub fn free_client(client_id: &str) {
73    let buffer = RustBuffer::from(client_id);
74    match unsafe { rust_call!(uniffi_release_client, buffer) } {
75        Ok(()) => {}
76    }
77}