rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
Documentation
//! JSON-based FFI API for numerical geometric algebra operations.

use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::c_char;

use serde::Deserialize;

use crate::ffi_apis::ffi_api::FfiResult;
use crate::numerical::geometric_algebra::Multivector3D;

#[derive(Deserialize)]
struct GaInput {
    mv: Multivector3D,
}

#[derive(Deserialize)]
struct TwoGaInput {
    mv1: Multivector3D,
    mv2: Multivector3D,
}

/// JSON FFI for `ga_add`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_add_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: TwoGaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv1 + input.mv2),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_sub`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_sub_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: TwoGaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv1 - input.mv2),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_mul`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_mul_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: TwoGaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv1 * input.mv2),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_wedge`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_wedge_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: TwoGaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv1.wedge(input.mv2)),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_dot`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_dot_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: TwoGaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv1.dot(input.mv2)),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_reverse`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_reverse_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: GaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv.reverse()),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_norm`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_norm_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: GaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = FfiResult {
            ok: Some(input.mv.norm()),
            err: None::<String>,
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}

/// JSON FFI for `ga_inv`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_ga_inv_json(json_ptr: *const c_char) -> *mut c_char {
    unsafe {
        let json_str = match CStr::from_ptr(json_ptr).to_str() {
            | Ok(s) => s,
            | Err(_) => return std::ptr::null_mut(),
        };

        let input: GaInput = match serde_json::from_str(json_str) {
            | Ok(v) => v,
            | Err(e) => {
                return CString::new(format!(
                    "{{\"err\": \
                         \"{e}\"}}"
                ))
                .unwrap()
                .into_raw();
            },
        };

        let res = match input.mv.inv() {
            | Some(v) => {
                FfiResult {
                    ok: Some(v),
                    err: None::<String>,
                }
            },
            | None => {
                FfiResult {
                    ok: None,
                    err: Some(
                        "Multivector is \
                     not invertible"
                            .to_string(),
                    ),
                }
            },
        };

        CString::new(serde_json::to_string(&res).unwrap())
            .unwrap()
            .into_raw()
    }
}