use std::ffi::c_char;
use blazen_uniffi::errors::BlazenError as InnerError;
use crate::error::BlazenError;
use crate::runtime::runtime;
use crate::string::cstr_to_str;
fn write_error(out_err: *mut *mut BlazenError, err: InnerError) {
if !out_err.is_null() {
unsafe {
*out_err = BlazenError::from(err).into_ptr();
}
}
}
fn write_internal_error(out_err: *mut *mut BlazenError, message: &str) -> i32 {
write_error(
out_err,
InnerError::Internal {
message: message.to_owned(),
},
);
-1
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_refresh_pricing_blocking(
url: *const c_char,
out_count: *mut u32,
out_err: *mut *mut BlazenError,
) -> i32 {
let target = if url.is_null() {
blazen_llm::DEFAULT_PRICING_URL.to_owned()
} else {
match unsafe { cstr_to_str(url) } {
Some(s) => s.to_owned(),
None => {
return write_internal_error(out_err, "blazen_refresh_pricing: invalid utf-8 url");
}
}
};
let result =
runtime().block_on(async move { blazen_llm::refresh_default_with_url(&target).await });
match result {
Ok(count) => {
if !out_count.is_null() {
let n = u32::try_from(count).unwrap_or(u32::MAX);
unsafe {
*out_count = n;
}
}
0
}
Err(e) => {
write_error(
out_err,
InnerError::Internal {
message: e.to_string(),
},
);
-1
}
}
}