Skip to main content

qubit_metadata/
metadata_value_type.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! [`MetadataValueType`] — JSON value classification for metadata.
10
11use std::fmt;
12
13use serde_json::Value;
14
15/// Coarse-grained JSON value types used by [`crate::MetadataError`] and inspection APIs.
16///
17/// `Metadata` stores arbitrary [`serde_json::Value`] instances, so it cannot
18/// recover the caller's original Rust type. `MetadataValueType` is therefore a
19/// JSON-level classification, analogous to the stricter `data_type()` concept
20/// in `qubit-value`, but tailored to an open-ended JSON model.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub enum MetadataValueType {
23    /// JSON `null`.
24    Null,
25    /// JSON boolean.
26    Bool,
27    /// JSON number.
28    Number,
29    /// JSON string.
30    String,
31    /// JSON array.
32    Array,
33    /// JSON object.
34    Object,
35}
36
37impl MetadataValueType {
38    /// Returns the JSON value type of `value`.
39    #[inline]
40    pub fn of(value: &Value) -> Self {
41        match value {
42            Value::Null => Self::Null,
43            Value::Bool(_) => Self::Bool,
44            Value::Number(_) => Self::Number,
45            Value::String(_) => Self::String,
46            Value::Array(_) => Self::Array,
47            Value::Object(_) => Self::Object,
48        }
49    }
50}
51
52impl From<&Value> for MetadataValueType {
53    #[inline]
54    fn from(value: &Value) -> Self {
55        Self::of(value)
56    }
57}
58
59impl fmt::Display for MetadataValueType {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        let text = match self {
62            Self::Null => "null",
63            Self::Bool => "bool",
64            Self::Number => "number",
65            Self::String => "string",
66            Self::Array => "array",
67            Self::Object => "object",
68        };
69        f.write_str(text)
70    }
71}