"""Internal helpers to produce JSON matching Rust serde externally-tagged enums."""
from __future__ import annotations

import math
from typing import Any


def scalar_value_to_json(value: Any) -> Any:
    """Convert a Python value to a Rust ScalarValue JSON representation.

    Rust serde format (externally tagged):
      None  -> "Null"
      bool  -> {"Bool": v}
      int   -> {"Int64": v}
      float -> {"Float64": v}
      str   -> {"Utf8": v}
    """
    if value is None:
        return "Null"
    if isinstance(value, bool):
        return {"Bool": value}
    if isinstance(value, int):
        return {"Int64": value}
    if isinstance(value, float):
        if math.isnan(value):
            return "NaN"
        return {"Float64": value}
    if isinstance(value, str):
        return {"Utf8": value}
    raise TypeError(f"Cannot convert {type(value).__name__} to ScalarValue")
