use super::guard::{clear_last_error, ffi_guard, take_last_error};
use super::types::*;
use crate::prelude::*;
use std::ffi::{c_char, CString};
use std::ptr;
#[no_mangle]
pub const extern "C" fn alice_sdf_version() -> VersionInfo {
VersionInfo::current()
}
#[no_mangle]
pub extern "C" fn alice_sdf_version_string() -> *mut c_char {
ffi_guard(std::ptr::null_mut(), || {
let version = format!(
"ALICE-SDF v{}.{}.{} (Deep Fried)",
VersionInfo::current().major,
VersionInfo::current().minor,
VersionInfo::current().patch
);
CString::new(version).map_or_else(|_| ptr::null_mut(), CString::into_raw)
})
}
#[no_mangle]
pub extern "C" fn alice_sdf_last_error() -> *mut c_char {
ffi_guard(ptr::null_mut(), || {
take_last_error()
.and_then(|m| CString::new(m).ok())
.map_or_else(ptr::null_mut, CString::into_raw)
})
}
#[no_mangle]
pub extern "C" fn alice_sdf_clear_last_error() {
ffi_guard((), clear_last_error);
}
#[no_mangle]
pub const extern "C" fn alice_sdf_primitive_count() -> u32 {
SdfCategory::Primitive.count()
}
#[no_mangle]
pub const extern "C" fn alice_sdf_operation_count() -> u32 {
SdfCategory::Operation.count()
}
#[no_mangle]
pub const extern "C" fn alice_sdf_transform_count() -> u32 {
SdfCategory::Transform.count()
}
#[no_mangle]
pub const extern "C" fn alice_sdf_modifier_count() -> u32 {
SdfCategory::Modifier.count()
}
#[no_mangle]
pub const extern "C" fn alice_sdf_total_count() -> u32 {
SdfCategory::total()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_is_the_crate_version() {
let v = alice_sdf_version();
assert_eq!(
(u32::from(v.major), u32::from(v.minor), u32::from(v.patch)),
(
env!("CARGO_PKG_VERSION_MAJOR").parse::<u32>().unwrap(),
env!("CARGO_PKG_VERSION_MINOR").parse::<u32>().unwrap(),
env!("CARGO_PKG_VERSION_PATCH").parse::<u32>().unwrap()
),
"alice_sdf_version() must follow Cargo.toml"
);
}
#[test]
fn version_string_matches_version() {
let ptr = alice_sdf_version_string();
assert!(!ptr.is_null());
let text = unsafe { CString::from_raw(ptr) }.into_string().unwrap();
assert!(
text.contains(env!("CARGO_PKG_VERSION")),
"`{text}` does not contain {}",
env!("CARGO_PKG_VERSION")
);
}
}