postscriptum 0.1.1

Multi-interpreted-language scripting engine for Rust with Rhai and Python backends
Documentation
use crate::EngineType;

#[cfg(feature = "python")]
use crate::python;

#[cfg(feature = "rhai")]
use crate::rhai;

#[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
use pyo3::Python;

#[cfg(all(target_arch = "wasm32", feature = "python"))]
use crate::wasm_utils::JsConvert;

/// A unified function bundle that can handle different scripting engine types
#[derive(Clone)]
pub enum FunctionBundle {
    #[cfg(feature = "python")]
    Python(python::function_bundle::PythonFunctionBundle),
    #[cfg(feature = "rhai")]
    Rhai(rhai::function_bundle::RhaiFunctionBundle),
}

#[allow(deprecated)]
impl FunctionBundle {
    pub fn new(engine_type: EngineType) -> Self {
        match engine_type {
            #[cfg(feature = "python")]
            EngineType::Python => {
                FunctionBundle::Python(python::function_bundle::PythonFunctionBundle::new())
            }
            #[cfg(feature = "rhai")]
            EngineType::Rhai => {
                FunctionBundle::Rhai(rhai::function_bundle::RhaiFunctionBundle::new())
            }
        }
    }

    #[cfg(feature = "python")]
    pub fn new_python() -> Self {
        FunctionBundle::Python(python::function_bundle::PythonFunctionBundle::new())
    }

    #[cfg(feature = "rhai")]
    pub fn new_rhai() -> Self {
        FunctionBundle::Rhai(rhai::function_bundle::RhaiFunctionBundle::new())
    }

