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;
40use crate::{
41 IntoValueDefault,
42 ValueError,
43};
44
45/// Single value container
46///
47/// Uses an enum to represent different types of values, providing
48/// type-safe value storage and access.
49///
50/// # Features
51///
52/// - Zero-cost abstraction with compile-time type checking
53/// - Supports multiple basic data types
54/// - Provides two sets of APIs for type checking and type conversion
55/// - Automatic memory management
56///
57/// # Example
58///
59/// ```rust
60/// use qubit_value::Value;
61///
62/// // Create an integer value
63/// let value = Value::Int32(42);
64/// assert_eq!(value.get_int32().unwrap(), 42);
65///
66/// // Type conversion
67/// let converted = value.to::<i64>().unwrap();
68/// assert_eq!(converted, 42i64);
69///
70/// // String value
71/// let text = Value::String("hello".to_string());
72/// assert_eq!(text.get_string().unwrap(), "hello");
73/// ```
74///
75///
76#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
77pub enum Value {
78 /// Empty value (has type but no value)
79 Empty(DataType),
80 /// Boolean value
81 Bool(bool),
82 /// Character value
83 Char(char),
84 /// 8-bit signed integer
85 Int8(i8),
86 /// 16-bit signed integer
87 Int16(i16),
88 /// 32-bit signed integer
89 Int32(i32),
90 /// 64-bit signed integer
91 Int64(i64),
92 /// 128-bit signed integer
93 Int128(i128),
94 /// 8-bit unsigned integer
95 UInt8(u8),
96 /// 16-bit unsigned integer
97 UInt16(u16),
98 /// 32-bit unsigned integer
99 UInt32(u32),
100 /// 64-bit unsigned integer
101 UInt64(u64),
102 /// 128-bit unsigned integer
103 UInt128(u128),
104 /// Platform-dependent signed integer (isize)
105 IntSize(isize),
106 /// Platform-dependent unsigned integer (usize)
107 UIntSize(usize),
108 /// 32-bit floating point number
109 Float32(f32),
110 /// 64-bit floating point number
111 Float64(f64),
112 /// Big integer type
113 BigInteger(BigInt),
114 /// Big decimal type
115 BigDecimal(BigDecimal),
116 /// String
117 String(String),
118 /// Date
119 Date(NaiveDate),
120 /// Time
121 Time(NaiveTime),
122 /// Date and time
123 DateTime(NaiveDateTime),
124 /// UTC instant
125 Instant(DateTime<Utc>),
126 /// Duration type (std::time::Duration)
127 Duration(Duration),
128 /// URL type (url::Url)
129 Url(Url),
130 /// String map type (HashMap<String, String>)
131 StringMap(HashMap<String, String>),
132 /// JSON value type (serde_json::Value)
133 Json(serde_json::Value),
134}
135
136use super::value_constructor::ValueConstructor;
137use super::value_converter::ValueConverter;
138use super::value_getter::ValueGetter;
139use 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 getter method with a default value.
285 ///
286 /// Returns the supplied default only when this value is empty. Type
287 /// mismatches and conversion errors are still returned as errors.
288 #[inline]
289 pub fn get_or<T>(&self, default: impl IntoValueDefault<T>) -> ValueResult<T>
290 where
291 Self: ValueGetter<T>,
292 {
293 match self.get() {
294 Err(ValueError::NoValue) => Ok(default.into_value_default()),
295 result => result,
296 }
297 }
298
299 /// Generic conversion method
300 ///
301 /// Converts the current value to the target type according to the shared
302 /// value conversion rules.
303 ///
304 /// # Supported Target Types And Source Variants
305 ///
306 /// `Value::to<T>()` currently supports the following target types:
307 ///
308 /// - `bool`
309 /// - `Value::Bool`
310 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
311 /// `Value::Int128`
312 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
313 /// `Value::UInt64`, `Value::UInt128`
314 /// - `Value::String`, parsed as `1`, `0`, or ASCII case-insensitive
315 /// `true` / `false`
316 /// - `char`
317 /// - `Value::Char`
318 /// - `i8`
319 /// - `Value::Int8`
320 /// - `Value::Bool`
321 /// - `Value::Char`
322 /// - all integer variants
323 /// - `Value::Float32`, `Value::Float64`
324 /// - `Value::String`, parsed as `i8`
325 /// - `Value::BigInteger`, `Value::BigDecimal`
326 /// - `i16`
327 /// - `Value::Int16`
328 /// - `Value::Bool`
329 /// - `Value::Char`
330 /// - all integer variants
331 /// - `Value::Float32`, `Value::Float64`
332 /// - `Value::String`, parsed as `i16`
333 /// - `Value::BigInteger`, `Value::BigDecimal`
334 /// - `i32`
335 /// - `Value::Int32`
336 /// - `Value::Bool`
337 /// - `Value::Char`
338 /// - `Value::Int8`, `Value::Int16`, `Value::Int64`, `Value::Int128`
339 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
340 /// `Value::UInt64`, `Value::UInt128`
341 /// - `Value::Float32`, `Value::Float64`
342 /// - `Value::String`, parsed as `i32`
343 /// - `Value::BigInteger`, `Value::BigDecimal`
344 /// - `i64`
345 /// - `Value::Int64`
346 /// - `Value::Bool`
347 /// - `Value::Char`
348 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int128`
349 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
350 /// `Value::UInt64`, `Value::UInt128`
351 /// - `Value::Float32`, `Value::Float64`
352 /// - `Value::String`, parsed as `i64`
353 /// - `Value::BigInteger`, `Value::BigDecimal`
354 /// - `i128`
355 /// - `Value::Int128`
356 /// - `Value::Bool`
357 /// - `Value::Char`
358 /// - all integer variants
359 /// - `Value::Float32`, `Value::Float64`
360 /// - `Value::String`, parsed as `i128`
361 /// - `Value::BigInteger`, `Value::BigDecimal`
362 /// - `u8`
363 /// - `Value::UInt8`
364 /// - `Value::Bool`
365 /// - `Value::Char`
366 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
367 /// `Value::Int128`
368 /// - `Value::UInt16`, `Value::UInt32`, `Value::UInt64`,
369 /// `Value::UInt128`
370 /// - `Value::String`, parsed as `u8`
371 /// - `u16`
372 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
373 /// `Value::UInt64`, `Value::UInt128`
374 /// - `Value::Bool`
375 /// - `Value::Char`
376 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
377 /// `Value::Int128`
378 /// - `Value::String`, parsed as `u16`
379 /// - `u32`
380 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
381 /// `Value::UInt64`, `Value::UInt128`
382 /// - `Value::Bool`
383 /// - `Value::Char`
384 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
385 /// `Value::Int128`
386 /// - `Value::String`, parsed as `u32`
387 /// - `u64`
388 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
389 /// `Value::UInt64`, `Value::UInt128`
390 /// - `Value::Bool`
391 /// - `Value::Char`
392 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
393 /// `Value::Int128`
394 /// - `Value::String`, parsed as `u64`
395 /// - `u128`
396 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
397 /// `Value::UInt64`, `Value::UInt128`
398 /// - `Value::Bool`
399 /// - `Value::Char`
400 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
401 /// `Value::Int128`
402 /// - `Value::String`, parsed as `u128`
403 /// - `f32`
404 /// - `Value::Float32`, `Value::Float64`
405 /// - `Value::Bool`
406 /// - `Value::Char`
407 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
408 /// `Value::Int128`
409 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
410 /// `Value::UInt64`, `Value::UInt128`
411 /// - `Value::String`, parsed as `f32`
412 /// - `Value::BigInteger`, `Value::BigDecimal`
413 /// - `f64`
414 /// - `Value::Float64`
415 /// - `Value::Bool`
416 /// - `Value::Char`
417 /// - `Value::Int8`, `Value::Int16`, `Value::Int32`, `Value::Int64`,
418 /// `Value::Int128`
419 /// - `Value::UInt8`, `Value::UInt16`, `Value::UInt32`,
420 /// `Value::UInt64`, `Value::UInt128`
421 /// - `Value::Float32`
422 /// - `Value::String`, parsed as `f64`
423 /// - `Value::BigInteger`, `Value::BigDecimal`
424 /// - `String`
425 /// - `Value::String`
426 /// - `Value::Bool`, `Value::Char`
427 /// - all integer and floating-point variants
428 /// - `Value::Date`, `Value::Time`, `Value::DateTime`, `Value::Instant`
429 /// - `Value::BigInteger`, `Value::BigDecimal`
430 /// - `Value::IntSize`, `Value::UIntSize`
431 /// - `Value::Duration`, formatted as `<nanoseconds>ns`
432 /// - `Value::Url`
433 /// - `Value::StringMap`, serialized as JSON text
434 /// - `Value::Json`, serialized as JSON text
435 /// - `NaiveDate`
436 /// - `Value::Date`
437 /// - `NaiveTime`
438 /// - `Value::Time`
439 /// - `NaiveDateTime`
440 /// - `Value::DateTime`
441 /// - `DateTime<Utc>`
442 /// - `Value::Instant`
443 /// - `BigInt`
444 /// - `Value::BigInteger`
445 /// - `BigDecimal`
446 /// - `Value::BigDecimal`
447 /// - `isize`
448 /// - `Value::IntSize`
449 /// - `Value::Bool`
450 /// - `Value::Char`
451 /// - all integer variants
452 /// - `Value::Float32`, `Value::Float64`
453 /// - `Value::String`, parsed as `isize`
454 /// - `Value::BigInteger`, `Value::BigDecimal`
455 /// - `usize`
456 /// - `Value::UIntSize`
457 /// - `Value::Bool`
458 /// - `Value::Char`
459 /// - all integer variants
460 /// - `Value::String`, parsed as `usize`
461 /// - `Duration`
462 /// - `Value::Duration`
463 /// - `Value::String`, parsed from `<nanoseconds>ns`
464 /// - `Url`
465 /// - `Value::Url`
466 /// - `Value::String`, parsed as URL text
467 /// - `HashMap<String, String>`
468 /// - `Value::StringMap`
469 /// - `serde_json::Value`
470 /// - `Value::Json`
471 /// - `Value::String`, parsed as JSON text
472 /// - `Value::StringMap`, converted to a JSON object
473 ///
474 /// Any target type not listed above is not supported by `Value::to<T>()`.
475 ///
476 /// # Type Parameters
477 ///
478 /// * `T` - The target type to convert to
479 ///
480 /// # Returns
481 ///
482 /// Returns the converted value on success, or an error if conversion is not
483 /// supported or fails.
484 ///
485 /// # Example
486 ///
487 /// ```rust
488 /// use qubit_value::Value;
489 ///
490 /// let value = Value::Int32(42);
491 ///
492 /// let num: i64 = value.to().unwrap();
493 /// assert_eq!(num, 42);
494 ///
495 /// let text: String = value.to().unwrap();
496 /// assert_eq!(text, "42");
497 /// ```
498 #[inline]
499 pub fn to<T>(&self) -> ValueResult<T>
500 where
501 Self: ValueConverter<T>,
502 {
503 <Self as ValueConverter<T>>::convert(self)
504 }
505
506 /// Converts this value to `T`, or returns `default` when it is empty.
507 ///
508 /// Conversion failures from non-empty values are preserved.
509 #[inline]
510 pub fn to_or<T>(&self, default: impl IntoValueDefault<T>) -> ValueResult<T>
511 where
512 Self: ValueConverter<T>,
513 {
514 match self.to() {
515 Err(ValueError::NoValue) => Ok(default.into_value_default()),
516 result => result,
517 }
518 }
519
520 /// Converts this value to `T` using the provided conversion options.
521 ///
522 /// This method uses the shared [`qubit_datatype`] conversion layer directly,
523 /// so options such as string trimming, blank string handling, and boolean
524 /// aliases are applied consistently with other value containers.
525 ///
526 /// # Type Parameters
527 ///
528 /// * `T` - The target type to convert to.
529 ///
530 /// # Parameters
531 ///
532 /// * `options` - Conversion options forwarded to the shared converter.
533 ///
534 /// # Returns
535 ///
536 /// Returns the converted value on success.
537 ///
538 /// # Errors
539 ///
540 /// Returns a [`crate::ValueError`] when the value is missing, unsupported, or
541 /// invalid for `T` under the provided options.
542 #[inline]
543 pub fn to_with<T>(&self, options: &DataConversionOptions) -> ValueResult<T>
544 where
545 for<'a> DataConverter<'a>: DataConvertTo<T>,
546 {
547 super::value_converters::convert_with_data_converter_with(self, options)
548 }
549
550 /// Converts this value to `T` using conversion options, or returns
551 /// `default` when it is empty.
552 ///
553 /// Conversion failures from non-empty values are preserved.
554 #[inline]
555 pub fn to_or_with<T>(
556 &self,
557 default: impl IntoValueDefault<T>,
558 options: &DataConversionOptions,
559 ) -> ValueResult<T>
560 where
561 for<'a> DataConverter<'a>: DataConvertTo<T>,
562 {
563 match self.to_with(options) {
564 Err(ValueError::NoValue) => Ok(default.into_value_default()),
565 result => result,
566 }
567 }
568
569 /// Generic setter method
570 ///
571 /// Automatically selects the correct setter method based on the target
572 /// type and replaces the current value.
573 ///
574 /// This operation updates the stored type to `T` when needed. It does not
575 /// perform runtime type-mismatch validation against the previous variant.
576 ///
577 /// # Supported Generic Types
578 ///
579 /// `Value::set<T>(value)` currently supports the following `T`:
580 ///
581 /// - `bool`
582 /// - `char`
583 /// - `i8`, `i16`, `i32`, `i64`, `i128`
584 /// - `u8`, `u16`, `u32`, `u64`, `u128`
585 /// - `f32`, `f64`
586 /// - `String`, `&str`
587 /// - `NaiveDate`, `NaiveTime`, `NaiveDateTime`, `DateTime<Utc>`
588 /// - `BigInt`, `BigDecimal`
589 /// - `isize`, `usize`
590 /// - `Duration`
591 /// - `Url`
592 /// - `HashMap<String, String>`
593 /// - `serde_json::Value`
594 ///
595 /// # Type Parameters
596 ///
597 /// * `T` - The target type to set
598 ///
599 /// # Parameters
600 ///
601 /// * `value` - The value to set
602 ///
603 /// # Returns
604 ///
605 /// If setting succeeds, returns `Ok(())`; otherwise returns an error
606 ///
607 /// # Example
608 ///
609 /// ```rust
610 /// use qubit_datatype::DataType;
611 /// use qubit_value::Value;
612 ///
613 /// let mut value = Value::Empty(DataType::Int32);
614 ///
615 /// // Through type inference
616 /// value.set(42i32).unwrap();
617 /// assert_eq!(value.get_int32().unwrap(), 42);
618 ///
619 /// // Explicitly specify type parameter
620 /// value.set::<i32>(100).unwrap();
621 /// assert_eq!(value.get_int32().unwrap(), 100);
622 ///
623 /// // String type
624 /// let mut text = Value::Empty(DataType::String);
625 /// text.set("hello".to_string()).unwrap();
626 /// assert_eq!(text.get_string().unwrap(), "hello");
627 /// ```
628 #[inline]
629 pub fn set<T>(&mut self, value: T) -> ValueResult<()>
630 where
631 Self: ValueSetter<T>,
632 {
633 <Self as ValueSetter<T>>::set_value(self, value)
634 }
635
636 /// Get the data type of the value
637 ///
638 /// # Returns
639 ///
640 /// Returns the data type corresponding to this value
641 ///
642 /// # Example
643 ///
644 /// ```rust
645 /// use qubit_datatype::DataType;
646 /// use qubit_value::Value;
647 ///
648 /// let value = Value::Int32(42);
649 /// assert_eq!(value.data_type(), DataType::Int32);
650 ///
651 /// let empty = Value::Empty(DataType::String);
652 /// assert_eq!(empty.data_type(), DataType::String);
653 /// ```
654 #[inline]
655 pub fn data_type(&self) -> DataType {
656 match self {
657 Value::Empty(dt) => *dt,
658 Value::Bool(_) => DataType::Bool,
659 Value::Char(_) => DataType::Char,
660 Value::Int8(_) => DataType::Int8,
661 Value::Int16(_) => DataType::Int16,
662 Value::Int32(_) => DataType::Int32,
663 Value::Int64(_) => DataType::Int64,
664 Value::Int128(_) => DataType::Int128,
665 Value::UInt8(_) => DataType::UInt8,
666 Value::UInt16(_) => DataType::UInt16,
667 Value::UInt32(_) => DataType::UInt32,
668 Value::UInt64(_) => DataType::UInt64,
669 Value::UInt128(_) => DataType::UInt128,
670 Value::Float32(_) => DataType::Float32,
671 Value::Float64(_) => DataType::Float64,
672 Value::String(_) => DataType::String,
673 Value::Date(_) => DataType::Date,
674 Value::Time(_) => DataType::Time,
675 Value::DateTime(_) => DataType::DateTime,
676 Value::Instant(_) => DataType::Instant,
677 Value::BigInteger(_) => DataType::BigInteger,
678 Value::BigDecimal(_) => DataType::BigDecimal,
679 Value::IntSize(_) => DataType::IntSize,
680 Value::UIntSize(_) => DataType::UIntSize,
681 Value::Duration(_) => DataType::Duration,
682 Value::Url(_) => DataType::Url,
683 Value::StringMap(_) => DataType::StringMap,
684 Value::Json(_) => DataType::Json,
685 }
686 }
687
688 /// Check if the value is empty
689 ///
690 /// # Returns
691 ///
692 /// Returns `true` if the value is empty
693 ///
694 /// # Example
695 ///
696 /// ```rust
697 /// use qubit_datatype::DataType;
698 /// use qubit_value::Value;
699 ///
700 /// let value = Value::Int32(42);
701 /// assert!(!value.is_empty());
702 ///
703 /// let empty = Value::Empty(DataType::String);
704 /// assert!(empty.is_empty());
705 /// ```
706 #[inline]
707 pub fn is_empty(&self) -> bool {
708 matches!(self, Value::Empty(_))
709 }
710
711 /// Clear the value while preserving the type
712 ///
713 /// Sets the current value to empty but retains its data type.
714 ///
715 /// # Example
716 ///
717 /// ```rust
718 /// use qubit_datatype::DataType;
719 /// use qubit_value::Value;
720 ///
721 /// let mut value = Value::Int32(42);
722 /// value.clear();
723 /// assert!(value.is_empty());
724 /// assert_eq!(value.data_type(), DataType::Int32);
725 /// ```
726 #[inline]
727 pub fn clear(&mut self) {
728 let dt = self.data_type();
729 *self = Value::Empty(dt);
730 }
731
732 /// Set the data type
733 ///
734 /// If the new type differs from the current type, clears the value
735 /// and sets the new type.
736 ///
737 /// # Parameters
738 ///
739 /// * `data_type` - The data type to set
740 ///
741 /// # Example
742 ///
743 /// ```rust
744 /// use qubit_datatype::DataType;
745 /// use qubit_value::Value;
746 ///
747 /// let mut value = Value::Int32(42);
748 /// value.set_type(DataType::String);
749 /// assert!(value.is_empty());
750 /// assert_eq!(value.data_type(), DataType::String);
751 /// ```
752 #[inline]
753 pub fn set_type(&mut self, data_type: DataType) {
754 if self.data_type() != data_type {
755 *self = Value::Empty(data_type);
756 }
757 }
758}
759
760impl Default for Value {
761 #[inline]
762 fn default() -> Self {
763 Value::Empty(DataType::String)
764 }
765}