127 lines
2.9 KiB
Rust
127 lines
2.9 KiB
Rust
use jiff::civil::Date;
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
use zxq5_id_lib::Identifiable;
|
|
|
|
use crate::model::ModelElement;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum FieldType {
|
|
Image,
|
|
SingleLine,
|
|
MultiLine,
|
|
Date,
|
|
Number,
|
|
Link(Uuid),
|
|
Links(Uuid),
|
|
}
|
|
|
|
impl Default for FieldType {
|
|
fn default() -> Self {
|
|
Self::SingleLine
|
|
}
|
|
}
|
|
|
|
impl FieldType {
|
|
pub fn types() -> Vec<FieldType> {
|
|
vec![
|
|
FieldType::Image,
|
|
FieldType::SingleLine,
|
|
FieldType::MultiLine,
|
|
FieldType::Date,
|
|
FieldType::Number,
|
|
// FieldType::Link { template_id: None },
|
|
// FieldType::Links { template_id: None },
|
|
]
|
|
}
|
|
}
|
|
|
|
#[derive(Identifiable, Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Template {
|
|
#[uuid]
|
|
uuid: Uuid,
|
|
pub name: String,
|
|
|
|
pub description: String,
|
|
pub fields: Vec<FieldDefinition>,
|
|
}
|
|
|
|
impl ModelElement for Template {
|
|
const PROJECT_SUBDIR: &'static str = "templates";
|
|
|
|
fn copy(&self) -> Self {
|
|
Self {
|
|
uuid: Uuid::new_v4(),
|
|
name: format!("{} (Copy)", self.name),
|
|
description: self.description.clone(),
|
|
fields: self.fields.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for Template {
|
|
fn default() -> Self {
|
|
Self {
|
|
uuid: Uuid::new_v4(),
|
|
name: String::from("New template"),
|
|
|
|
description: String::new(),
|
|
fields: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct FieldDefinition {
|
|
pub name: String,
|
|
pub field_type: FieldType,
|
|
pub required: bool,
|
|
|
|
#[serde(default)]
|
|
pub on_preview: bool,
|
|
pub description: String,
|
|
}
|
|
|
|
impl Default for FieldDefinition {
|
|
fn default() -> Self {
|
|
Self {
|
|
name: String::new(),
|
|
description: String::new(),
|
|
field_type: FieldType::SingleLine,
|
|
required: true,
|
|
on_preview: true,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub enum FieldValue {
|
|
Image(String),
|
|
SingleLine(String),
|
|
MultiLine(String),
|
|
Date(Date),
|
|
Number(f64),
|
|
Link(Option<Uuid>),
|
|
Links(Vec<Uuid>),
|
|
}
|
|
|
|
impl FieldValue {
|
|
pub fn new_from(_type: &FieldType) -> Self {
|
|
match _type {
|
|
FieldType::Image => Self::Image(String::new()),
|
|
FieldType::SingleLine => Self::SingleLine(String::new()),
|
|
FieldType::MultiLine => Self::MultiLine(String::new()),
|
|
FieldType::Date => Self::Date(Date::new(1970, 1, 1).unwrap()),
|
|
FieldType::Number => Self::Number(0.0),
|
|
FieldType::Link(_) => Self::Link(None), // TODO!
|
|
FieldType::Links(_) => Self::Links(Vec::new()), // TODO!
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for FieldValue {
|
|
fn default() -> Self {
|
|
Self::SingleLine(String::new())
|
|
}
|
|
}
|