postscriptum 0.1.1

Multi-interpreted-language scripting engine for Rust with Rhai and Python backends
Documentation
//! JSON bridge for the Rhai backend (G5): thin wrappers, no new semantics.
//!
//! [`to_dynamic_value`] / [`from_dynamic_value`] move
//! `serde_json::Value` across the rhai boundary via rhai's `serde`
//! feature. Documented fallbacks: `()` → `Null`; exotic dynamics
//! (bytes, object handles, …) → [`Error::UnconvertibleValue`], never
//! a panic. Whole-number floats keep their type (`10.0` stays float
//! — pinned by test, do NOT "fix").

use rhai::Dynamic;
use serde_json::Value;

use crate::{Error, Result};

/// Convert JSON to a rhai [`Dynamic`].
///
/// Infallible in practice for JSON-shaped data; exotic `Value`s that
/// rhai cannot represent fail with [`Error::UnconvertibleValue`].
pub fn to_dynamic_value(value: &Value) -> Result<Dynamic> {
    rhai::serde::to_dynamic(value).map_err(|_| Error::UnconvertibleValue {
        backend: crate::EngineType::Rhai,
    })
}

/// Convert a rhai [`Dynamic`] to JSON.
///
/// `()` becomes `Null`. Values with no JSON representation (BLOBs,
/// opaque object handles, …) fail with
/// [`Error::UnconvertibleValue`] — never panic on user-shaped data.
pub fn from_dynamic_value(value: &Dynamic) -> Result<Value> {
    rhai::serde::from_dynamic(value).map_err(|_| Error::UnconvertibleValue {
        backend: crate::EngineType::Rhai,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn round_trip_representative_json() {
        let cases = [
            json!(42),
            json!(-7),
            json!(10.0),
            json!(0.5),
            json!("hello"),
            json!(""),
            json!(true),
            json!(false),
            json!(null),
            json!([1, "two", 3.0, null, true]),
            json!({"a": 1, "b": [1, 2], "c": {"d": null}}),
        ];
        for value in cases {
            let back = from_dynamic_value(&to_dynamic_value(&value).unwrap()).unwrap();
            assert_eq!(back, value, "round trip failed for {value}");
        }
    }

    #[test]
    fn whole_floats_keep_their_type() {
        // `10.0` must stay float, NOT collapse to int 10. Pinned.
        let back = from_dynamic_value(&to_dynamic_value(&json!(10.0)).unwrap()).unwrap();
        assert!(back.is_f64(), "whole float must stay float, got: {back}");
        assert_eq!(back, json!(10.0));
        // …while genuine ints stay ints.
        let back = from_dynamic_value(&to_dynamic_value(&json!(10)).unwrap()).unwrap();
        assert!(back.is_i64(), "int must stay int, got: {back}");
    }

    #[test]
    fn unit_becomes_null() {
        assert_eq!(from_dynamic_value(&Dynamic::UNIT).unwrap(), Value::Null);
    }

    #[test]
    fn exotic_values_error_without_panicking() {
        // Opaque Rust values stored in a Dynamic have no JSON
        // representation. (Note: BLOBs are NOT exotic — rhai::serde
        // reads them as arrays of numbers.)
        #[derive(Clone)]
        struct Opaque;
        let exotic = Dynamic::from(Opaque);
        let err = from_dynamic_value(&exotic).unwrap_err();
        assert!(
            matches!(err, Error::UnconvertibleValue { .. }),
            "expected UnconvertibleValue, got: {err:?}"
        );
    }
}