    // Python-specific methods
    #[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
    pub fn add_python_fn0<F, R>(&mut self, name: &str, f: F)
    where
        F: Fn() -> R + Send + Sync + 'static,
        R: for<'py> pyo3::IntoPyObject<'py> + Send + Sync + 'static,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn0(name, f);
        }
    }

    #[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
    pub fn add_python_fn1<F, A, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A) -> R + Send + Sync + 'static,
        A: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        R: for<'py> pyo3::IntoPyObject<'py> + Send + Sync + 'static,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn1(name, f);
        }
    }

    #[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
    pub fn add_python_fn2<F, A, B, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B) -> R + Send + Sync + 'static,
        A: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        B: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        R: for<'py> pyo3::IntoPyObject<'py> + Send + Sync + 'static,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn2(name, f);
        }
    }

    #[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
    pub fn add_python_fn3<F, A, B, C, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B, C) -> R + Send + Sync + 'static,
        A: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        B: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        C: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        R: for<'py> pyo3::IntoPyObject<'py> + Send + Sync + 'static,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn3(name, f);
        }
    }

    #[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
    pub fn add_python_fn4<F, A, B, C, D, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B, C, D) -> R + Send + Sync + 'static,
        A: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        B: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        C: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        D: for<'py, 'source> pyo3::FromPyObject<'py, 'source> + Send + Sync + 'static,
        R: for<'py> pyo3::IntoPyObject<'py> + Send + Sync + 'static,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn4(name, f);
        }
    }

    // Rhai-specific methods
    #[cfg(feature = "rhai")]
    pub fn add_rhai_fn0<F, R>(&mut self, name: &str, f: F)
    where
        F: Fn() -> R + Send + Sync + 'static,
        R: Clone + Send + Sync + 'static,
    {
        if let FunctionBundle::Rhai(bundle) = self {
            bundle.add_fn0(name, f);
        }
    }

    #[cfg(feature = "rhai")]
    pub fn add_rhai_fn1<F, A, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A) -> R + Send + Sync + 'static,
        A: Clone + Send + Sync + 'static,
        R: Clone + Send + Sync + 'static,
    {
        if let FunctionBundle::Rhai(bundle) = self {
            bundle.add_fn1(name, f);
        }
    }

    #[cfg(feature = "rhai")]
    pub fn add_rhai_fn2<F, A, B, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B) -> R + Send + Sync + 'static,
        A: Clone + Send + Sync + 'static,
        B: Clone + Send + Sync + 'static,
        R: Clone + Send + Sync + 'static,
    {
        if let FunctionBundle::Rhai(bundle) = self {
            bundle.add_fn2(name, f);
        }
    }

    #[cfg(feature = "rhai")]
    pub fn add_rhai_fn3<F, A, B, C, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B, C) -> R + Send + Sync + 'static,
        A: Clone + Send + Sync + 'static,
        B: Clone + Send + Sync + 'static,
        C: Clone + Send + Sync + 'static,
        R: Clone + Send + Sync + 'static,
    {
        if let FunctionBundle::Rhai(bundle) = self {
            bundle.add_fn3(name, f);
        }
    }

    #[cfg(feature = "rhai")]
    pub fn add_rhai_fn4<F, A, B, C, D, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B, C, D) -> R + Send + Sync + 'static,
        A: Clone + Send + Sync + 'static,
        B: Clone + Send + Sync + 'static,
        C: Clone + Send + Sync + 'static,
        D: Clone + Send + Sync + 'static,
        R: Clone + Send + Sync + 'static,
    {
        if let FunctionBundle::Rhai(bundle) = self {
            bundle.add_fn4(name, f);
        }
    }

    /// Register all functions in the bundle with their respective engine
    ///
    /// This is intentionally a **no-op stub**: use the engine-specific
    /// `register_python` / `register_rhai` methods instead.
    pub fn register(&self) {
        #[cfg(feature = "python")]
        if let FunctionBundle::Python(_bundle) = self {
            // Python registration requires a Python context.
            // For now, use `register_python` instead.
        }
        #[cfg(feature = "rhai")]
        if let FunctionBundle::Rhai(_bundle) = self {
            // Rhai registration requires an engine instance.
            // For now, use `register_rhai` instead.
        }
    }

    /// Register Python functions (requires Python context and Python engine)
    #[cfg(all(not(target_arch = "wasm32"), feature = "python"))]
    pub fn register_python(
        &self,
        py: Python<'_>,
        engine: &mut crate::python::PythonEngine,
    ) -> pyo3::PyResult<()> {
        if let FunctionBundle::Python(bundle) = self {
            bundle.register(py, engine)
        } else {
            Ok(())
        }
    }

    /// Register Python functions (requires Python context and Python engine) for WASM
    #[cfg(all(target_arch = "wasm32", feature = "python"))]
    pub fn register_python(
        &self,
        _engine: &mut crate::python::PythonEngine,
    ) -> Result<(), wasm_bindgen::JsValue> {
        if let FunctionBundle::Python(bundle) = self {
            // First, register the functions in the global JavaScript scope
            bundle.register()?;
        }
        Ok(())
    }

    /// Register Rhai functions (requires Rhai engine)
    #[cfg(feature = "rhai")]
    pub fn register_rhai(&self, engine: &mut ::rhai::Engine) {
        if let FunctionBundle::Rhai(bundle) = self {
            bundle.register(engine);
        }
    }
}

