rapido_core/component/
mod.rs1use std::collections::HashMap;
2
3use sea_query::{
4 ColumnDef, ColumnType, Iden, IntoIden, StringLen, Table, TableCreateStatement,
5 TableDropStatement,
6};
7use serde::{Deserialize, Serialize};
8
9pub mod attribute;
10use attribute::Attribute;
11
12#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
13pub struct CollectionName(String);
14impl Iden for CollectionName {
15 fn unquoted(&self, s: &mut dyn std::fmt::Write) {
16 write!(s, "tbl_{}", self.0).unwrap()
17 }
18}
19
20#[derive(Debug, Deserialize, Clone, Serialize, PartialEq, Eq)]
22pub struct Component {
23 #[serde(rename = "collectionName")]
25 pub collection_name: CollectionName,
26
27 pub info: Info,
28 pub options: Options,
29
30 pub attributes: Attributes,
32}
33
34impl Component {
35 pub fn into_table_create_statement(&self) -> TableCreateStatement {
37 let mut stmt = Table::create();
38
39 stmt.table(self.collection_name.clone().into_iden())
40 .if_not_exists();
41
42 for column_attribute in self.attributes.into_column_defs() {
43 stmt.col(column_attribute);
44 }
45 stmt
46 }
47
48 pub fn into_table_drop_statement(&self) -> TableDropStatement {
50 Table::drop()
51 .table(self.collection_name.clone().into_iden())
52 .if_exists()
53 .to_owned()
54 }
55}
56
57#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash, Serialize)]
58pub struct ColName(String);
59impl Iden for ColName {
60 fn unquoted(&self, s: &mut dyn std::fmt::Write) {
61 write!(s, "{}", self.0).unwrap()
62 }
63}
64
65#[derive(Debug, Deserialize, Clone, Serialize, PartialEq, Eq)]
67pub struct Attributes(HashMap<ColName, Attribute>);
68impl Attributes {
69 pub fn into_column_defs(&self) -> Vec<ColumnDef> {
71 self.0
72 .iter()
73 .map(|(col_name, col_attribute)| {
74 let name = col_name.clone().into_iden();
75 let types = col_attribute.into_column_type();
76
77 ColumnDef::new_with_type(name, types)
78 })
79 .collect()
80 }
81}
82
83#[derive(Debug, Deserialize, Clone, Serialize, PartialEq, Eq)]
84pub struct Options {}
85
86#[derive(Debug, Deserialize, Clone, Serialize, PartialEq, Eq)]
87pub struct Info {}