Skip to main content

qubit_value/value/
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//! # Single Value Container
11//!
12//! Provides type-safe storage and access functionality for single values.
13//!
14
15use bigdecimal::BigDecimal;
16use chrono::{
17    DateTime,
18    NaiveDate,
19    NaiveDateTime,
20    NaiveTime,
21    Utc,
22};
23use num_bigint::BigInt;
24use serde::{
25    Deserialize,
26    Serialize,
27};
28use std::collections::HashMap;
29use std::time::Duration;
30use url::Url;
31
32use qubit_datatype::{
33    DataConversionOptions,
34    DataConvertTo,
35    DataConverter,
36    DataType,
37};
38
39use crate::value_error::ValueResult;
40
41/// Single value container
42///
43/// Uses an enum to represent different types of values, providing
44/// type-safe value storage and access.
45///
46/// # Features
47///
48/// - Zero-cost abstraction with compile-time type checking
49/// - Supports multiple basic data types
50/// - Provides two sets of APIs for type checking and type conversion
51/// - Automatic memory management
52///
53/// # Example
54///
55/// ```rust
56/// use qubit_value::Value;
57///
58/// // Create an integer value
59/// let value = Value::Int32(42);
60/// assert_eq!(value.get_int32().unwrap(), 42);
61///
62/// // Type conversion
63/// let converted = value.to::<i64>().unwrap();
64/// assert_eq!(converted, 42i64);
65///
66/// // String value
67/// let text = Value::String("hello".to_string());
68/// assert_eq!(text.get_string().unwrap(), "hello");
69/// ```
70///
71///
72#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
73pub enum Value {
74    /// Empty value (has type but no value)
75    Empty(DataType),
76    /// Boolean value
77    Bool(bool),
78    /// Character value
79    Char(char),
80    /// 8-bit signed integer
81    Int8(i8),
82    /// 16-bit signed integer
83    Int16(i16),
84    /// 32-bit signed integer
85    Int32(i32),
86    /// 64-bit signed integer
87    Int64(i64),
88    /// 128-bit signed integer
89    Int128(i128),
90    /// 8-bit unsigned integer
91    UInt8(u8),
92    /// 16-bit unsigned integer
93    UInt16(u16),
94    /// 32-bit unsigned integer
95    UInt32(u32),
96    /// 64-bit unsigned integer
97    UInt64(u64),
98    /// 128-bit unsigned integer
99    UInt128(u128),
100    /// Platform-dependent signed integer (isize)
101    IntSize(isize),
102    /// Platform-dependent unsigned integer (usize)
103    UIntSize(usize),
104    /// 32-bit floating point number
105    Float32(f32),
106    /// 64-bit floating point number
107    Float64(f64),
108    /// Big integer type
109    BigInteger(BigInt),
110    /// Big decimal type
111    BigDecimal(BigDecimal),
112    /// String
113    String(String),
114    /// Date
115    Date(NaiveDate),
116    /// Time
117    Time(NaiveTime),
118    /// Date and time
119    DateTime(NaiveDateTime),
120    /// UTC instant
121    Instant(DateTime<Utc>),
122    /// Duration type (std::time::Duration)
123    Duration(Duration),
124    /// URL type (url::Url)
125    Url(Url),
126    /// String map type (HashMap<String, String>)
127    StringMap(HashMap<String, String>),
128    /// JSON value type (serde_json::Value)
129    Json(serde_json::Value),
130}
131
132#[doc(hidden)]
133pub use super::value_constructor::ValueConstructor;
134#[doc(hidden)]
135pub use super::value_converter::ValueConverter;
136#[doc(hidden)]
137pub use super::value_getter::ValueGetter;
138#[doc(hidden)]
139pub use super::value_setter::ValueSetter;
140
141// ============================================================================
142// Getter method generation macro
143// ============================================================================
144
145/// Unified getter generation macro
146///
147/// Supports two modes:
148/// 1. `copy:` - For types implementing the Copy trait, directly returns the value
149/// 2. `ref:` - For non-Copy types, returns a reference
150///
151/// # Documentation Comment Support
152///
153/// The macro automatically extracts preceding documentation comments, so
154/// you can add `///` comments before macro invocations.
155///
156///
157impl Value {
158    /// Generic constructor method
159    ///
160    /// Creates a `Value` from any supported type, avoiding direct use of
161    /// enum variants.
162    ///
163    /// # Supported Generic Types
164    ///
165    /// `Value::new<T>(value)` currently supports the following `T`:
166    ///
167    /// - `bool`
168    /// - `char`
169    /// - `i8`, `i16`, `i32`, `i64`, `i128`
170    /// - `u8`, `u16`, `u32`, `u64`, `u128`
171    /// - `f32`, `f64`
172    /// - `String`, `&str`
173    /// - `NaiveDate`, `NaiveTime`, `NaiveDateTime`, `DateTime<Utc>`
174    /// - `BigInt`, `BigDecimal`
175    /// - `isize`, `usize`
176    /// - `Duration`
177    /// - `Url`
178    /// - `HashMap<String, String>`
179    /// - `serde_json::Value`
180    ///
181    /// # Type Parameters
182    ///
183    /// * `T` - The type of the value to wrap
184    ///
185    /// # Returns
186    ///
187    /// Returns a `Value` wrapping the given value
188    ///
189    /// # Example
190    ///
191    /// ```rust
192    /// use qubit_value::Value;
193    ///
194    /// // Basic types
195    /// let v = Value::new(42i32);
196    /// assert_eq!(v.get_int32().unwrap(), 42);
197    ///
198    /// let v = Value::new(true);
199    /// assert_eq!(v.get_bool().unwrap(), true);
200    ///
201    /// // String
202    /// let v = Value::new("hello".to_string());
203    /// assert_eq!(v.get_string().unwrap(), "hello");
204    /// ```
205    #[inline]
206    pub fn new<T>(value: T) -> Self
207    where
208        Self: ValueConstructor<T>,
209    {
210        <Self as ValueConstructor<T>>::from_type(value)
211    }
212
213    /// Generic getter method
214    ///
215    /// Automatically selects the correct getter method based on the target
216    /// type, performing strict type checking.
217    ///
218    /// `get<T>()` performs strict type matching. It does not do cross-type
219    /// conversion.
220    ///
221    /// For example, `Value::Int32(42).get::<i64>()` fails, while
222    /// `Value::Int32(42).to::<i64>()` succeeds.
223    ///
224    /// # Supported Generic Types
225    ///
226    /// `Value::get<T>()` currently supports the following `T`:
227    ///
228    /// - `bool`
229    /// - `char`
230    /// - `i8`, `i16`, `i32`, `i64`, `i128`
231    /// - `u8`, `u16`, `u32`, `u64`, `u128`
232    /// - `f32`, `f64`
233    /// - `String`
234    /// - `NaiveDate`, `NaiveTime`, `NaiveDateTime`, `DateTime<Utc>`
235    /// - `BigInt`, `BigDecimal`
236    /// - `isize`, `usize`
237    /// - `Duration`
238    /// - `Url`
239    /// - `HashMap<String, String>`
240    /// - `serde_json::Value`
241    ///
242    /// # Type Parameters
243    ///
244    /// * `T` - The target type to retrieve
245    ///
246    /// # Returns
247    ///
248    /// If types match, returns the value of the corresponding type;
249    /// otherwise returns an error
250    ///
251    /// # Example
252    ///
253    /// ```rust
254    /// use qubit_value::Value;
255    ///
256    /// let value = Value::Int32(42);
257    ///
258    /// // Through type inference
259    /// let num: i32 = value.get().unwrap();
260    /// assert_eq!(num, 42);
261    ///
262    /// // Explicitly specify type parameter
263    /// let num = value.get::<i32>().unwrap();
264    /// assert_eq!(num, 42);
265    ///
266    /// // Different type
267    /// let text = Value::String("hello".to_string());
268    /// let s: String = text.get().unwrap();
269    /// assert_eq!(s, "hello");
270    ///
271    /// // Boolean value
272    /// let flag = Value::Bool(true);
273    /// let b: bool = flag.get().unwrap();
274    /// assert_eq!(b, true);
275    /// ```
276    #[inline]
277    pub fn get<T>(&self) -> ValueResult<T>
278    where
279        Self: ValueGetter<T>,
280    {
281        <Self as ValueGetter<T>>::get_value(self)
282    }
283
284    /// Generic conversion method
285    ///
286    /// Converts the current value to the target type according to the conversion
287    /// rules defined by [`ValueConverter<T>`].
288    ///
289    /// # Supported Target Types And Source Variants
290    ///
291    /// `Value::to<T>()` currently supports the following target types:
292    ///
293    /// - `bool`
294    ///   - `Value::Bool`
295    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
296    ///     `Value::Int128`
297    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
298    ///     `Value::UInt64`, `Value::UInt128`
299    ///   - `Value::String`, parsed as `1`, `0`, or ASCII case-insensitive
300    ///     `true` / `false`
301    /// - `char`
302    ///   - `Value::Char`
303    /// - `i8`
304    ///   - `Value::Int8`
305    ///   - `Value::Bool`
306    ///   - `Value::Char`
307    ///   - all integer variants
308    ///   - `Value::Float32`, `Value::Float64`
309    ///   - `Value::String`, parsed as `i8`
310    ///   - `Value::BigInteger`, `Value::BigDecimal`
311    /// - `i16`
312    ///   - `Value::Int16`
313    ///   - `Value::Bool`
314    ///   - `Value::Char`
315    ///   - all integer variants
316    ///   - `Value::Float32`, `Value::Float64`
317    ///   - `Value::String`, parsed as `i16`
318    ///   - `Value::BigInteger`, `Value::BigDecimal`
319    /// - `i32`
320    ///   - `Value::Int32`
321    ///   - `Value::Bool`
322    ///   - `Value::Char`
323    ///   - `Value::Int8`, `Value::Int16`, `Value::Int64`, `Value::Int128`
324    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
325    ///     `Value::UInt64`, `Value::UInt128`
326    ///   - `Value::Float32`, `Value::Float64`
327    ///   - `Value::String`, parsed as `i32`
328    ///   - `Value::BigInteger`, `Value::BigDecimal`
329    /// - `i64`
330    ///   - `Value::Int64`
331    ///   - `Value::Bool`
332    ///   - `Value::Char`
333    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int128`
334    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
335    ///     `Value::UInt64`, `Value::UInt128`
336    ///   - `Value::Float32`, `Value::Float64`
337    ///   - `Value::String`, parsed as `i64`
338    ///   - `Value::BigInteger`, `Value::BigDecimal`
339    /// - `i128`
340    ///   - `Value::Int128`
341    ///   - `Value::Bool`
342    ///   - `Value::Char`
343    ///   - all integer variants
344    ///   - `Value::Float32`, `Value::Float64`
345    ///   - `Value::String`, parsed as `i128`
346    ///   - `Value::BigInteger`, `Value::BigDecimal`
347    /// - `u8`
348    ///   - `Value::UInt8`
349    ///   - `Value::Bool`
350    ///   - `Value::Char`
351    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
352    ///     `Value::Int128`
353    ///   - `Value::UInt16`, `Value::UInt32`, `Value::UInt64`,
354    ///     `Value::UInt128`
355    ///   - `Value::String`, parsed as `u8`
356    /// - `u16`
357    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
358    ///     `Value::UInt64`, `Value::UInt128`
359    ///   - `Value::Bool`
360    ///   - `Value::Char`
361    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
362    ///     `Value::Int128`
363    ///   - `Value::String`, parsed as `u16`
364    /// - `u32`
365    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
366    ///     `Value::UInt64`, `Value::UInt128`
367    ///   - `Value::Bool`
368    ///   - `Value::Char`
369    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
370    ///     `Value::Int128`
371    ///   - `Value::String`, parsed as `u32`
372    /// - `u64`
373    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
374    ///     `Value::UInt64`, `Value::UInt128`
375    ///   - `Value::Bool`
376    ///   - `Value::Char`
377    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
378    ///     `Value::Int128`
379    ///   - `Value::String`, parsed as `u64`
380    /// - `u128`
381    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
382    ///     `Value::UInt64`, `Value::UInt128`
383    ///   - `Value::Bool`
384    ///   - `Value::Char`
385    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
386    ///     `Value::Int128`
387    ///   - `Value::String`, parsed as `u128`
388    /// - `f32`
389    ///   - `Value::Float32`, `Value::Float64`
390    ///   - `Value::Bool`
391    ///   - `Value::Char`
392    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
393    ///     `Value::Int128`
394    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
395    ///     `Value::UInt64`, `Value::UInt128`
396    ///   - `Value::String`, parsed as `f32`
397    ///   - `Value::BigInteger`, `Value::BigDecimal`
398    /// - `f64`
399    ///   - `Value::Float64`
400    ///   - `Value::Bool`
401    ///   - `Value::Char`
402    ///   - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
403    ///     `Value::Int128`
404    ///   - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
405    ///     `Value::UInt64`, `Value::UInt128`
406    ///   - `Value::Float32`
407    ///   - `Value::String`, parsed as `f64`
408    ///   - `Value::BigInteger`, `Value::BigDecimal`
409    /// - `String`
410    ///   - `Value::String`
411    ///   - `Value::Bool`, `Value::Char`
412    ///   - all integer and floating-point variants
413    ///   - `Value::Date`, `Value::Time`, `Value::DateTime`, `Value::Instant`
414    ///   - `Value::BigInteger`, `Value::BigDecimal`
415    ///   - `Value::IntSize`, `Value::UIntSize`
416    ///   - `Value::Duration`, formatted as `<nanoseconds>ns`
417    ///   - `Value::Url`
418    ///   - `Value::StringMap`, serialized as JSON text
419    ///   - `Value::Json`, serialized as JSON text
420    /// - `NaiveDate`
421    ///   - `Value::Date`
422    /// - `NaiveTime`
423    ///   - `Value::Time`
424    /// - `NaiveDateTime`
425    ///   - `Value::DateTime`
426    /// - `DateTime<Utc>`
427    ///   - `Value::Instant`
428    /// - `BigInt`
429    ///   - `Value::BigInteger`
430    /// - `BigDecimal`
431    ///   - `Value::BigDecimal`
432    /// - `isize`
433    ///   - `Value::IntSize`
434    ///   - `Value::Bool`
435    ///   - `Value::Char`
436    ///   - all integer variants
437    ///   - `Value::Float32`, `Value::Float64`
438    ///   - `Value::String`, parsed as `isize`
439    ///   - `Value::BigInteger`, `Value::BigDecimal`
440    /// - `usize`
441    ///   - `Value::UIntSize`
442    ///   - `Value::Bool`
443    ///   - `Value::Char`
444    ///   - all integer variants
445    ///   - `Value::String`, parsed as `usize`
446    /// - `Duration`
447    ///   - `Value::Duration`
448    ///   - `Value::String`, parsed from `<nanoseconds>ns`
449    /// - `Url`
450    ///   - `Value::Url`
451    ///   - `Value::String`, parsed as URL text
452    /// - `HashMap<String, String>`
453    ///   - `Value::StringMap`
454    /// - `serde_json::Value`
455    ///   - `Value::Json`
456    ///   - `Value::String`, parsed as JSON text
457    ///   - `Value::StringMap`, converted to a JSON object
458    ///
459    /// Any target type not listed above is not supported by `Value::to<T>()`.
460    ///
461    /// # Type Parameters
462    ///
463    /// * `T` - The target type to convert to
464    ///
465    /// # Returns
466    ///
467    /// Returns the converted value on success, or an error if conversion is not
468    /// supported or fails.
469    ///
470    /// # Example
471    ///
472    /// ```rust
473    /// use qubit_value::Value;
474    ///
475    /// let value = Value::Int32(42);
476    ///
477    /// let num: i64 = value.to().unwrap();
478    /// assert_eq!(num, 42);
479    ///
480    /// let text: String = value.to().unwrap();
481    /// assert_eq!(text, "42");
482    /// ```
483    #[inline]
484    pub fn to<T>(&self) -> ValueResult<T>
485    where
486        Self: ValueConverter<T>,
487    {
488        <Self as ValueConverter<T>>::convert(self)
489    }
490
491    /// Converts this value to `T` using the provided conversion options.
492    ///
493    /// This method uses the shared [`qubit_datatype`] conversion layer directly,
494    /// so options such as string trimming, blank string handling, and boolean
495    /// aliases are applied consistently with other value containers.
496    ///
497    /// # Type Parameters
498    ///
499    /// * `T` - The target type to convert to.
500    ///
501    /// # Parameters
502    ///
503    /// * `options` - Conversion options forwarded to the shared converter.
504    ///
505    /// # Returns
506    ///
507    /// Returns the converted value on success.
508    ///
509    /// # Errors
510    ///
511    /// Returns a [`crate::ValueError`] when the value is missing, unsupported, or
512    /// invalid for `T` under the provided options.
513    #[inline]
514    pub fn to_with<T>(&self, options: &DataConversionOptions) -> ValueResult<T>
515    where
516        for<'a> DataConverter<'a>: DataConvertTo<T>,
517    {
518        super::value_converters::convert_with_data_converter_with(self, options)
519    }
520
521    /// Generic setter method
522    ///
523    /// Automatically selects the correct setter method based on the target
524    /// type and replaces the current value.
525    ///
526    /// This operation updates the stored type to `T` when needed. It does not
527    /// perform runtime type-mismatch validation against the previous variant.
528    ///
529    /// # Supported Generic Types
530    ///
531    /// `Value::set<T>(value)` currently supports the following `T`:
532    ///
533    /// - `bool`
534    /// - `char`
535    /// - `i8`, `i16`, `i32`, `i64`, `i128`
536    /// - `u8`, `u16`, `u32`, `u64`, `u128`
537    /// - `f32`, `f64`
538    /// - `String`, `&str`
539    /// - `NaiveDate`, `NaiveTime`, `NaiveDateTime`, `DateTime<Utc>`
540    /// - `BigInt`, `BigDecimal`
541    /// - `isize`, `usize`
542    /// - `Duration`
543    /// - `Url`
544    /// - `HashMap<String, String>`
545    /// - `serde_json::Value`
546    ///
547    /// # Type Parameters
548    ///
549    /// * `T` - The target type to set
550    ///
551    /// # Parameters
552    ///
553    /// * `value` - The value to set
554    ///
555    /// # Returns
556    ///
557    /// If setting succeeds, returns `Ok(())`; otherwise returns an error
558    ///
559    /// # Example
560    ///
561    /// ```rust
562    /// use qubit_datatype::DataType;
563    /// use qubit_value::Value;
564    ///
565    /// let mut value = Value::Empty(DataType::Int32);
566    ///
567    /// // Through type inference
568    /// value.set(42i32).unwrap();
569    /// assert_eq!(value.get_int32().unwrap(), 42);
570    ///
571    /// // Explicitly specify type parameter
572    /// value.set::<i32>(100).unwrap();
573    /// assert_eq!(value.get_int32().unwrap(), 100);
574    ///
575    /// // String type
576    /// let mut text = Value::Empty(DataType::String);
577    /// text.set("hello".to_string()).unwrap();
578    /// assert_eq!(text.get_string().unwrap(), "hello");
579    /// ```
580    #[inline]
581    pub fn set<T>(&mut self, value: T) -> ValueResult<()>
582    where
583        Self: ValueSetter<T>,
584    {
585        <Self as ValueSetter<T>>::set_value(self, value)
586    }
587
588    /// Get the data type of the value
589    ///
590    /// # Returns
591    ///
592    /// Returns the data type corresponding to this value
593    ///
594    /// # Example
595    ///
596    /// ```rust
597    /// use qubit_datatype::DataType;
598    /// use qubit_value::Value;
599    ///
600    /// let value = Value::Int32(42);
601    /// assert_eq!(value.data_type(), DataType::Int32);
602    ///
603    /// let empty = Value::Empty(DataType::String);
604    /// assert_eq!(empty.data_type(), DataType::String);
605    /// ```
606    #[inline]
607    pub fn data_type(&self) -> DataType {
608        match self {
609            Value::Empty(dt) => *dt,
610            Value::Bool(_) => DataType::Bool,
611            Value::Char(_) => DataType::Char,
612            Value::Int8(_) => DataType::Int8,
613            Value::Int16(_) => DataType::Int16,
614            Value::Int32(_) => DataType::Int32,
615            Value::Int64(_) => DataType::Int64,
616            Value::Int128(_) => DataType::Int128,
617            Value::UInt8(_) => DataType::UInt8,
618            Value::UInt16(_) => DataType::UInt16,
619            Value::UInt32(_) => DataType::UInt32,
620            Value::UInt64(_) => DataType::UInt64,
621            Value::UInt128(_) => DataType::UInt128,
622            Value::Float32(_) => DataType::Float32,
623            Value::Float64(_) => DataType::Float64,
624            Value::String(_) => DataType::String,
625            Value::Date(_) => DataType::Date,
626            Value::Time(_) => DataType::Time,
627            Value::DateTime(_) => DataType::DateTime,
628            Value::Instant(_) => DataType::Instant,
629            Value::BigInteger(_) => DataType::BigInteger,
630            Value::BigDecimal(_) => DataType::BigDecimal,
631            Value::IntSize(_) => DataType::IntSize,
632            Value::UIntSize(_) => DataType::UIntSize,
633            Value::Duration(_) => DataType::Duration,
634            Value::Url(_) => DataType::Url,
635            Value::StringMap(_) => DataType::StringMap,
636            Value::Json(_) => DataType::Json,
637        }
638    }
639
640    /// Check if the value is empty
641    ///
642    /// # Returns
643    ///
644    /// Returns `true` if the value is empty
645    ///
646    /// # Example
647    ///
648    /// ```rust
649    /// use qubit_datatype::DataType;
650    /// use qubit_value::Value;
651    ///
652    /// let value = Value::Int32(42);
653    /// assert!(!value.is_empty());
654    ///
655    /// let empty = Value::Empty(DataType::String);
656    /// assert!(empty.is_empty());
657    /// ```
658    #[inline]
659    pub fn is_empty(&self) -> bool {
660        matches!(self, Value::Empty(_))
661    }
662
663    /// Clear the value while preserving the type
664    ///
665    /// Sets the current value to empty but retains its data type.
666    ///
667    /// # Example
668    ///
669    /// ```rust
670    /// use qubit_datatype::DataType;
671    /// use qubit_value::Value;
672    ///
673    /// let mut value = Value::Int32(42);
674    /// value.clear();
675    /// assert!(value.is_empty());
676    /// assert_eq!(value.data_type(), DataType::Int32);
677    /// ```
678    #[inline]
679    pub fn clear(&mut self) {
680        let dt = self.data_type();
681        *self = Value::Empty(dt);
682    }
683
684    /// Set the data type
685    ///
686    /// If the new type differs from the current type, clears the value
687    /// and sets the new type.
688    ///
689    /// # Parameters
690    ///
691    /// * `data_type` - The data type to set
692    ///
693    /// # Example
694    ///
695    /// ```rust
696    /// use qubit_datatype::DataType;
697    /// use qubit_value::Value;
698    ///
699    /// let mut value = Value::Int32(42);
700    /// value.set_type(DataType::String);
701    /// assert!(value.is_empty());
702    /// assert_eq!(value.data_type(), DataType::String);
703    /// ```
704    #[inline]
705    pub fn set_type(&mut self, data_type: DataType) {
706        if self.data_type() != data_type {
707            *self = Value::Empty(data_type);
708        }
709    }
710}
711
712impl Default for Value {
713    #[inline]
714    fn default() -> Self {
715        Value::Empty(DataType::String)
716    }
717}