app_query.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use crate::{entities::app::parser::AppId, errors::*};
  2. use flowy_derive::ProtoBuf;
  3. use std::convert::TryInto;
  4. #[derive(Default, ProtoBuf)]
  5. pub struct QueryAppRequest {
  6. #[pb(index = 1)]
  7. pub app_id: String,
  8. #[pb(index = 2)]
  9. pub read_belongings: bool,
  10. #[pb(index = 3)]
  11. pub is_trash: bool,
  12. }
  13. impl QueryAppRequest {
  14. pub fn new(app_id: &str) -> Self {
  15. QueryAppRequest {
  16. app_id: app_id.to_string(),
  17. read_belongings: false,
  18. is_trash: false,
  19. }
  20. }
  21. pub fn set_read_views(mut self, read_views: bool) -> Self {
  22. self.read_belongings = read_views;
  23. self
  24. }
  25. pub fn set_is_trash(mut self, is_trash: bool) -> Self {
  26. self.is_trash = is_trash;
  27. self
  28. }
  29. }
  30. #[derive(ProtoBuf, Default, Clone)]
  31. pub struct QueryAppParams {
  32. #[pb(index = 1)]
  33. pub app_id: String,
  34. #[pb(index = 2)]
  35. pub read_belongings: bool,
  36. #[pb(index = 3)]
  37. pub is_trash: bool,
  38. }
  39. impl QueryAppParams {
  40. pub fn new(app_id: &str) -> Self {
  41. Self {
  42. app_id: app_id.to_string(),
  43. ..Default::default()
  44. }
  45. }
  46. pub fn read_belongings(mut self) -> Self {
  47. self.read_belongings = true;
  48. self
  49. }
  50. pub fn trash(mut self) -> Self {
  51. self.is_trash = true;
  52. self
  53. }
  54. }
  55. impl TryInto<QueryAppParams> for QueryAppRequest {
  56. type Error = WorkspaceError;
  57. fn try_into(self) -> Result<QueryAppParams, Self::Error> {
  58. let app_id = AppId::parse(self.app_id)
  59. .map_err(|e| ErrorBuilder::new(ErrorCode::AppIdInvalid).msg(e).build())?
  60. .0;
  61. Ok(QueryAppParams {
  62. app_id,
  63. read_belongings: self.read_belongings,
  64. is_trash: self.is_trash,
  65. })
  66. }
  67. }