checkbox_description.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use crate::impl_from_and_to_type_option;
  2. use crate::services::row::StringifyCellData;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use flowy_error::FlowyError;
  5. use flowy_grid_data_model::entities::{Field, FieldType};
  6. use serde::{Deserialize, Serialize};
  7. #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
  8. pub struct CheckboxDescription {
  9. #[pb(index = 1)]
  10. pub is_selected: bool,
  11. }
  12. impl_from_and_to_type_option!(CheckboxDescription, FieldType::Checkbox);
  13. impl StringifyCellData for CheckboxDescription {
  14. fn str_from_cell_data(&self, data: String) -> String {
  15. data
  16. }
  17. fn str_to_cell_data(&self, s: &str) -> Result<String, FlowyError> {
  18. let s = match string_to_bool(s) {
  19. true => "1",
  20. false => "0",
  21. };
  22. Ok(s.to_owned())
  23. }
  24. }
  25. fn string_to_bool(bool_str: &str) -> bool {
  26. let lower_case_str: &str = &bool_str.to_lowercase();
  27. match lower_case_str {
  28. "1" => true,
  29. "true" => true,
  30. "yes" => true,
  31. "0" => false,
  32. "false" => false,
  33. "no" => false,
  34. _ => false,
  35. }
  36. }