arri_repr/
values.rs

1use crate::{MetadataSchema, Serializable, serializer::Serializer};
2
3/// Represents a schema for values in an Arri schema.
4///
5/// This struct defines the values, optional metadata, and nullability
6/// associated with the schema.
7#[derive(Debug)]
8pub struct ValuesSchema {
9    /// The values associated with the schema, represented as a serializable object.
10    pub values: Box<dyn Serializable>,
11
12    /// Optional metadata associated with the schema.
13    pub metadata: Option<MetadataSchema>,
14
15    /// Indicates whether the schema allows null values. If `Some(true)`,
16    /// null values are permitted.
17    pub is_nullable: Option<bool>,
18}
19
20impl ValuesSchema {
21    /// Creates a new `ValuesSchema` instance with the specified values.
22    ///
23    /// # Arguments
24    ///
25    /// * `values` - A serializable object representing the values.
26    ///
27    /// # Returns
28    ///
29    /// A new `ValuesSchema` instance with default metadata and nullability.
30    pub fn new(values: Box<dyn Serializable>) -> Self {
31        Self {
32            values,
33            metadata: None,
34            is_nullable: None,
35        }
36    }
37}
38
39impl Serializable for ValuesSchema {
40    /// Serializes the `ValuesSchema` into a string representation.
41    ///
42    /// # Returns
43    ///
44    /// An `Option<String>` containing the serialized schema, or `None` if serialization fails.
45    fn serialize(&self) -> Option<String> {
46        Serializer::builder()
47            .set("values", &self.values)
48            .set("metadata", &self.metadata)
49            .set("isNullable", &self.is_nullable)
50            .build()
51            .into()
52    }
53
54    /// Sets the metadata for the schema.
55    ///
56    /// # Arguments
57    ///
58    /// * `metadata` - A `MetadataSchema` object to associate with the schema.
59    fn set_metadata(&mut self, metadata: MetadataSchema) {
60        self.metadata = Some(metadata);
61    }
62
63    /// Sets the nullability flag for the schema.
64    ///
65    /// # Arguments
66    ///
67    /// * `nullable` - A boolean indicating whether null values are allowed.
68    fn set_nullable(&mut self, nullable: bool) {
69        self.is_nullable = Some(nullable);
70    }
71}