Skip to content

Commit 918db3d

Browse files
committed
feat: add default type option data from type
1 parent 699d6e3 commit 918db3d

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

collab-database/src/entity.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
#![allow(clippy::upper_case_acronyms)]
22
use crate::database::{gen_database_id, gen_database_view_id, gen_row_id, timestamp, DatabaseData};
33
use crate::error::DatabaseError;
4-
use crate::fields::Field;
4+
use crate::fields::checkbox_type_option::CheckboxTypeOption;
5+
use crate::fields::checklist_type_option::ChecklistTypeOption;
6+
use crate::fields::date_type_option::{DateTypeOption, TimeTypeOption};
7+
use crate::fields::media_type_option::MediaTypeOption;
8+
use crate::fields::number_type_option::NumberTypeOption;
9+
use crate::fields::relation_type_option::RelationTypeOption;
10+
use crate::fields::select_type_option::{MultiSelectTypeOption, SingleSelectTypeOption};
11+
use crate::fields::summary_type_option::SummarizationTypeOption;
12+
use crate::fields::text_type_option::RichTextTypeOption;
13+
use crate::fields::timestamp_type_option::TimestampTypeOption;
14+
use crate::fields::translate_type_option::TranslateTypeOption;
15+
use crate::fields::url_type_option::URLTypeOption;
16+
use crate::fields::{Field, TypeOptionData};
517
use crate::rows::CreateRowParams;
618
use crate::views::{
719
DatabaseLayout, FieldOrder, FieldSettingsByFieldIdMap, FieldSettingsMap, FilterMap,
@@ -323,6 +335,29 @@ impl From<i64> for FieldType {
323335
}
324336
}
325337

338+
pub fn default_type_option_data_from_type(field_type: FieldType) -> TypeOptionData {
339+
match field_type {
340+
FieldType::RichText => RichTextTypeOption.into(),
341+
FieldType::Number => NumberTypeOption::default().into(),
342+
FieldType::DateTime => DateTypeOption::default().into(),
343+
FieldType::LastEditedTime | FieldType::CreatedTime => TimestampTypeOption {
344+
field_type: field_type.into(),
345+
..Default::default()
346+
}
347+
.into(),
348+
FieldType::SingleSelect => SingleSelectTypeOption::default().into(),
349+
FieldType::MultiSelect => MultiSelectTypeOption::default().into(),
350+
FieldType::Checkbox => CheckboxTypeOption.into(),
351+
FieldType::URL => URLTypeOption::default().into(),
352+
FieldType::Time => TimeTypeOption.into(),
353+
FieldType::Media => MediaTypeOption::default().into(),
354+
FieldType::Checklist => ChecklistTypeOption.into(),
355+
FieldType::Relation => RelationTypeOption::default().into(),
356+
FieldType::Summary => SummarizationTypeOption::default().into(),
357+
FieldType::Translate => TranslateTypeOption::default().into(),
358+
}
359+
}
360+
326361
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
327362
#[repr(u8)]
328363
pub enum FileUploadType {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use super::{TypeOptionData, TypeOptionDataBuilder};
4+
5+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6+
pub struct ChecklistTypeOption;
7+
8+
impl From<TypeOptionData> for ChecklistTypeOption {
9+
fn from(_data: TypeOptionData) -> Self {
10+
Self
11+
}
12+
}
13+
14+
impl From<ChecklistTypeOption> for TypeOptionData {
15+
fn from(_data: ChecklistTypeOption) -> Self {
16+
TypeOptionDataBuilder::default()
17+
}
18+
}

collab-database/src/fields/type_option/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
pub mod checkbox_type_option;
2+
pub mod checklist_type_option;
23
pub mod date_type_option;
34
pub mod media_type_option;
45
pub mod number_type_option;
6+
pub mod relation_type_option;
57
pub mod select_type_option;
8+
pub mod summary_type_option;
69
pub mod text_type_option;
710
pub mod timestamp_type_option;
11+
pub mod translate_type_option;
812
pub mod url_type_option;
913

1014
use std::collections::HashMap;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use collab::util::AnyMapExt;
2+
use serde::{Deserialize, Serialize};
3+
4+
use super::{TypeOptionData, TypeOptionDataBuilder};
5+
6+
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7+
pub struct RelationTypeOption {
8+
pub database_id: String,
9+
}
10+
11+
impl From<TypeOptionData> for RelationTypeOption {
12+
fn from(data: TypeOptionData) -> Self {
13+
let database_id: String = data.get_as("database_id").unwrap_or_default();
14+
Self { database_id }
15+
}
16+
}
17+
18+
impl From<RelationTypeOption> for TypeOptionData {
19+
fn from(data: RelationTypeOption) -> Self {
20+
TypeOptionDataBuilder::from([("database_id".into(), data.database_id.into())])
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use collab::util::AnyMapExt;
2+
use serde::{Deserialize, Serialize};
3+
4+
use super::{TypeOptionData, TypeOptionDataBuilder};
5+
6+
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7+
pub struct SummarizationTypeOption {
8+
pub auto_fill: bool,
9+
}
10+
11+
impl From<TypeOptionData> for SummarizationTypeOption {
12+
fn from(data: TypeOptionData) -> Self {
13+
let auto_fill: bool = data.get_as("auto_fill").unwrap_or_default();
14+
Self { auto_fill }
15+
}
16+
}
17+
18+
impl From<SummarizationTypeOption> for TypeOptionData {
19+
fn from(data: SummarizationTypeOption) -> Self {
20+
TypeOptionDataBuilder::from([("auto_fill".into(), data.auto_fill.into())])
21+
}
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use serde::{Deserialize, Serialize};
2+
use yrs::{encoding::serde::from_any, Any};
3+
4+
use super::{TypeOptionData, TypeOptionDataBuilder};
5+
6+
#[derive(Debug, Clone, Serialize, Deserialize)]
7+
pub struct TranslateTypeOption {
8+
#[serde(default)]
9+
pub auto_fill: bool,
10+
/// Use [TranslateTypeOption::language_from_type] to get the language name
11+
#[serde(default, rename = "language")]
12+
pub language_type: i64,
13+
}
14+
15+
impl TranslateTypeOption {
16+
pub fn language_from_type(language_type: i64) -> &'static str {
17+
match language_type {
18+
0 => "Traditional Chinese",
19+
1 => "English",
20+
2 => "French",
21+
3 => "German",
22+
4 => "Hindi",
23+
5 => "Spanish",
24+
6 => "Portuguese",
25+
7 => "Standard Arabic",
26+
8 => "Simplified Chinese",
27+
_ => "English",
28+
}
29+
}
30+
}
31+
32+
impl Default for TranslateTypeOption {
33+
fn default() -> Self {
34+
Self {
35+
auto_fill: false,
36+
language_type: 1,
37+
}
38+
}
39+
}
40+
41+
impl From<TypeOptionData> for TranslateTypeOption {
42+
fn from(data: TypeOptionData) -> Self {
43+
from_any(&Any::from(data)).unwrap()
44+
}
45+
}
46+
47+
impl From<TranslateTypeOption> for TypeOptionData {
48+
fn from(value: TranslateTypeOption) -> Self {
49+
TypeOptionDataBuilder::from([
50+
("auto_fill".into(), value.auto_fill.into()),
51+
("language".into(), Any::BigInt(value.language_type)),
52+
])
53+
}
54+
}

0 commit comments

Comments
 (0)