view_query.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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::QueryDocParams;
  7. use std::convert::TryInto;
  8. #[derive(Default, ProtoBuf)]
  9. pub struct QueryViewRequest {
  10. #[pb(index = 1)]
  11. pub view_id: String,
  12. #[pb(index = 2)]
  13. pub is_trash: bool,
  14. #[pb(index = 3)]
  15. pub read_belongings: bool,
  16. }
  17. impl QueryViewRequest {
  18. pub fn new(view_id: &str) -> Self {
  19. Self {
  20. view_id: view_id.to_owned(),
  21. is_trash: false,
  22. read_belongings: false,
  23. }
  24. }
  25. pub fn trash(mut self) -> Self {
  26. self.is_trash = true;
  27. self
  28. }
  29. }
  30. #[derive(Default, ProtoBuf, Clone, Debug)]
  31. pub struct QueryViewParams {
  32. #[pb(index = 1)]
  33. pub view_id: String,
  34. #[pb(index = 2)]
  35. pub is_trash: bool,
  36. #[pb(index = 3)]
  37. pub read_belongings: bool,
  38. }
  39. impl QueryViewParams {
  40. pub fn new(view_id: &str) -> Self {
  41. Self {
  42. view_id: view_id.to_owned(),
  43. ..Default::default()
  44. }
  45. }
  46. pub fn trash(mut self) -> Self {
  47. self.is_trash = true;
  48. self
  49. }
  50. pub fn read_belongings(mut self) -> Self {
  51. self.read_belongings = true;
  52. self
  53. }
  54. }
  55. impl std::convert::Into<QueryDocParams> for QueryViewParams {
  56. fn into(self) -> QueryDocParams { QueryDocParams { doc_id: self.view_id } }
  57. }
  58. impl TryInto<QueryViewParams> for QueryViewRequest {
  59. type Error = WorkspaceError;
  60. fn try_into(self) -> Result<QueryViewParams, Self::Error> {
  61. let view_id = ViewId::parse(self.view_id)
  62. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewIdInvalid).msg(e).build())?
  63. .0;
  64. Ok(QueryViewParams {
  65. view_id,
  66. is_trash: self.is_trash,
  67. read_belongings: self.read_belongings,
  68. })
  69. }
  70. }
  71. #[derive(Default, ProtoBuf)]
  72. pub struct OpenViewRequest {
  73. #[pb(index = 1)]
  74. pub view_id: String,
  75. }
  76. impl std::convert::TryInto<QueryDocParams> for OpenViewRequest {
  77. type Error = WorkspaceError;
  78. fn try_into(self) -> Result<QueryDocParams, Self::Error> {
  79. let view_id = ViewId::parse(self.view_id)
  80. .map_err(|e| ErrorBuilder::new(ErrorCode::ViewIdInvalid).msg(e).build())?
  81. .0;
  82. Ok(QueryDocParams { doc_id: view_id })
  83. }
  84. }