meta.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 serde_repr::*;
  7. use std::collections::HashMap;
  8. use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString};
  9. pub const DEFAULT_ROW_HEIGHT: i32 = 42;
  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 blocks: 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 rows: 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. let width = field_type.default_cell_width();
  90. Self {
  91. id: uuid::Uuid::new_v4().to_string(),
  92. name: name.to_string(),
  93. desc: desc.to_string(),
  94. field_type,
  95. frozen: false,
  96. visibility: true,
  97. width,
  98. type_options: Default::default(),
  99. }
  100. }
  101. pub fn insert_type_option_entry<T>(&mut self, entry: &T)
  102. where
  103. T: TypeOptionDataEntry + ?Sized,
  104. {
  105. self.type_options.insert(entry.field_type().type_id(), entry.json_str());
  106. }
  107. pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type: &FieldType) -> Option<T> {
  108. self.type_options
  109. .get(&field_type.type_id())
  110. .map(|s| T::from_json_str(s))
  111. }
  112. pub fn insert_type_option_str(&mut self, field_type: &FieldType, json_str: String) {
  113. self.type_options.insert(field_type.type_id(), json_str);
  114. }
  115. pub fn get_type_option_str(&self, field_type: Option<FieldType>) -> Option<String> {
  116. let field_type = field_type.as_ref().unwrap_or(&self.field_type);
  117. self.type_options.get(&field_type.type_id()).map(|s| s.to_owned())
  118. }
  119. }
  120. pub trait TypeOptionDataEntry {
  121. fn field_type(&self) -> FieldType;
  122. fn json_str(&self) -> String;
  123. fn protobuf_bytes(&self) -> Bytes;
  124. }
  125. pub trait TypeOptionDataDeserializer {
  126. fn from_json_str(s: &str) -> Self;
  127. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  128. }
  129. #[derive(Debug, Clone, Default, ProtoBuf)]
  130. pub struct FieldChangesetPayload {
  131. #[pb(index = 1)]
  132. pub field_id: String,
  133. #[pb(index = 2)]
  134. pub grid_id: String,
  135. #[pb(index = 3, one_of)]
  136. pub name: Option<String>,
  137. #[pb(index = 4, one_of)]
  138. pub desc: Option<String>,
  139. #[pb(index = 5, one_of)]
  140. pub field_type: Option<FieldType>,
  141. #[pb(index = 6, one_of)]
  142. pub frozen: Option<bool>,
  143. #[pb(index = 7, one_of)]
  144. pub visibility: Option<bool>,
  145. #[pb(index = 8, one_of)]
  146. pub width: Option<i32>,
  147. #[pb(index = 9, one_of)]
  148. pub type_option_data: Option<Vec<u8>>,
  149. }
  150. #[derive(Debug, Clone, Default)]
  151. pub struct FieldChangesetParams {
  152. pub field_id: String,
  153. pub grid_id: String,
  154. pub name: Option<String>,
  155. pub desc: Option<String>,
  156. pub field_type: Option<FieldType>,
  157. pub frozen: Option<bool>,
  158. pub visibility: Option<bool>,
  159. pub width: Option<i32>,
  160. pub type_option_data: Option<Vec<u8>>,
  161. }
  162. impl TryInto<FieldChangesetParams> for FieldChangesetPayload {
  163. type Error = ErrorCode;
  164. fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
  165. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  166. let field_id = NotEmptyUuid::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  167. if let Some(type_option_data) = self.type_option_data.as_ref() {
  168. if type_option_data.is_empty() {
  169. return Err(ErrorCode::TypeOptionDataIsEmpty);
  170. }
  171. }
  172. Ok(FieldChangesetParams {
  173. field_id: field_id.0,
  174. grid_id: grid_id.0,
  175. name: self.name,
  176. desc: self.desc,
  177. field_type: self.field_type,
  178. frozen: self.frozen,
  179. visibility: self.visibility,
  180. width: self.width,
  181. type_option_data: self.type_option_data,
  182. })
  183. }
  184. }
  185. #[derive(
  186. Debug,
  187. Clone,
  188. PartialEq,
  189. Eq,
  190. ProtoBuf_Enum,
  191. EnumCountMacro,
  192. EnumString,
  193. EnumIter,
  194. Display,
  195. Serialize_repr,
  196. Deserialize_repr,
  197. )]
  198. #[repr(u8)]
  199. pub enum FieldType {
  200. RichText = 0,
  201. Number = 1,
  202. DateTime = 2,
  203. SingleSelect = 3,
  204. MultiSelect = 4,
  205. Checkbox = 5,
  206. }
  207. impl std::default::Default for FieldType {
  208. fn default() -> Self {
  209. FieldType::RichText
  210. }
  211. }
  212. impl AsRef<FieldType> for FieldType {
  213. fn as_ref(&self) -> &FieldType {
  214. self
  215. }
  216. }
  217. impl From<&FieldType> for FieldType {
  218. fn from(field_type: &FieldType) -> Self {
  219. field_type.clone()
  220. }
  221. }
  222. impl FieldType {
  223. pub fn type_id(&self) -> String {
  224. let ty = self.clone();
  225. format!("{}", ty as u8)
  226. }
  227. pub fn default_cell_width(&self) -> i32 {
  228. match self {
  229. FieldType::DateTime => 180,
  230. _ => 150,
  231. }
  232. }
  233. }
  234. #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
  235. pub struct AnyData {
  236. #[pb(index = 1)]
  237. pub type_id: String,
  238. #[pb(index = 2)]
  239. pub value: Vec<u8>,
  240. }
  241. impl AnyData {
  242. pub fn from_str<F: Into<FieldType>>(field_type: F, s: &str) -> AnyData {
  243. Self::from_bytes(field_type, s.as_bytes().to_vec())
  244. }
  245. pub fn from_bytes<T: AsRef<[u8]>, F: Into<FieldType>>(field_type: F, bytes: T) -> AnyData {
  246. AnyData {
  247. type_id: field_type.into().type_id(),
  248. value: bytes.as_ref().to_vec(),
  249. }
  250. }
  251. }
  252. impl ToString for AnyData {
  253. fn to_string(&self) -> String {
  254. match String::from_utf8(self.value.clone()) {
  255. Ok(s) => s,
  256. Err(_) => "".to_owned(),
  257. }
  258. }
  259. }
  260. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ProtoBuf)]
  261. pub struct RowMeta {
  262. #[pb(index = 1)]
  263. pub id: String,
  264. #[pb(index = 2)]
  265. pub block_id: String,
  266. #[pb(index = 3)]
  267. /// cells contains key/value pairs.
  268. /// key: field id,
  269. /// value: CellMeta
  270. pub cells: HashMap<String, CellMeta>,
  271. #[pb(index = 4)]
  272. pub height: i32,
  273. #[pb(index = 5)]
  274. pub visibility: bool,
  275. }
  276. impl RowMeta {
  277. pub fn new(block_id: &str) -> Self {
  278. Self {
  279. id: uuid::Uuid::new_v4().to_string(),
  280. block_id: block_id.to_owned(),
  281. cells: Default::default(),
  282. height: DEFAULT_ROW_HEIGHT,
  283. visibility: true,
  284. }
  285. }
  286. }
  287. #[derive(Debug, Clone, Default, ProtoBuf)]
  288. pub struct RowMetaChangeset {
  289. #[pb(index = 1)]
  290. pub row_id: String,
  291. #[pb(index = 2, one_of)]
  292. pub height: Option<i32>,
  293. #[pb(index = 3, one_of)]
  294. pub visibility: Option<bool>,
  295. #[pb(index = 4)]
  296. pub cell_by_field_id: HashMap<String, CellMeta>,
  297. }
  298. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, ProtoBuf)]
  299. pub struct CellMeta {
  300. #[pb(index = 1)]
  301. pub data: String,
  302. }
  303. impl CellMeta {
  304. pub fn new(data: String) -> Self {
  305. Self { data }
  306. }
  307. }
  308. #[derive(Debug, Clone, Default, ProtoBuf)]
  309. pub struct CellMetaChangeset {
  310. #[pb(index = 1)]
  311. pub grid_id: String,
  312. #[pb(index = 2)]
  313. pub row_id: String,
  314. #[pb(index = 3)]
  315. pub field_id: String,
  316. #[pb(index = 4, one_of)]
  317. pub data: Option<String>,
  318. }
  319. impl std::convert::From<CellMetaChangeset> for RowMetaChangeset {
  320. fn from(changeset: CellMetaChangeset) -> Self {
  321. let mut cell_by_field_id = HashMap::with_capacity(1);
  322. let field_id = changeset.field_id;
  323. let cell_meta = CellMeta {
  324. data: changeset.data.unwrap_or_else(|| "".to_owned()),
  325. };
  326. cell_by_field_id.insert(field_id, cell_meta);
  327. RowMetaChangeset {
  328. row_id: changeset.row_id,
  329. height: None,
  330. visibility: None,
  331. cell_by_field_id,
  332. }
  333. }
  334. }
  335. #[derive(Clone, ProtoBuf)]
  336. pub struct BuildGridContext {
  337. #[pb(index = 1)]
  338. pub field_metas: Vec<FieldMeta>,
  339. #[pb(index = 2)]
  340. pub block_meta: GridBlockMeta,
  341. #[pb(index = 3)]
  342. pub block_meta_data: GridBlockMetaData,
  343. }
  344. impl std::default::Default for BuildGridContext {
  345. fn default() -> Self {
  346. let block_meta = GridBlockMeta::new();
  347. let block_meta_data = GridBlockMetaData {
  348. block_id: block_meta.block_id.clone(),
  349. rows: vec![],
  350. };
  351. Self {
  352. field_metas: vec![],
  353. block_meta,
  354. block_meta_data,
  355. }
  356. }
  357. }