//! Data model structs and enums use serde::{Serialize, Deserialize}; use std::borrow::Cow; use super::ID; use super::data::TypedValue; use std::fmt::{Display, Formatter}; use std::fmt; /// 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 pub id : ID, /// Template name, unique within the database pub name: String, /// Parent object template ID pub parent_tpl_id: Option, } /// Relation between templates #[derive(Debug,Clone,Serialize,Deserialize)] pub struct RelationModel { /// PK pub id: ID, /// Object template ID pub object_tpl_id: 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_tpl_id: ID, } /// Property definition #[derive(Debug,Clone,Serialize,Deserialize)] pub struct PropertyModel { /// PK pub id: ID, /// Object or Reference template ID pub parent_tpl_id: 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) } }