Skip to main content

qubit_metadata/
into_metadata_value.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Provides conversion from supported Rust values into metadata values.
11
12use std::collections::HashMap;
13use std::time::Duration;
14
15use bigdecimal::BigDecimal;
16use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
17use qubit_value::Value;
18use url::Url;
19
20/// Converts supported Rust values into the metadata backing [`Value`] type.
21///
22/// This trait is implemented for the same concrete input types accepted by
23/// [`Value::new`]. It exists so [`crate::Metadata`] can keep a generic public
24/// API while `qubit-value` keeps its internal constructor trait private.
25pub trait IntoMetadataValue {
26    /// Converts this value into a [`Value`] without changing its concrete data type.
27    ///
28    /// # Returns
29    ///
30    /// Returns the corresponding [`Value`] variant for this input type.
31    fn into_metadata_value(self) -> Value;
32}
33
34macro_rules! impl_into_metadata_value {
35    ($($type:ty),+ $(,)?) => {
36        $(
37            impl IntoMetadataValue for $type {
38                #[inline]
39                fn into_metadata_value(self) -> Value {
40                    Value::new(self)
41                }
42            }
43        )+
44    };
45}
46
47impl_into_metadata_value!(
48    bool,
49    char,
50    i8,
51    i16,
52    i32,
53    i64,
54    i128,
55    u8,
56    u16,
57    u32,
58    u64,
59    u128,
60    f32,
61    f64,
62    String,
63    &str,
64    NaiveDate,
65    NaiveTime,
66    NaiveDateTime,
67    DateTime<Utc>,
68    num_bigint::BigInt,
69    BigDecimal,
70    isize,
71    usize,
72    Duration,
73    Url,
74    HashMap<String, String>,
75    serde_json::Value,
76);