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::{
17 DateTime,
18 NaiveDate,
19 NaiveDateTime,
20 NaiveTime,
21 Utc,
22};
23use qubit_value::Value;
24use url::Url;
25
26/// Converts supported Rust values into the metadata backing [`Value`] type.
27///
28/// This trait is implemented for the same concrete input types accepted by
29/// [`Value::new`]. It exists so [`crate::Metadata`] can keep a generic public
30/// API while `qubit-value` keeps its internal constructor trait private.
31pub trait IntoMetadataValue {
32 /// Converts this value into a [`Value`] without changing its concrete data type.
33 ///
34 /// # Returns
35 ///
36 /// Returns the corresponding [`Value`] variant for this input type.
37 fn into_metadata_value(self) -> Value;
38}
39
40macro_rules! impl_into_metadata_value {
41 ($($type:ty),+ $(,)?) => {
42 $(
43 impl IntoMetadataValue for $type {
44 #[inline]
45 fn into_metadata_value(self) -> Value {
46 Value::new(self)
47 }
48 }
49 )+
50 };
51}
52
53impl_into_metadata_value!(
54 bool,
55 char,
56 i8,
57 i16,
58 i32,
59 i64,
60 i128,
61 u8,
62 u16,
63 u32,
64 u64,
65 u128,
66 f32,
67 f64,
68 String,
69 &str,
70 NaiveDate,
71 NaiveTime,
72 NaiveDateTime,
73 DateTime<Utc>,
74 num_bigint::BigInt,
75 BigDecimal,
76 isize,
77 usize,
78 Duration,
79 Url,
80 HashMap<String, String>,
81 serde_json::Value,
82);