view_update.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use crate::{
  2. entities::view::parser::{ViewId, *},
  3. errors::{ErrorBuilder, ErrorCode, WorkspaceError},
  4. };
  5. use flowy_derive::ProtoBuf;
  6. use flowy_document::entities::doc::UpdateDocParams;
  7. use std::convert::TryInto;
  8. #[derive(Default, ProtoBuf)]
  9. pub struct UpdateViewRequest {
  10. #[pb(index = 1)]
  11. pub view_id: String,
  12. #[pb(index = 2, one_of)]
  13. pub name: Option<String>,
  14. #[pb(index = 3, one_of)]
  15. pub desc: Option<String>,
  16. #[pb(index = 4, one_of)]
  17. pub thumbnail: Option<String>,
  18. #[pb(index = 5, one_of)]
  19. pub is_trash: Option<bool>,
  20. }
  21. #[derive(Default, ProtoBuf, Clone, Debug)]
  22. pub struct UpdateViewParams {
  23. #[pb(index = 1)]
  24. pub view_id: String,
  25. #[pb(index = 2, one_of)]
  26. pub name: Option<String>,
  27. #[pb(index = 3, one_of)]
  28. pub desc: Option<String>,
  29. #[pb(index = 4, one_of)]
  30. pub thumbnail: Option<String>,
  31. #[pb(index = 5, one_of)]
  32. pub is_trash: Option<bool>,
  33. }
  34. impl UpdateViewParams {
  35. pub fn new(view_id: &str) -> Self {
  36. Self {
  37. view_id: view_id.to_owned(),
  38. ..Default::default()
  39. }
  40. }
  41. pub fn trash(mut self) -> Self {
  42. self.is_trash = Some(true);
  43. self
  44. }
  45. pub fn name(mut self, name: &str) -> Self {
  46. self.name = Some(name.to_owned());
  47. self
  48. }
  49. pub fn desc(mut self, desc: &str) -> Self {
  50. self.desc = Some(desc.to_owned());
  51. self
  52. }
  53. }
  54. impl TryInto<UpdateViewParams> for UpdateViewRequest {
  55. type Error = WorkspaceError;
  56. fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
  57. let view_id = ViewId::parse(self.view_id)
  58. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewIdInvalid).msg(e).build())?
  59. .0;
  60. let name = match self.name {
  61. None => None,
  62. Some(name) => Some(
  63. ViewName::parse(name)
  64. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewNameInvalid).msg(e).build())?
  65. .0,
  66. ),
  67. };
  68. let desc = match self.desc {
  69. None => None,
  70. Some(desc) => Some(
  71. ViewDesc::parse(desc)
  72. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewDescInvalid).msg(e).build())?
  73. .0,
  74. ),
  75. };
  76. let thumbnail = match self.thumbnail {
  77. None => None,
  78. Some(thumbnail) => Some(
  79. ViewThumbnail::parse(thumbnail)
  80. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewThumbnailInvalid).msg(e).build())?
  81. .0,
  82. ),
  83. };
  84. Ok(UpdateViewParams {
  85. view_id,
  86. name,
  87. desc,
  88. thumbnail,
  89. is_trash: self.is_trash,
  90. })
  91. }
  92. }
  93. #[derive(Default, ProtoBuf)]
  94. pub struct UpdateViewDataRequest {
  95. #[pb(index = 1)]
  96. pub view_id: String,
  97. #[pb(index = 2)]
  98. pub data: String,
  99. }
  100. impl TryInto<UpdateDocParams> for UpdateViewDataRequest {
  101. type Error = WorkspaceError;
  102. fn try_into(self) -> Result<UpdateDocParams, Self::Error> {
  103. let view_id = ViewId::parse(self.view_id)
  104. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewIdInvalid).msg(e).build())?
  105. .0;
  106. Ok(UpdateDocParams {
  107. id: view_id,
  108. data: Some(self.data),
  109. })
  110. }
  111. }