Skip to main content

qubit_value/value/
value_constructor.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
11//! `From<T>` implementations for all supported `Value` input types.
12
13use std::collections::HashMap;
14use std::time::Duration;
15
16use bigdecimal::BigDecimal;
17use chrono::{
18    DateTime,
19    NaiveDate,
20    NaiveDateTime,
21    NaiveTime,
22    Utc,
23};
24use num_bigint::BigInt;
25use url::Url;
26
27use super::value::Value;
28
29macro_rules! impl_value_from {
30    ($type:ty, $variant:expr) => {
31        impl From<$type> for Value {
32            #[inline]
33            fn from(value: $type) -> Self {
34                $variant(value)
35            }
36        }
37    };
38}
39
40impl_value_from!(bool, Value::Bool);
41impl_value_from!(char, Value::Char);
42impl_value_from!(i8, Value::Int8);
43impl_value_from!(i16, Value::Int16);
44impl_value_from!(i32, Value::Int32);
45impl_value_from!(i64, Value::Int64);
46impl_value_from!(i128, Value::Int128);
47impl_value_from!(u8, Value::UInt8);
48impl_value_from!(u16, Value::UInt16);
49impl_value_from!(u32, Value::UInt32);
50impl_value_from!(u64, Value::UInt64);
51impl_value_from!(u128, Value::UInt128);
52impl_value_from!(f32, Value::Float32);
53impl_value_from!(f64, Value::Float64);
54impl_value_from!(NaiveDate, Value::Date);
55impl_value_from!(NaiveTime, Value::Time);
56impl_value_from!(NaiveDateTime, Value::DateTime);
57impl_value_from!(DateTime<Utc>, Value::Instant);
58impl_value_from!(BigInt, Value::BigInteger);
59impl_value_from!(BigDecimal, Value::BigDecimal);
60impl_value_from!(isize, Value::IntSize);
61impl_value_from!(usize, Value::UIntSize);
62impl_value_from!(Duration, Value::Duration);
63impl_value_from!(Url, Value::Url);
64impl_value_from!(HashMap<String, String>, Value::StringMap);
65impl_value_from!(serde_json::Value, Value::Json);
66impl_value_from!(String, Value::String);
67
68impl From<&str> for Value {
69    #[inline]
70    fn from(value: &str) -> Self {
71        Value::String(value.to_string())
72    }
73}