field_builder.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. use crate::services::field::{
  2. CheckboxDescription, DateDescription, DateFormat, MoneySymbol, MultiSelectDescription, NumberDescription,
  3. RichTextDescription, SelectOption, SingleSelectDescription, TimeFormat,
  4. };
  5. use flowy_grid_data_model::entities::{AnyData, Field, FieldType};
  6. pub struct FieldBuilder {
  7. field: Field,
  8. type_options_builder: Box<dyn TypeOptionsBuilder>,
  9. }
  10. impl FieldBuilder {
  11. pub fn new<T: TypeOptionsBuilder + 'static>(type_options_builder: T) -> Self {
  12. let field = Field::new("Name", "", FieldType::RichText);
  13. Self {
  14. field,
  15. type_options_builder: Box::new(type_options_builder),
  16. }
  17. }
  18. pub fn name(mut self, name: &str) -> Self {
  19. self.field.name = name.to_owned();
  20. self
  21. }
  22. pub fn desc(mut self, desc: &str) -> Self {
  23. self.field.desc = desc.to_owned();
  24. self
  25. }
  26. pub fn field_type(mut self, field_type: FieldType) -> Self {
  27. self.field.field_type = field_type;
  28. self
  29. }
  30. pub fn visibility(mut self, visibility: bool) -> Self {
  31. self.field.visibility = visibility;
  32. self
  33. }
  34. pub fn width(mut self, width: i32) -> Self {
  35. self.field.width = width;
  36. self
  37. }
  38. pub fn frozen(mut self, frozen: bool) -> Self {
  39. self.field.frozen = frozen;
  40. self
  41. }
  42. pub fn build(mut self) -> Field {
  43. assert_eq!(self.field.field_type, self.type_options_builder.field_type());
  44. let type_options = self.type_options_builder.build();
  45. self.field.type_options = type_options;
  46. self.field
  47. }
  48. }
  49. pub trait TypeOptionsBuilder {
  50. fn field_type(&self) -> FieldType;
  51. fn build(&self) -> String;
  52. }
  53. // Text
  54. pub struct RichTextTypeOptionsBuilder(RichTextDescription);
  55. impl RichTextTypeOptionsBuilder {
  56. pub fn new() -> Self {
  57. Self(RichTextDescription::default())
  58. }
  59. }
  60. impl TypeOptionsBuilder for RichTextTypeOptionsBuilder {
  61. fn field_type(&self) -> FieldType {
  62. self.0.field_type()
  63. }
  64. fn build(&self) -> String {
  65. self.0.clone().into()
  66. }
  67. }
  68. // Number
  69. pub struct NumberTypeOptionsBuilder(NumberDescription);
  70. impl NumberTypeOptionsBuilder {
  71. pub fn new() -> Self {
  72. Self(NumberDescription::default())
  73. }
  74. pub fn name(mut self, name: &str) -> Self {
  75. self.0.name = name.to_string();
  76. self
  77. }
  78. pub fn set_money_symbol(mut self, money_symbol: MoneySymbol) -> Self {
  79. self.0.set_money_symbol(money_symbol);
  80. self
  81. }
  82. pub fn scale(mut self, scale: u32) -> Self {
  83. self.0.scale = scale;
  84. self
  85. }
  86. pub fn positive(mut self, positive: bool) -> Self {
  87. self.0.sign_positive = positive;
  88. self
  89. }
  90. }
  91. impl TypeOptionsBuilder for NumberTypeOptionsBuilder {
  92. fn field_type(&self) -> FieldType {
  93. self.0.field_type()
  94. }
  95. fn build(&self) -> String {
  96. self.0.clone().into()
  97. }
  98. }
  99. // Date
  100. pub struct DateTypeOptionsBuilder(DateDescription);
  101. impl DateTypeOptionsBuilder {
  102. pub fn new() -> Self {
  103. Self(DateDescription::default())
  104. }
  105. pub fn date_format(mut self, date_format: DateFormat) -> Self {
  106. self.0.date_format = date_format;
  107. self
  108. }
  109. pub fn time_format(mut self, time_format: TimeFormat) -> Self {
  110. self.0.time_format = time_format;
  111. self
  112. }
  113. }
  114. impl TypeOptionsBuilder for DateTypeOptionsBuilder {
  115. fn field_type(&self) -> FieldType {
  116. self.0.field_type()
  117. }
  118. fn build(&self) -> String {
  119. self.0.clone().into()
  120. }
  121. }
  122. // Single Select
  123. pub struct SingleSelectTypeOptionsBuilder(SingleSelectDescription);
  124. impl SingleSelectTypeOptionsBuilder {
  125. pub fn new() -> Self {
  126. Self(SingleSelectDescription::default())
  127. }
  128. pub fn option(mut self, opt: SelectOption) -> Self {
  129. self.0.options.push(opt);
  130. self
  131. }
  132. }
  133. impl TypeOptionsBuilder for SingleSelectTypeOptionsBuilder {
  134. fn field_type(&self) -> FieldType {
  135. self.0.field_type()
  136. }
  137. fn build(&self) -> String {
  138. self.0.clone().into()
  139. }
  140. }
  141. // Multi Select
  142. pub struct MultiSelectTypeOptionsBuilder(MultiSelectDescription);
  143. impl MultiSelectTypeOptionsBuilder {
  144. pub fn new() -> Self {
  145. Self(MultiSelectDescription::default())
  146. }
  147. pub fn option(mut self, opt: SelectOption) -> Self {
  148. self.0.options.push(opt);
  149. self
  150. }
  151. }
  152. impl TypeOptionsBuilder for MultiSelectTypeOptionsBuilder {
  153. fn field_type(&self) -> FieldType {
  154. self.0.field_type()
  155. }
  156. fn build(&self) -> String {
  157. self.0.clone().into()
  158. }
  159. }
  160. // Checkbox
  161. pub struct CheckboxTypeOptionsBuilder(CheckboxDescription);
  162. impl CheckboxTypeOptionsBuilder {
  163. pub fn new() -> Self {
  164. Self(CheckboxDescription::default())
  165. }
  166. pub fn set_selected(mut self, is_selected: bool) -> Self {
  167. self.0.is_selected = is_selected;
  168. self
  169. }
  170. }
  171. impl TypeOptionsBuilder for CheckboxTypeOptionsBuilder {
  172. fn field_type(&self) -> FieldType {
  173. self.0.field_type()
  174. }
  175. fn build(&self) -> String {
  176. self.0.clone().into()
  177. }
  178. }