Skip to main content

qubit_metadata/schema/
metadata_field.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! [`MetadataField`] — one field definition in a metadata schema.
9
10use qubit_datatype::DataType;
11use serde::Deserialize;
12use serde::Serialize;
13
14/// Definition of one metadata field in a [`crate::MetadataSchema`].
15///
16/// # Examples
17///
18/// ```
19/// use qubit_datatype::DataType;
20/// use qubit_metadata::MetadataField;
21///
22/// let field = MetadataField::new(DataType::String, true);
23/// assert!(field.is_required());
24/// ```
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(deny_unknown_fields)]
27pub struct MetadataField {
28    /// Runtime data type of this field.
29    data_type: DataType,
30    /// Whether this field must be present when validating metadata.
31    required: bool,
32}
33
34impl MetadataField {
35    /// Creates a field definition.
36    ///
37    /// # Parameters
38    ///
39    /// * `data_type` - Concrete data type accepted by the field.
40    /// * `required` - Whether metadata must provide a concrete value.
41    ///
42    /// # Returns
43    ///
44    /// A new field definition.
45    #[inline(always)]
46    #[must_use = "the constructed metadata field should be used"]
47    pub fn new(data_type: DataType, required: bool) -> Self {
48        Self { data_type, required }
49    }
50
51    /// Returns the runtime data type of this field.
52    ///
53    /// # Returns
54    ///
55    /// The declared field data type.
56    #[inline(always)]
57    #[must_use = "the metadata field type should be inspected"]
58    pub fn data_type(&self) -> DataType {
59        self.data_type
60    }
61
62    /// Returns `true` when this field is required.
63    ///
64    /// # Returns
65    ///
66    /// `true` when validation requires a concrete value for this field.
67    #[inline(always)]
68    #[must_use]
69    pub fn is_required(&self) -> bool {
70        self.required
71    }
72}