//! Data model structs and enums use std::fmt::{Display, Formatter}; use std::fmt; use serde::{Deserialize, Serialize}; use super::data::TypedValue; use super::ID; use crate::id::HaveId; /// Get a description of a struct pub trait Describe { /// Short but informative description for error messages fn describe(&self) -> String; } /// Object template #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ObjectModel { /// PK #[serde(default)] pub id: ID, /// Template name, unique within the database pub name: String, /// Parent object template ID pub parent: Option, } /// Relation between templates #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RelationModel { /// PK #[serde(default)] pub id: ID, /// Object template ID pub object: ID, /// Relation name, unique within the parent object pub name: String, /// Relation is optional pub optional: bool, /// Relation can be multiple pub multiple: bool, /// Related object template ID pub related: ID, } /// Property definition #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PropertyModel { /// PK #[serde(default)] pub id: ID, /// Object or Reference template ID pub object: ID, /// Property name, unique within the parent object or reference pub name: String, /// Property is optional pub optional: bool, /// Property can be multiple pub multiple: bool, /// Property data type pub data_type: DataType, /// Default value, used for newly created objects pub default: Option, } /// Value data type #[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)] pub enum DataType { /// Text String, /// Integer Integer, /// Floating point number Decimal, /// Boolean yes/no Boolean, } impl Display for ObjectModel { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "object \"{}\" ({})", self.name, self.id) } } impl Display for RelationModel { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "relation \"{}\" ({})", self.name, self.id) } } impl Display for PropertyModel { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "property \"{}\" ({})", self.name, self.id) } } impl HaveId for ObjectModel { fn get_id(&self) -> ID { self.id } } impl HaveId for RelationModel { fn get_id(&self) -> ID { self.id } } impl HaveId for PropertyModel { fn get_id(&self) -> ID { self.id } }