arri_repr/
type.rs

1use crate::{MetadataSchema, Serializable};
2use ronky_derive::Serializable as SerializableDerive;
3
4/// Represents a schema for a type in an Arri schema.
5///
6/// This struct defines the type, optional metadata, and nullability
7/// associated with the schema.
8#[derive(Debug, PartialEq, Eq, SerializableDerive)]
9pub struct TypeSchema {
10    /// The type of the schema.
11    r#type: Types,
12
13    /// Optional metadata associated with the schema.
14    metadata: Option<MetadataSchema>,
15
16    /// Indicates whether the schema allows null values. If `Some(true)`,
17    /// null values are permitted.
18    is_nullable: Option<bool>,
19}
20
21impl TypeSchema {
22    /// Creates a new `TypeSchema` instance with the specified type.
23    ///
24    /// # Arguments
25    ///
26    /// * `r#type` - The type of the schema.
27    ///
28    /// # Returns
29    ///
30    /// A new `TypeSchema` instance with default values for metadata and nullability.
31    pub fn new(r#type: Types) -> Self {
32        Self {
33            r#type,
34            metadata: None,
35            is_nullable: None,
36        }
37    }
38}
39
40#[derive(Debug, PartialEq, Eq)]
41pub enum Types {
42    String,
43    Boolean,
44    Timestamp,
45    Float32,
46    Float64,
47    Int8,
48    Uint8,
49    Int16,
50    Uint16,
51    Int32,
52    Uint32,
53    Int64,
54    Uint64,
55}
56
57impl Serializable for Types {
58    fn serialize(&self) -> Option<String> {
59        (match self {
60            Self::String => "string",
61            Self::Boolean => "boolean",
62            Self::Timestamp => "timestamp",
63            Self::Float32 => "float32",
64            Self::Float64 => "float64",
65            Self::Int8 => "int8",
66            Self::Uint8 => "uint8",
67            Self::Int16 => "int16",
68            Self::Uint16 => "uint16",
69            Self::Int32 => "int32",
70            Self::Uint32 => "uint32",
71            Self::Int64 => "int64",
72            Self::Uint64 => "uint64",
73        })
74        .serialize()
75    }
76}