checkbox_description.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use crate::impl_from_and_to_type_option;
  2. use crate::services::row::StringifyCellData;
  3. use flowy_derive::ProtoBuf;
  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. }
  37. #[cfg(test)]
  38. mod tests {
  39. use crate::services::cell::CheckboxDescription;
  40. use crate::services::row::StringifyCellData;
  41. #[test]
  42. fn checkout_box_description_test() {
  43. let description = CheckboxDescription::default();
  44. assert_eq!(description.str_to_cell_data("true").unwrap(), "1".to_owned());
  45. assert_eq!(description.str_to_cell_data("1").unwrap(), "1".to_owned());
  46. assert_eq!(description.str_to_cell_data("yes").unwrap(), "1".to_owned());
  47. assert_eq!(description.str_to_cell_data("false").unwrap(), "0".to_owned());
  48. assert_eq!(description.str_to_cell_data("no").unwrap(), "0".to_owned());
  49. assert_eq!(description.str_to_cell_data("123").unwrap(), "0".to_owned());
  50. assert_eq!(description.str_from_cell_data("1".to_owned()), "1".to_owned());
  51. }
  52. }