|
| 1 | +use std::{ffi::c_void, os::raw::c_char, sync::Arc}; |
| 2 | + |
| 3 | +use longbridge::asset::{ |
| 4 | + AssetContext, GetStatementListOptions, GetStatementOptions, StatementType, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + asset_context::types::CStatementItemOwned, |
| 9 | + async_call::{CAsyncCallback, execute_async}, |
| 10 | + config::CConfig, |
| 11 | + types::{CString, CVec, cstr_to_rust}, |
| 12 | +}; |
| 13 | + |
| 14 | +/// Asset context |
| 15 | +pub struct CAssetContext { |
| 16 | + ctx: AssetContext, |
| 17 | +} |
| 18 | + |
| 19 | +/// Create a new `AssetContext` |
| 20 | +/// |
| 21 | +/// @param config Config object |
| 22 | +/// @return A new asset context |
| 23 | +#[unsafe(no_mangle)] |
| 24 | +pub unsafe extern "C" fn lb_asset_context_new(config: *const CConfig) -> *const CAssetContext { |
| 25 | + let config = Arc::new((*config).0.clone()); |
| 26 | + let ctx = AssetContext::new(config); |
| 27 | + Arc::into_raw(Arc::new(CAssetContext { ctx })) |
| 28 | +} |
| 29 | + |
| 30 | +/// Retain the asset context (increment reference count) |
| 31 | +#[unsafe(no_mangle)] |
| 32 | +pub unsafe extern "C" fn lb_asset_context_retain(ctx: *const CAssetContext) { |
| 33 | + Arc::increment_strong_count(ctx); |
| 34 | +} |
| 35 | + |
| 36 | +/// Release the asset context (decrement reference count) |
| 37 | +#[unsafe(no_mangle)] |
| 38 | +pub unsafe extern "C" fn lb_asset_context_release(ctx: *const CAssetContext) { |
| 39 | + let _ = Arc::from_raw(ctx); |
| 40 | +} |
| 41 | + |
| 42 | +/// Get statement data list |
| 43 | +/// |
| 44 | +/// @param ctx Asset context |
| 45 | +/// @param statement_type 1 = daily, 2 = monthly |
| 46 | +/// @param start_date Start date for pagination (0 = default) |
| 47 | +/// @param limit Number of results (0 = default 20) |
| 48 | +/// @param callback Async callback |
| 49 | +/// @param userdata User data passed to the callback |
| 50 | +#[unsafe(no_mangle)] |
| 51 | +pub unsafe extern "C" fn lb_asset_context_statements( |
| 52 | + ctx: *const CAssetContext, |
| 53 | + statement_type: i32, |
| 54 | + start_date: i32, |
| 55 | + limit: i32, |
| 56 | + callback: CAsyncCallback, |
| 57 | + userdata: *mut c_void, |
| 58 | +) { |
| 59 | + let ctx_inner = (*ctx).ctx.clone(); |
| 60 | + let st = if statement_type == 2 { |
| 61 | + StatementType::Monthly |
| 62 | + } else { |
| 63 | + StatementType::Daily |
| 64 | + }; |
| 65 | + let mut opts = GetStatementListOptions::new(st); |
| 66 | + if start_date > 0 { |
| 67 | + opts = opts.page(start_date); |
| 68 | + } |
| 69 | + if limit > 0 { |
| 70 | + opts = opts.page_size(limit); |
| 71 | + } |
| 72 | + execute_async(callback, ctx, userdata, async move { |
| 73 | + let rows: CVec<CStatementItemOwned> = ctx_inner.statements(opts).await?.list.into(); |
| 74 | + Ok(rows) |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +/// Get statement data download URL |
| 79 | +/// |
| 80 | +/// @param ctx Asset context |
| 81 | +/// @param file_key File key from the list response |
| 82 | +/// @param callback Async callback |
| 83 | +/// @param userdata User data passed to the callback |
| 84 | +#[unsafe(no_mangle)] |
| 85 | +pub unsafe extern "C" fn lb_asset_context_download_url( |
| 86 | + ctx: *const CAssetContext, |
| 87 | + file_key: *const c_char, |
| 88 | + callback: CAsyncCallback, |
| 89 | + userdata: *mut c_void, |
| 90 | +) { |
| 91 | + let ctx_inner = (*ctx).ctx.clone(); |
| 92 | + let file_key = cstr_to_rust(file_key); |
| 93 | + let opts = GetStatementOptions::new(file_key); |
| 94 | + execute_async(callback, ctx, userdata, async move { |
| 95 | + let url: CString = ctx_inner.statement_download_url(opts).await?.url.into(); |
| 96 | + Ok(url) |
| 97 | + }); |
| 98 | +} |
0 commit comments