a small relational database with user-editable schema for manual data entry
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
yopa/yopa/src/model.rs

115 lines
2.6 KiB

//! 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,
}
/// 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 name when viewed from the other side, unique within the related object's relations
pub reciprocal_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<TypedValue>,
}
/// 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
}
}