parse_rs/
schema.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::collections::HashMap;
4
5/// Represents the possible data types for a field in a Parse class schema.
6#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
7pub enum FieldType {
8    String,
9    Number,
10    Boolean,
11    Date,
12    Object, // Generic JSON object
13    Array,
14    Pointer,
15    Relation,
16    File,
17    GeoPoint,
18    ACL,
19    Bytes,
20    Polygon,
21    // Note: Add other types if Parse Server supports more that are relevant for schema definition.
22}
23
24/// Represents the schema definition for a single field within a Parse class.
25#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
26pub struct FieldSchema {
27    /// The data type of the field.
28    #[serde(rename = "type")]
29    pub field_type: FieldType,
30
31    /// For `Pointer` and `Relation` types, this specifies the target class name.
32    #[serde(rename = "targetClass", skip_serializing_if = "Option::is_none")]
33    pub target_class: Option<String>,
34
35    /// Indicates if the field is required.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub required: Option<bool>,
38
39    /// The default value for the field.
40    #[serde(rename = "defaultValue", skip_serializing_if = "Option::is_none")]
41    pub default_value: Option<Value>,
42}
43
44/// Represents the Class Level Permissions (CLP) for a Parse class schema.
45#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
46#[serde(rename_all = "camelCase")]
47pub struct ClassLevelPermissionsSchema {
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub get: Option<HashMap<String, bool>>,
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub find: Option<HashMap<String, bool>>,
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub count: Option<HashMap<String, bool>>,
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub create: Option<HashMap<String, bool>>,
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub update: Option<HashMap<String, bool>>,
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub delete: Option<HashMap<String, bool>>,
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub add_field: Option<HashMap<String, bool>>,
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub read_user_fields: Option<Vec<String>>,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub write_user_fields: Option<Vec<String>>,
66}
67
68/// Represents the schema for a Parse class, including its fields, CLP, and indexes.
69#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
70#[serde(untagged)]
71pub enum IndexFieldType {
72    SortOrder(i32),
73    Text(String),
74    Other(Value),
75}
76
77#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
78#[serde(rename_all = "camelCase")]
79pub struct ParseSchema {
80    pub class_name: String,
81    pub fields: HashMap<String, FieldSchema>,
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub class_level_permissions: Option<ClassLevelPermissionsSchema>,
84    /// Indexes are represented as a map where the key is the index name
85    /// and the value is another map from field name to sort order (1 for asc, -1 for desc).
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub indexes: Option<HashMap<String, HashMap<String, IndexFieldType>>>,
88}
89
90/// Represents the response structure when fetching all schemas.
91#[derive(Debug, Deserialize, Clone)]
92pub struct GetAllSchemasResponse {
93    pub results: Vec<ParseSchema>,
94}