meta.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  2. use serde::{Deserialize, Serialize};
  3. use std::collections::HashMap;
  4. use strum_macros::{Display, EnumIter, EnumString};
  5. pub const DEFAULT_ROW_HEIGHT: i32 = 36;
  6. pub const DEFAULT_FIELD_WIDTH: i32 = 150;
  7. #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf)]
  8. pub struct GridMeta {
  9. #[pb(index = 1)]
  10. pub grid_id: String,
  11. #[pb(index = 2)]
  12. pub fields: Vec<Field>,
  13. #[pb(index = 3)]
  14. pub blocks: Vec<GridBlock>,
  15. }
  16. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ProtoBuf)]
  17. pub struct GridBlock {
  18. #[pb(index = 1)]
  19. pub id: String,
  20. #[pb(index = 2)]
  21. pub start_row_index: i32,
  22. #[pb(index = 3)]
  23. pub row_count: i32,
  24. }
  25. impl GridBlock {
  26. pub fn new() -> Self {
  27. GridBlock {
  28. id: uuid::Uuid::new_v4().to_string(),
  29. ..Default::default()
  30. }
  31. }
  32. }
  33. pub struct GridBlockChangeset {
  34. pub block_id: String,
  35. pub start_row_index: Option<i32>,
  36. pub row_count: Option<i32>,
  37. }
  38. impl GridBlockChangeset {
  39. pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
  40. Self {
  41. block_id: block_id.to_string(),
  42. start_row_index: None,
  43. row_count: Some(row_count),
  44. }
  45. }
  46. }
  47. #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf)]
  48. pub struct GridBlockMeta {
  49. #[pb(index = 1)]
  50. pub block_id: String,
  51. #[pb(index = 2)]
  52. pub rows: Vec<RowMeta>,
  53. }
  54. #[derive(Debug, Clone, Default, Serialize, Deserialize, ProtoBuf, PartialEq, Eq)]
  55. pub struct Field {
  56. #[pb(index = 1)]
  57. pub id: String,
  58. #[pb(index = 2)]
  59. pub name: String,
  60. #[pb(index = 3)]
  61. pub desc: String,
  62. #[pb(index = 4)]
  63. pub field_type: FieldType,
  64. #[pb(index = 5)]
  65. pub frozen: bool,
  66. #[pb(index = 6)]
  67. pub visibility: bool,
  68. #[pb(index = 7)]
  69. pub width: i32,
  70. #[pb(index = 8)]
  71. pub type_options: String,
  72. }
  73. impl Field {
  74. pub fn new(name: &str, desc: &str, field_type: FieldType) -> Self {
  75. Self {
  76. id: uuid::Uuid::new_v4().to_string(),
  77. name: name.to_string(),
  78. desc: desc.to_string(),
  79. field_type,
  80. frozen: false,
  81. visibility: true,
  82. width: DEFAULT_FIELD_WIDTH,
  83. type_options: Default::default(),
  84. }
  85. }
  86. }
  87. #[derive(Debug, Clone, Default, ProtoBuf)]
  88. pub struct FieldChangeset {
  89. #[pb(index = 1)]
  90. pub field_id: String,
  91. #[pb(index = 2, one_of)]
  92. pub name: Option<String>,
  93. #[pb(index = 3, one_of)]
  94. pub desc: Option<String>,
  95. #[pb(index = 4, one_of)]
  96. pub field_type: Option<FieldType>,
  97. #[pb(index = 5, one_of)]
  98. pub frozen: Option<bool>,
  99. #[pb(index = 6, one_of)]
  100. pub visibility: Option<bool>,
  101. #[pb(index = 7, one_of)]
  102. pub width: Option<i32>,
  103. #[pb(index = 8, one_of)]
  104. pub type_options: Option<String>,
  105. }
  106. #[derive(Debug, Default, ProtoBuf)]
  107. pub struct RepeatedField {
  108. #[pb(index = 1)]
  109. pub items: Vec<Field>,
  110. }
  111. impl std::ops::Deref for RepeatedField {
  112. type Target = Vec<Field>;
  113. fn deref(&self) -> &Self::Target {
  114. &self.items
  115. }
  116. }
  117. impl std::ops::DerefMut for RepeatedField {
  118. fn deref_mut(&mut self) -> &mut Self::Target {
  119. &mut self.items
  120. }
  121. }
  122. impl std::convert::From<Vec<Field>> for RepeatedField {
  123. fn from(items: Vec<Field>) -> Self {
  124. Self { items }
  125. }
  126. }
  127. #[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum, EnumString, EnumIter, Display, Serialize, Deserialize)]
  128. pub enum FieldType {
  129. RichText = 0,
  130. Number = 1,
  131. DateTime = 2,
  132. SingleSelect = 3,
  133. MultiSelect = 4,
  134. Checkbox = 5,
  135. }
  136. impl std::default::Default for FieldType {
  137. fn default() -> Self {
  138. FieldType::RichText
  139. }
  140. }
  141. impl AsRef<FieldType> for FieldType {
  142. fn as_ref(&self) -> &FieldType {
  143. self
  144. }
  145. }
  146. impl From<&FieldType> for FieldType {
  147. fn from(field: &FieldType) -> Self {
  148. field.clone()
  149. }
  150. }
  151. impl FieldType {
  152. pub fn type_id(&self) -> String {
  153. let ty = self.clone();
  154. format!("{}", ty as u8)
  155. }
  156. pub fn from_type_id(type_id: &str) -> Result<FieldType, String> {
  157. match type_id {
  158. "0" => Ok(FieldType::RichText),
  159. "1" => Ok(FieldType::Number),
  160. "2" => Ok(FieldType::DateTime),
  161. "3" => Ok(FieldType::SingleSelect),
  162. "4" => Ok(FieldType::MultiSelect),
  163. "5" => Ok(FieldType::Checkbox),
  164. _ => Err(format!("Invalid type_id: {}", type_id)),
  165. }
  166. }
  167. }
  168. #[derive(Debug, Clone, Serialize, Deserialize, Default, ProtoBuf)]
  169. pub struct AnyData {
  170. #[pb(index = 1)]
  171. pub type_id: String,
  172. #[pb(index = 2)]
  173. pub value: Vec<u8>,
  174. }
  175. impl AnyData {
  176. pub fn from_str<F: Into<FieldType>>(field_type: F, s: &str) -> AnyData {
  177. Self::from_bytes(field_type, s.as_bytes().to_vec())
  178. }
  179. pub fn from_bytes<T: AsRef<[u8]>, F: Into<FieldType>>(field_type: F, bytes: T) -> AnyData {
  180. AnyData {
  181. type_id: field_type.into().type_id(),
  182. value: bytes.as_ref().to_vec(),
  183. }
  184. }
  185. }
  186. impl ToString for AnyData {
  187. fn to_string(&self) -> String {
  188. match String::from_utf8(self.value.clone()) {
  189. Ok(s) => s,
  190. Err(_) => "".to_owned(),
  191. }
  192. }
  193. }
  194. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, ProtoBuf)]
  195. pub struct RowMeta {
  196. #[pb(index = 1)]
  197. pub id: String,
  198. #[pb(index = 2)]
  199. pub block_id: String,
  200. #[pb(index = 3)]
  201. pub cell_by_field_id: HashMap<String, CellMeta>,
  202. #[pb(index = 4)]
  203. pub height: i32,
  204. #[pb(index = 5)]
  205. pub visibility: bool,
  206. }
  207. impl RowMeta {
  208. pub fn new(block_id: &str) -> Self {
  209. Self {
  210. id: uuid::Uuid::new_v4().to_string(),
  211. block_id: block_id.to_owned(),
  212. cell_by_field_id: Default::default(),
  213. height: DEFAULT_ROW_HEIGHT,
  214. visibility: true,
  215. }
  216. }
  217. }
  218. #[derive(Debug, Clone, Default, ProtoBuf)]
  219. pub struct RowMetaChangeset {
  220. #[pb(index = 1)]
  221. pub row_id: String,
  222. #[pb(index = 2, one_of)]
  223. pub height: Option<i32>,
  224. #[pb(index = 3, one_of)]
  225. pub visibility: Option<bool>,
  226. #[pb(index = 4)]
  227. pub cell_by_field_id: HashMap<String, CellMeta>,
  228. }
  229. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, ProtoBuf)]
  230. pub struct CellMeta {
  231. #[pb(index = 1)]
  232. pub field_id: String,
  233. #[pb(index = 2)]
  234. pub data: String,
  235. }
  236. impl CellMeta {
  237. pub fn new(field_id: &str, data: String) -> Self {
  238. Self {
  239. field_id: field_id.to_string(),
  240. data,
  241. }
  242. }
  243. }
  244. #[derive(Debug, Clone, Default, ProtoBuf)]
  245. pub struct CellMetaChangeset {
  246. #[pb(index = 1)]
  247. pub row_id: String,
  248. #[pb(index = 2)]
  249. pub field_id: String,
  250. #[pb(index = 3, one_of)]
  251. pub data: Option<String>,
  252. }
  253. impl std::convert::From<CellMetaChangeset> for RowMetaChangeset {
  254. fn from(changeset: CellMetaChangeset) -> Self {
  255. let mut cell_by_field_id = HashMap::with_capacity(1);
  256. if let Some(data) = changeset.data {
  257. let field_id = changeset.field_id;
  258. let cell_meta = CellMeta {
  259. field_id: field_id.clone(),
  260. data,
  261. };
  262. cell_by_field_id.insert(field_id, cell_meta);
  263. }
  264. RowMetaChangeset {
  265. row_id: changeset.row_id,
  266. height: None,
  267. visibility: None,
  268. cell_by_field_id,
  269. }
  270. }
  271. }