// Add WASM-specific methods when on WASM target
#[cfg(all(target_arch = "wasm32", feature = "python"))]
impl FunctionBundle {
    #[cfg(all(target_arch = "wasm32", feature = "python"))]
    pub fn add_python_fn0<F, R>(&mut self, name: &str, f: F)
    where
        F: Fn() -> R + Send + Sync + Clone + 'static,
        R: Clone + Send + Sync + 'static + Into<wasm_bindgen::JsValue> + JsConvert,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn0(name, f);
        }
    }

    #[cfg(all(target_arch = "wasm32", feature = "python"))]
    pub fn add_python_fn1<F, A, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A) -> R + Send + Sync + Clone + 'static,
        A: Clone + Send + Sync + 'static + JsConvert,
        R: Clone + Send + Sync + 'static + Into<wasm_bindgen::JsValue> + JsConvert,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn1(name, f);
        }
    }

    #[cfg(all(target_arch = "wasm32", feature = "python"))]
    pub fn add_python_fn2<F, A, B, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B) -> R + Send + Sync + Clone + 'static,
        A: Clone + Send + Sync + 'static + JsConvert,
        B: Clone + Send + Sync + 'static + JsConvert,
        R: Clone + Send + Sync + 'static + Into<wasm_bindgen::JsValue> + JsConvert,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn2(name, f);
        }
    }

    #[cfg(all(target_arch = "wasm32", feature = "python"))]
    pub fn add_python_fn3<F, A, B, C, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B, C) -> R + Send + Sync + Clone + 'static,
        A: Clone + Send + Sync + 'static + JsConvert,
        B: Clone + Send + Sync + 'static + JsConvert,
        C: Clone + Send + Sync + 'static + JsConvert,
        R: Clone + Send + Sync + 'static + Into<wasm_bindgen::JsValue> + JsConvert,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn3(name, f);
        }
    }

    #[cfg(all(target_arch = "wasm32", feature = "python"))]
    pub fn add_python_fn4<F, A, B, C, D, R>(&mut self, name: &str, f: F)
    where
        F: Fn(A, B, C, D) -> R + Send + Sync + Clone + 'static,
        A: Clone + Send + Sync + 'static + JsConvert,
        B: Clone + Send + Sync + 'static + JsConvert,
        C: Clone + Send + Sync + 'static + JsConvert,
        D: Clone + Send + Sync + 'static + JsConvert,
        R: Clone + Send + Sync + 'static + Into<wasm_bindgen::JsValue> + JsConvert,
    {
        if let FunctionBundle::Python(bundle) = self {
            bundle.add_fn4(name, f);
        }
    }
}

#[cfg(all(test, feature = "python", feature = "rhai"))]
mod tests {
    use super::*;

    #[test]
    fn test_function_bundle_creation_and_registration() {
        // Test Python bundle creation
        let mut python_bundle = FunctionBundle::new_python();
        python_bundle.add_python_fn0::<fn() -> i32, i32>("get_number", || 42);
        python_bundle.add_python_fn1::<fn(i64) -> i64, i64, i64>("square", |x: i64| x * x);
        python_bundle.add_python_fn2::<fn(f64, f64) -> f64, f64, f64, f64>(
            "power",
            |base: f64, exp: f64| base.powf(exp),
        );
        python_bundle.add_python_fn3::<fn(i32, i32, i32) -> i32, i32, i32, i32, i32>(
            "sum_three",
            |a: i32, b: i32, c: i32| a + b + c,
        );
        python_bundle.add_python_fn4::<fn(i32, i32, i32, i32) -> i32, i32, i32, i32, i32, i32>(
            "sum_four",
            |a: i32, b: i32, c: i32, d: i32| a + b + c + d,
        );

        println!("✓ Created Python function bundle with 5 functions");

        // Test Rhai bundle creation
        let mut rhai_bundle = FunctionBundle::new_rhai();
        rhai_bundle.add_rhai_fn0::<fn() -> i32, i32>("get_answer", || 42);
        rhai_bundle.add_rhai_fn1::<fn(i32) -> i32, i32, i32>("increment", |x: i32| x + 1);
        rhai_bundle
            .add_rhai_fn2::<fn(i32, i32) -> i32, i32, i32, i32>("modulo", |a: i32, b: i32| a % b);
        rhai_bundle.add_rhai_fn3::<fn(i32, i32, i32) -> i32, i32, i32, i32, i32>(
            "sum_three",
            |a: i32, b: i32, c: i32| a + b + c,
        );
        rhai_bundle.add_rhai_fn4::<fn(i32, i32, i32, i32) -> i32, i32, i32, i32, i32, i32>(
            "sum_four",
            |a: i32, b: i32, c: i32, d: i32| a + b + c + d,
        );

        println!("✓ Created Rhai function bundle with 5 functions");

        // Verify that the bundles were created without errors
        match python_bundle {
            FunctionBundle::Python(_) => println!("✓ Python bundle is correctly typed"),
            _ => panic!("Python bundle has incorrect type"),
        }

        match rhai_bundle {
            FunctionBundle::Rhai(_) => println!("✓ Rhai bundle is correctly typed"),
            _ => panic!("Rhai bundle has incorrect type"),
        }
    }
}