cell_data_operation.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. use crate::services::field::*;
  2. use std::fmt::Formatter;
  3. use flowy_error::FlowyError;
  4. use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType};
  5. use serde::{Deserialize, Serialize};
  6. pub trait CellDataOperation {
  7. fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> String;
  8. fn apply_changeset<T: Into<CellDataChangeset>>(
  9. &self,
  10. changeset: T,
  11. cell_meta: Option<CellMeta>,
  12. ) -> Result<String, FlowyError>;
  13. }
  14. #[derive(Debug)]
  15. pub struct CellDataChangeset(String);
  16. impl std::fmt::Display for CellDataChangeset {
  17. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  18. write!(f, "{}", &self.0)
  19. }
  20. }
  21. impl<T: AsRef<str>> std::convert::From<T> for CellDataChangeset {
  22. fn from(s: T) -> Self {
  23. let s = s.as_ref().to_owned();
  24. CellDataChangeset(s)
  25. }
  26. }
  27. impl std::ops::Deref for CellDataChangeset {
  28. type Target = str;
  29. fn deref(&self) -> &Self::Target {
  30. &self.0
  31. }
  32. }
  33. #[derive(Debug, Serialize, Deserialize)]
  34. pub struct TypeOptionCellData {
  35. pub data: String,
  36. pub field_type: FieldType,
  37. }
  38. impl std::str::FromStr for TypeOptionCellData {
  39. type Err = FlowyError;
  40. fn from_str(s: &str) -> Result<Self, Self::Err> {
  41. let type_option_cell_data: TypeOptionCellData = serde_json::from_str(s)?;
  42. Ok(type_option_cell_data)
  43. }
  44. }
  45. impl TypeOptionCellData {
  46. pub fn new<T: ToString>(data: T, field_type: FieldType) -> Self {
  47. TypeOptionCellData {
  48. data: data.to_string(),
  49. field_type,
  50. }
  51. }
  52. pub fn json(&self) -> String {
  53. serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
  54. }
  55. pub fn is_number(&self) -> bool {
  56. self.field_type == FieldType::Number
  57. }
  58. pub fn is_text(&self) -> bool {
  59. self.field_type == FieldType::RichText
  60. }
  61. pub fn is_checkbox(&self) -> bool {
  62. self.field_type == FieldType::Checkbox
  63. }
  64. pub fn is_date(&self) -> bool {
  65. self.field_type == FieldType::DateTime
  66. }
  67. pub fn is_single_select(&self) -> bool {
  68. self.field_type == FieldType::SingleSelect
  69. }
  70. pub fn is_multi_select(&self) -> bool {
  71. self.field_type == FieldType::MultiSelect
  72. }
  73. }
  74. /// The function,apply_cell_data_changeset, will apply the cell_data_changeset.
  75. ///
  76. /// The cell_data_changeset will be deserialized into specific data base on the FieldType.
  77. pub fn apply_cell_data_changeset<T: Into<CellDataChangeset>>(
  78. changeset: T,
  79. cell_meta: Option<CellMeta>,
  80. field_meta: &FieldMeta,
  81. ) -> Result<String, FlowyError> {
  82. match field_meta.field_type {
  83. FieldType::RichText => RichTextTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
  84. FieldType::Number => NumberTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
  85. FieldType::DateTime => DateTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
  86. FieldType::SingleSelect => SingleSelectTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
  87. FieldType::MultiSelect => MultiSelectTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
  88. FieldType::Checkbox => CheckboxTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
  89. }
  90. }
  91. #[tracing::instrument(level = "trace", skip(field_meta, data), fields(content), err)]
  92. pub fn decode_cell_data(data: String, field_meta: &FieldMeta) -> Result<String, FlowyError> {
  93. let s = match field_meta.field_type {
  94. FieldType::RichText => RichTextTypeOption::from(field_meta).decode_cell_data(data, field_meta),
  95. FieldType::Number => NumberTypeOption::from(field_meta).decode_cell_data(data, field_meta),
  96. FieldType::DateTime => DateTypeOption::from(field_meta).decode_cell_data(data, field_meta),
  97. FieldType::SingleSelect => SingleSelectTypeOption::from(field_meta).decode_cell_data(data, field_meta),
  98. FieldType::MultiSelect => MultiSelectTypeOption::from(field_meta).decode_cell_data(data, field_meta),
  99. FieldType::Checkbox => CheckboxTypeOption::from(field_meta).decode_cell_data(data, field_meta),
  100. };
  101. tracing::Span::current().record("content", &format!("{:?}: {}", field_meta.field_type, s).as_str());
  102. Ok(s)
  103. }