view_create.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. use crate::{
  2. entities::trash::{Trash, TrashType},
  3. errors::ErrorCode,
  4. impl_def_and_def_mut,
  5. parser::{
  6. app::AppId,
  7. view::{ViewName, ViewThumbnail},
  8. },
  9. };
  10. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  11. use flowy_document::services::doc::doc_initial_string;
  12. use std::convert::TryInto;
  13. #[derive(PartialEq, Debug, ProtoBuf_Enum, Clone)]
  14. pub enum ViewType {
  15. Blank = 0,
  16. Doc = 1,
  17. }
  18. impl std::default::Default for ViewType {
  19. fn default() -> Self { ViewType::Blank }
  20. }
  21. impl std::convert::From<i32> for ViewType {
  22. fn from(val: i32) -> Self {
  23. match val {
  24. 1 => ViewType::Doc,
  25. 0 => ViewType::Blank,
  26. _ => {
  27. log::error!("Invalid view type: {}", val);
  28. ViewType::Blank
  29. },
  30. }
  31. }
  32. }
  33. #[derive(Default, ProtoBuf)]
  34. pub struct CreateViewRequest {
  35. #[pb(index = 1)]
  36. pub belong_to_id: String,
  37. #[pb(index = 2)]
  38. pub name: String,
  39. #[pb(index = 3)]
  40. pub desc: String,
  41. #[pb(index = 4, one_of)]
  42. pub thumbnail: Option<String>,
  43. #[pb(index = 5)]
  44. pub view_type: ViewType,
  45. }
  46. #[derive(Default, ProtoBuf, Debug, Clone)]
  47. pub struct CreateViewParams {
  48. #[pb(index = 1)]
  49. pub belong_to_id: String,
  50. #[pb(index = 2)]
  51. pub name: String,
  52. #[pb(index = 3)]
  53. pub desc: String,
  54. #[pb(index = 4)]
  55. pub thumbnail: String,
  56. #[pb(index = 5)]
  57. pub view_type: ViewType,
  58. #[pb(index = 6)]
  59. pub data: String,
  60. }
  61. impl CreateViewParams {
  62. pub fn new(belong_to_id: String, name: String, desc: String, view_type: ViewType, thumbnail: String) -> Self {
  63. Self {
  64. belong_to_id,
  65. name,
  66. desc,
  67. thumbnail,
  68. view_type,
  69. data: doc_initial_string(),
  70. }
  71. }
  72. }
  73. impl TryInto<CreateViewParams> for CreateViewRequest {
  74. type Error = ErrorCode;
  75. fn try_into(self) -> Result<CreateViewParams, Self::Error> {
  76. let name = ViewName::parse(self.name)?.0;
  77. let belong_to_id = AppId::parse(self.belong_to_id)?.0;
  78. let thumbnail = match self.thumbnail {
  79. None => "".to_string(),
  80. Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
  81. };
  82. Ok(CreateViewParams::new(
  83. belong_to_id,
  84. name,
  85. self.desc,
  86. self.view_type,
  87. thumbnail,
  88. ))
  89. }
  90. }
  91. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  92. pub struct View {
  93. #[pb(index = 1)]
  94. pub id: String,
  95. #[pb(index = 2)]
  96. pub belong_to_id: String,
  97. #[pb(index = 3)]
  98. pub name: String,
  99. #[pb(index = 4)]
  100. pub desc: String,
  101. #[pb(index = 5)]
  102. pub view_type: ViewType,
  103. #[pb(index = 6)]
  104. pub version: i64,
  105. #[pb(index = 7)]
  106. pub belongings: RepeatedView,
  107. #[pb(index = 8)]
  108. pub modified_time: i64,
  109. #[pb(index = 9)]
  110. pub create_time: i64,
  111. }
  112. #[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
  113. pub struct RepeatedView {
  114. #[pb(index = 1)]
  115. pub items: Vec<View>,
  116. }
  117. impl_def_and_def_mut!(RepeatedView, View);
  118. impl std::convert::Into<Trash> for View {
  119. fn into(self) -> Trash {
  120. Trash {
  121. id: self.id,
  122. name: self.name,
  123. modified_time: self.modified_time,
  124. create_time: self.create_time,
  125. ty: TrashType::View,
  126. }
  127. }
  128. }