use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
pub struct AdminSchema {
pub entities: Vec<EntitySchema>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
pub struct EntitySchema {
pub name: String,
pub label: String,
pub fields: Vec<FieldSchema>,
pub read_capability: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
pub struct FieldSchema {
pub name: String,
pub label: String,
pub field_type: FieldType,
#[serde(default)]
pub nullable: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum FieldType {
String,
Integer,
Boolean,
Timestamp,
Json,
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> AdminSchema {
AdminSchema {
entities: vec![EntitySchema {
name: "users".to_owned(),
label: "Users".to_owned(),
read_capability: "identity.users.read".to_owned(),
fields: vec![
FieldSchema {
name: "email".into(),
label: "Email".into(),
field_type: FieldType::String,
nullable: false,
},
FieldSchema {
name: "created_at".into(),
label: "Created".into(),
field_type: FieldType::Timestamp,
nullable: false,
},
],
}],
}
}
#[test]
fn admin_schema_round_trips_through_json() {
let schema = sample();
let json = serde_json::to_string(&schema).expect("serialize");
let back: AdminSchema = serde_json::from_str(&json).expect("deserialize");
assert_eq!(schema, back);
}
#[test]
fn field_type_serializes_with_kind_tag() {
let json = serde_json::to_string(&FieldType::Timestamp).expect("serialize");
assert_eq!(json, r#"{"kind":"timestamp"}"#);
}
}