app.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. use crate::{
  2. entities::parser::{
  3. app::{AppColorStyle, AppIdentify, AppName},
  4. workspace::WorkspaceIdentify,
  5. },
  6. entities::view::RepeatedViewPB,
  7. errors::ErrorCode,
  8. impl_def_and_def_mut,
  9. };
  10. use flowy_derive::ProtoBuf;
  11. use flowy_folder_data_model::revision::AppRevision;
  12. use std::convert::TryInto;
  13. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  14. pub struct AppPB {
  15. #[pb(index = 1)]
  16. pub id: String,
  17. #[pb(index = 2)]
  18. pub workspace_id: String,
  19. #[pb(index = 3)]
  20. pub name: String,
  21. #[pb(index = 4)]
  22. pub desc: String,
  23. #[pb(index = 5)]
  24. pub belongings: RepeatedViewPB,
  25. #[pb(index = 6)]
  26. pub version: i64,
  27. #[pb(index = 7)]
  28. pub modified_time: i64,
  29. #[pb(index = 8)]
  30. pub create_time: i64,
  31. }
  32. impl std::convert::From<AppRevision> for AppPB {
  33. fn from(app_serde: AppRevision) -> Self {
  34. AppPB {
  35. id: app_serde.id,
  36. workspace_id: app_serde.workspace_id,
  37. name: app_serde.name,
  38. desc: app_serde.desc,
  39. belongings: app_serde.belongings.into(),
  40. version: app_serde.version,
  41. modified_time: app_serde.modified_time,
  42. create_time: app_serde.create_time,
  43. }
  44. }
  45. }
  46. #[derive(Eq, PartialEq, Debug, Default, ProtoBuf, Clone)]
  47. pub struct RepeatedAppPB {
  48. #[pb(index = 1)]
  49. pub items: Vec<AppPB>,
  50. }
  51. impl_def_and_def_mut!(RepeatedAppPB, AppPB);
  52. impl std::convert::From<Vec<AppRevision>> for RepeatedAppPB {
  53. fn from(values: Vec<AppRevision>) -> Self {
  54. let items = values.into_iter().map(|value| value.into()).collect::<Vec<AppPB>>();
  55. RepeatedAppPB { items }
  56. }
  57. }
  58. #[derive(ProtoBuf, Default)]
  59. pub struct CreateAppPayloadPB {
  60. #[pb(index = 1)]
  61. pub workspace_id: String,
  62. #[pb(index = 2)]
  63. pub name: String,
  64. #[pb(index = 3)]
  65. pub desc: String,
  66. #[pb(index = 4)]
  67. pub color_style: ColorStylePB,
  68. }
  69. #[derive(ProtoBuf, Default, Debug, Clone)]
  70. pub struct ColorStylePB {
  71. #[pb(index = 1)]
  72. pub theme_color: String,
  73. }
  74. #[derive(Debug)]
  75. pub struct CreateAppParams {
  76. pub workspace_id: String,
  77. pub name: String,
  78. pub desc: String,
  79. pub color_style: ColorStylePB,
  80. }
  81. impl TryInto<CreateAppParams> for CreateAppPayloadPB {
  82. type Error = ErrorCode;
  83. fn try_into(self) -> Result<CreateAppParams, Self::Error> {
  84. let name = AppName::parse(self.name)?;
  85. let id = WorkspaceIdentify::parse(self.workspace_id)?;
  86. let color_style = AppColorStyle::parse(self.color_style.theme_color.clone())?;
  87. Ok(CreateAppParams {
  88. workspace_id: id.0,
  89. name: name.0,
  90. desc: self.desc,
  91. color_style: color_style.into(),
  92. })
  93. }
  94. }
  95. impl std::convert::From<AppColorStyle> for ColorStylePB {
  96. fn from(data: AppColorStyle) -> Self {
  97. ColorStylePB {
  98. theme_color: data.theme_color,
  99. }
  100. }
  101. }
  102. #[derive(ProtoBuf, Default, Clone, Debug)]
  103. pub struct AppIdPB {
  104. #[pb(index = 1)]
  105. pub value: String,
  106. }
  107. impl AppIdPB {
  108. pub fn new(app_id: &str) -> Self {
  109. Self {
  110. value: app_id.to_string(),
  111. }
  112. }
  113. }
  114. #[derive(ProtoBuf, Default)]
  115. pub struct UpdateAppPayloadPB {
  116. #[pb(index = 1)]
  117. pub app_id: String,
  118. #[pb(index = 2, one_of)]
  119. pub name: Option<String>,
  120. #[pb(index = 3, one_of)]
  121. pub desc: Option<String>,
  122. #[pb(index = 4, one_of)]
  123. pub color_style: Option<ColorStylePB>,
  124. #[pb(index = 5, one_of)]
  125. pub is_trash: Option<bool>,
  126. }
  127. #[derive(Debug, Clone)]
  128. pub struct UpdateAppParams {
  129. pub app_id: String,
  130. pub name: Option<String>,
  131. pub desc: Option<String>,
  132. pub color_style: Option<ColorStylePB>,
  133. pub is_trash: Option<bool>,
  134. }
  135. impl UpdateAppParams {
  136. pub fn new(app_id: &str) -> Self {
  137. Self {
  138. app_id: app_id.to_string(),
  139. name: None,
  140. desc: None,
  141. color_style: None,
  142. is_trash: None,
  143. }
  144. }
  145. pub fn name(mut self, name: &str) -> Self {
  146. self.name = Some(name.to_string());
  147. self
  148. }
  149. pub fn desc(mut self, desc: &str) -> Self {
  150. self.desc = Some(desc.to_string());
  151. self
  152. }
  153. pub fn trash(mut self) -> Self {
  154. self.is_trash = Some(true);
  155. self
  156. }
  157. }
  158. impl TryInto<UpdateAppParams> for UpdateAppPayloadPB {
  159. type Error = ErrorCode;
  160. fn try_into(self) -> Result<UpdateAppParams, Self::Error> {
  161. let app_id = AppIdentify::parse(self.app_id)?.0;
  162. let name = match self.name {
  163. None => None,
  164. Some(name) => Some(AppName::parse(name)?.0),
  165. };
  166. let color_style = match self.color_style {
  167. None => None,
  168. Some(color_style) => Some(AppColorStyle::parse(color_style.theme_color)?.into()),
  169. };
  170. Ok(UpdateAppParams {
  171. app_id,
  172. name,
  173. desc: self.desc,
  174. color_style,
  175. is_trash: self.is_trash,
  176. })
  177. }
  178. }