qubit-value 0.12.2

Type-safe containers for single, multi-valued, and named runtime values
Documentation
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================

//! Borrowed scalar payload used by V1 serialization.

use serde::Serialize;

use super::WireDataTypeV1;
use crate::Value;
use crate::value::ValueRepr;

/// Defines the borrowed scalar payload and its exhaustive runtime conversion.
macro_rules! define_scalar_wire_ref {
    (
        $($arg:expr),*;
        $(
            (
                [$($cfg:meta),*],
                $variant:ident,
                $type:ty,
                $_data_type:expr,
                $_materialization:ident,
                $_json_class:ident,
                $_number_projection:ident,
                $_value_doc:literal,
                $_multi_doc:literal,
                [$($scalar_attr:meta),*],
                [$($collection_attr:meta),*],
                $tag:literal,
                $_wire_preflight:ident
            )
        ),+ $(,)?
    ) => {
        /// Borrowed payload for one typed scalar.
        ///
        /// # Type Parameters
        ///
        /// * `'a` - Lifetime of the borrowed scalar payload.
        #[derive(Clone, Copy, Serialize)]
        pub(in crate::value_wire) enum ScalarWireRef<'a> {
            /// Unset scalar and its declared data type.
            #[serde(rename = "unset")]
            Unset(
                /// Declared data type of the unset scalar.
                WireDataTypeV1,
            ),
            $(
                #[doc = concat!("Borrowed `", $tag, "` scalar payload.")]
                $(#[$cfg])*
                $(#[$scalar_attr])*
                #[serde(rename = $tag)]
                $variant(
                    #[doc = concat!("Stored `", $tag, "` scalar value.")]
                    &'a $type,
                ),
            )+
        }

        impl<'a> From<&'a Value> for ScalarWireRef<'a> {
            /// Borrows the exact runtime variant for V1 serialization.
            fn from(value: &'a Value) -> Self {
                match &value.repr {
                    ValueRepr::Unset(data_type) => Self::Unset((*data_type).into()),
                    $(
                        $(#[$cfg])*
                        ValueRepr::$variant(value) => {
                            Self::$variant(value_storage_ref!($variant, value))
                        },
                    )+
                }
            }
        }
    };
}

for_each_value_type!(define_scalar_wire_ref);