|
| 1 | +use super::{TypeOptionData, TypeOptionDataBuilder}; |
| 2 | +use crate::database::entity::FieldType; |
| 3 | +use crate::database::fields::{TypeOptionCellReader, TypeOptionCellWriter}; |
| 4 | +use crate::database::rows::{Cell, new_cell_builder}; |
| 5 | +use crate::database::template::entity::CELL_DATA; |
| 6 | +use crate::util::AnyMapExt; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use serde_json::{Value, json}; |
| 9 | +use serde_repr::{Deserialize_repr, Serialize_repr}; |
| 10 | +use yrs::Any; |
| 11 | + |
| 12 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize_repr, Deserialize_repr)] |
| 13 | +#[repr(i64)] |
| 14 | +pub enum RollupDisplayMode { |
| 15 | + #[default] |
| 16 | + Calculated = 0, |
| 17 | + OriginalList = 1, |
| 18 | + UniqueList = 2, |
| 19 | +} |
| 20 | + |
| 21 | +impl From<i64> for RollupDisplayMode { |
| 22 | + fn from(value: i64) -> Self { |
| 23 | + match value { |
| 24 | + 0 => RollupDisplayMode::Calculated, |
| 25 | + 1 => RollupDisplayMode::OriginalList, |
| 26 | + 2 => RollupDisplayMode::UniqueList, |
| 27 | + _ => RollupDisplayMode::Calculated, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl From<RollupDisplayMode> for i64 { |
| 33 | + fn from(value: RollupDisplayMode) -> Self { |
| 34 | + value as i64 |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 39 | +pub struct RollupTypeOption { |
| 40 | + pub relation_field_id: String, |
| 41 | + pub target_field_id: String, |
| 42 | + pub calculation_type: i64, |
| 43 | + pub show_as: RollupDisplayMode, |
| 44 | + #[serde(default)] |
| 45 | + pub condition_value: String, |
| 46 | +} |
| 47 | + |
| 48 | +impl Default for RollupTypeOption { |
| 49 | + fn default() -> Self { |
| 50 | + Self { |
| 51 | + relation_field_id: String::new(), |
| 52 | + target_field_id: String::new(), |
| 53 | + // Default to Count, which is applicable across all field types. |
| 54 | + calculation_type: 5, |
| 55 | + show_as: RollupDisplayMode::Calculated, |
| 56 | + condition_value: String::new(), |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl From<TypeOptionData> for RollupTypeOption { |
| 62 | + fn from(data: TypeOptionData) -> Self { |
| 63 | + let relation_field_id: String = data.get_as("relation_field_id").unwrap_or_default(); |
| 64 | + let target_field_id: String = data.get_as("target_field_id").unwrap_or_default(); |
| 65 | + let calculation_type: i64 = data.get_as("calculation_type").unwrap_or(5); |
| 66 | + let show_as: i64 = data.get_as("show_as").unwrap_or(0); |
| 67 | + let condition_value: String = data.get_as("condition_value").unwrap_or_default(); |
| 68 | + Self { |
| 69 | + relation_field_id, |
| 70 | + target_field_id, |
| 71 | + calculation_type, |
| 72 | + show_as: show_as.into(), |
| 73 | + condition_value, |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl From<RollupTypeOption> for TypeOptionData { |
| 79 | + fn from(data: RollupTypeOption) -> Self { |
| 80 | + TypeOptionDataBuilder::from([ |
| 81 | + ( |
| 82 | + "relation_field_id".into(), |
| 83 | + Any::String(data.relation_field_id.into()), |
| 84 | + ), |
| 85 | + ( |
| 86 | + "target_field_id".into(), |
| 87 | + Any::String(data.target_field_id.into()), |
| 88 | + ), |
| 89 | + ("calculation_type".into(), Any::BigInt(data.calculation_type)), |
| 90 | + ("show_as".into(), Any::BigInt(i64::from(data.show_as))), |
| 91 | + ( |
| 92 | + "condition_value".into(), |
| 93 | + Any::String(data.condition_value.into()), |
| 94 | + ), |
| 95 | + ]) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl TypeOptionCellReader for RollupTypeOption { |
| 100 | + fn json_cell(&self, cell: &Cell) -> Value { |
| 101 | + json!(self.stringify_cell(cell)) |
| 102 | + } |
| 103 | + |
| 104 | + fn numeric_cell(&self, cell: &Cell) -> Option<f64> { |
| 105 | + self.stringify_cell(cell).parse().ok() |
| 106 | + } |
| 107 | + |
| 108 | + fn convert_raw_cell_data(&self, cell_data: &str) -> String { |
| 109 | + cell_data.to_string() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl TypeOptionCellWriter for RollupTypeOption { |
| 114 | + fn convert_json_to_cell(&self, json_value: Value) -> Cell { |
| 115 | + let mut cell = new_cell_builder(FieldType::Rollup); |
| 116 | + match json_value { |
| 117 | + Value::String(value_str) => { |
| 118 | + cell.insert(CELL_DATA.into(), value_str.into()); |
| 119 | + }, |
| 120 | + _ => { |
| 121 | + cell.insert(CELL_DATA.into(), json_value.to_string().into()); |
| 122 | + }, |
| 123 | + } |
| 124 | + cell |
| 125 | + } |
| 126 | +} |
0 commit comments