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