meta.rs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. use crate::parser::NotEmptyUuid;
  2. use bytes::Bytes;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use flowy_error_code::ErrorCode;
  5. use serde::{Deserialize, Serialize};
  6. use std::collections::HashMap;
  7. use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString};
  8. pub const DEFAULT_ROW_HEIGHT: i32 = 42;
  9. pub const DEFAULT_FIELD_WIDTH: i32 = 150;
  10. #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf)]
  11. pub struct GridMeta {
  12. #[pb(index = 1)]
  13. pub grid_id: String,
  14. #[pb(index = 2)]
  15. pub fields: Vec<FieldMeta>,
  16. #[pb(index = 3)]
  17. pub block_metas: Vec<GridBlockMeta>,
  18. }
  19. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ProtoBuf)]
  20. pub struct GridBlockMeta {
  21. #[pb(index = 1)]
  22. pub block_id: String,
  23. #[pb(index = 2)]
  24. pub start_row_index: i32,
  25. #[pb(index = 3)]
  26. pub row_count: i32,
  27. }
  28. impl GridBlockMeta {
  29. pub fn len(&self) -> i32 {
  30. self.row_count
  31. }
  32. pub fn is_empty(&self) -> bool {
  33. self.row_count == 0
  34. }
  35. }
  36. impl GridBlockMeta {
  37. pub fn new() -> Self {
  38. GridBlockMeta {
  39. block_id: uuid::Uuid::new_v4().to_string(),
  40. ..Default::default()
  41. }
  42. }
  43. }
  44. pub struct GridBlockMetaChangeset {
  45. pub block_id: String,
  46. pub start_row_index: Option<i32>,
  47. pub row_count: Option<i32>,
  48. }
  49. impl GridBlockMetaChangeset {
  50. pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
  51. Self {
  52. block_id: block_id.to_string(),
  53. start_row_index: None,
  54. row_count: Some(row_count),
  55. }
  56. }
  57. }
  58. #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf)]
  59. pub struct GridBlockMetaData {
  60. #[pb(index = 1)]
  61. pub block_id: String,
  62. #[pb(index = 2)]
  63. pub row_metas: Vec<RowMeta>,
  64. }
  65. #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf, Eq, PartialEq)]
  66. pub struct FieldMeta {
  67. #[pb(index = 1)]
  68. pub id: String,
  69. #[pb(index = 2)]
  70. pub name: String,
  71. #[pb(index = 3)]
  72. pub desc: String,
  73. #[pb(index = 4)]
  74. pub field_type: FieldType,
  75. #[pb(index = 5)]
  76. pub frozen: bool,
  77. #[pb(index = 6)]
  78. pub visibility: bool,
  79. #[pb(index = 7)]
  80. pub width: i32,
  81. #[pb(index = 8)]
  82. /// type_options contains key/value pairs
  83. /// key: id of the FieldType
  84. /// value: type option data string
  85. pub type_options: HashMap<String, String>,
  86. }
  87. impl FieldMeta {
  88. pub fn new(name: &str, desc: &str, field_type: FieldType) -> Self {
  89. Self {
  90. id: uuid::Uuid::new_v4().to_string(),
  91. name: name.to_string(),
  92. desc: desc.to_string(),
  93. field_type,
  94. frozen: false,
  95. visibility: true,
  96. width: DEFAULT_FIELD_WIDTH,
  97. type_options: Default::default(),
  98. }
  99. }
  100. pub fn insert_type_option_entry<T: TypeOptionDataEntry + ?Sized>(&mut self, entry: &T) {
  101. self.type_options.insert(entry.field_type().type_id(), entry.json_str());
  102. }
  103. pub fn get_type_option_entry<T: TypeOptionDataEntity>(&self, field_type: Option<FieldType>) -> Option<T> {
  104. let field_type = field_type.as_ref().unwrap_or(&self.field_type);
  105. self.type_options
  106. .get(&field_type.type_id())
  107. .map(|s| T::from_json_str(s))
  108. }
  109. pub fn insert_type_option_str(&mut self, field_type: &FieldType, json_str: String) {
  110. self.type_options.insert(field_type.type_id(), json_str);
  111. }
  112. pub fn get_type_option_str(&self, field_type: Option<FieldType>) -> Option<String> {
  113. let field_type = field_type.as_ref().unwrap_or(&self.field_type);
  114. self.type_options.get(&field_type.type_id()).map(|s| s.to_owned())
  115. }
  116. }
  117. pub trait TypeOptionDataEntry {
  118. fn field_type(&self) -> FieldType;
  119. fn json_str(&self) -> String;
  120. fn protobuf_bytes(&self) -> Bytes;
  121. }
  122. pub trait TypeOptionDataEntity {
  123. fn from_json_str(s: &str) -> Self;
  124. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  125. }
  126. #[derive(Debug, Clone, Default, ProtoBuf)]
  127. pub struct FieldChangesetPayload {
  128. #[pb(index = 1)]
  129. pub field_id: String,
  130. #[pb(index = 2)]
  131. pub grid_id: String,
  132. #[pb(index = 3, one_of)]
  133. pub name: Option<String>,
  134. #[pb(index = 4, one_of)]
  135. pub desc: Option<String>,
  136. #[pb(index = 5, one_of)]
  137. pub field_type: Option<FieldType>,
  138. #[pb(index = 6, one_of)]
  139. pub frozen: Option<bool>,
  140. #[pb(index = 7, one_of)]
  141. pub visibility: Option<bool>,
  142. #[pb(index = 8, one_of)]
  143. pub width: Option<i32>,
  144. #[pb(index = 9, one_of)]
  145. pub type_option_data: Option<Vec<u8>>,
  146. }
  147. #[derive(Debug, Clone, Default)]
  148. pub struct FieldChangesetParams {
  149. pub field_id: String,
  150. pub grid_id: String,
  151. pub name: Option<String>,
  152. pub desc: Option<String>,
  153. pub field_type: Option<FieldType>,
  154. pub frozen: Option<bool>,
  155. pub visibility: Option<bool>,
  156. pub width: Option<i32>,
  157. pub type_option_data: Option<Vec<u8>>,
  158. }
  159. impl TryInto<FieldChangesetParams> for FieldChangesetPayload {
  160. type Error = ErrorCode;
  161. fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
  162. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  163. let field_id = NotEmptyUuid::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  164. if let Some(type_option_data) = self.type_option_data.as_ref() {
  165. if type_option_data.is_empty() {
  166. return Err(ErrorCode::TypeOptionDataIsEmpty);
  167. }
  168. }
  169. Ok(FieldChangesetParams {
  170. field_id: field_id.0,
  171. grid_id: grid_id.0,
  172. name: self.name,
  173. desc: self.desc,
  174. field_type: self.field_type,
  175. frozen: self.frozen,
  176. visibility: self.visibility,
  177. width: self.width,
  178. type_option_data: self.type_option_data,
  179. })
  180. }
  181. }
  182. #[derive(
  183. Debug, Clone, PartialEq, Eq, ProtoBuf_Enum, EnumCountMacro, EnumString, EnumIter, Display, Serialize, Deserialize,
  184. )]
  185. #[repr(u8)]
  186. pub enum FieldType {
  187. RichText = 0,
  188. Number = 1,
  189. DateTime = 2,
  190. SingleSelect = 3,
  191. MultiSelect = 4,
  192. Checkbox = 5,
  193. }
  194. impl std::default::Default for FieldType {
  195. fn default() -> Self {
  196. FieldType::RichText
  197. }
  198. }
  199. impl AsRef<FieldType> for FieldType {
  200. fn as_ref(&self) -> &FieldType {
  201. self
  202. }
  203. }
  204. impl From<&FieldType> for FieldType {
  205. fn from(field_type: &FieldType) -> Self {
  206. field_type.clone()
  207. }
  208. }
  209. impl FieldType {
  210. pub fn type_id(&self) -> String {
  211. let ty = self.clone();
  212. format!("{}", ty as u8)
  213. }
  214. }
  215. #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
  216. pub struct AnyData {
  217. #[pb(index = 1)]
  218. pub type_id: String,
  219. #[pb(index = 2)]
  220. pub value: Vec<u8>,
  221. }
  222. impl AnyData {
  223. pub fn from_str<F: Into<FieldType>>(field_type: F, s: &str) -> AnyData {
  224. Self::from_bytes(field_type, s.as_bytes().to_vec())
  225. }
  226. pub fn from_bytes<T: AsRef<[u8]>, F: Into<FieldType>>(field_type: F, bytes: T) -> AnyData {
  227. AnyData {
  228. type_id: field_type.into().type_id(),
  229. value: bytes.as_ref().to_vec(),
  230. }
  231. }
  232. }
  233. impl ToString for AnyData {
  234. fn to_string(&self) -> String {
  235. match String::from_utf8(self.value.clone()) {
  236. Ok(s) => s,
  237. Err(_) => "".to_owned(),
  238. }
  239. }
  240. }
  241. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ProtoBuf)]
  242. pub struct RowMeta {
  243. #[pb(index = 1)]
  244. pub id: String,
  245. #[pb(index = 2)]
  246. pub block_id: String,
  247. #[pb(index = 3)]
  248. /// cells contains key/value pairs.
  249. /// key: field id,
  250. /// value: CellMeta
  251. pub cells: HashMap<String, CellMeta>,
  252. #[pb(index = 4)]
  253. pub height: i32,
  254. #[pb(index = 5)]
  255. pub visibility: bool,
  256. }
  257. impl RowMeta {
  258. pub fn new(block_id: &str) -> Self {
  259. Self {
  260. id: uuid::Uuid::new_v4().to_string(),
  261. block_id: block_id.to_owned(),
  262. cells: Default::default(),
  263. height: DEFAULT_ROW_HEIGHT,
  264. visibility: true,
  265. }
  266. }
  267. }
  268. #[derive(Debug, Clone, Default, ProtoBuf)]
  269. pub struct RowMetaChangeset {
  270. #[pb(index = 1)]
  271. pub row_id: String,
  272. #[pb(index = 2, one_of)]
  273. pub height: Option<i32>,
  274. #[pb(index = 3, one_of)]
  275. pub visibility: Option<bool>,
  276. #[pb(index = 4)]
  277. pub cell_by_field_id: HashMap<String, CellMeta>,
  278. }
  279. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, ProtoBuf)]
  280. pub struct CellMeta {
  281. #[pb(index = 1)]
  282. pub data: String,
  283. }
  284. impl CellMeta {
  285. pub fn new(data: String) -> Self {
  286. Self { data }
  287. }
  288. }
  289. #[derive(Debug, Clone, Default, ProtoBuf)]
  290. pub struct CellMetaChangeset {
  291. #[pb(index = 1)]
  292. pub grid_id: String,
  293. #[pb(index = 2)]
  294. pub row_id: String,
  295. #[pb(index = 3)]
  296. pub field_id: String,
  297. #[pb(index = 4, one_of)]
  298. pub data: Option<String>,
  299. }
  300. impl std::convert::From<CellMetaChangeset> for RowMetaChangeset {
  301. fn from(changeset: CellMetaChangeset) -> Self {
  302. let mut cell_by_field_id = HashMap::with_capacity(1);
  303. let field_id = changeset.field_id;
  304. let cell_meta = CellMeta {
  305. data: changeset.data.unwrap_or_else(|| "".to_owned()),
  306. };
  307. cell_by_field_id.insert(field_id, cell_meta);
  308. RowMetaChangeset {
  309. row_id: changeset.row_id,
  310. height: None,
  311. visibility: None,
  312. cell_by_field_id: cell_by_field_id,
  313. }
  314. }
  315. }
  316. #[derive(Clone, ProtoBuf)]
  317. pub struct BuildGridContext {
  318. #[pb(index = 1)]
  319. pub field_metas: Vec<FieldMeta>,
  320. #[pb(index = 2)]
  321. pub block_metas: GridBlockMeta,
  322. #[pb(index = 3)]
  323. pub block_meta_data: GridBlockMetaData,
  324. }
  325. impl std::default::Default for BuildGridContext {
  326. fn default() -> Self {
  327. let grid_block = GridBlockMeta::new();
  328. let grid_block_meta_data = GridBlockMetaData {
  329. block_id: grid_block.block_id.clone(),
  330. row_metas: vec![],
  331. };
  332. Self {
  333. field_metas: vec![],
  334. block_metas: grid_block,
  335. block_meta_data: grid_block_meta_data,
  336. }
  337. }
  338. }