workspace_query.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. use crate::{entities::workspace::parser::*, errors::*};
  2. use flowy_derive::ProtoBuf;
  3. use std::convert::TryInto;
  4. #[derive(Default, ProtoBuf, Clone)]
  5. pub struct QueryWorkspaceRequest {
  6. // return all workspace if workspace_id is None
  7. #[pb(index = 1, one_of)]
  8. pub workspace_id: Option<String>,
  9. }
  10. impl QueryWorkspaceRequest {
  11. pub fn new() -> Self { Self { workspace_id: None } }
  12. pub fn workspace_id(mut self, workspace_id: &str) -> Self {
  13. self.workspace_id = Some(workspace_id.to_owned());
  14. self
  15. }
  16. }
  17. // Read all workspaces if the workspace_id is None
  18. #[derive(Clone, ProtoBuf, Default, Debug)]
  19. pub struct QueryWorkspaceParams {
  20. #[pb(index = 1, one_of)]
  21. pub workspace_id: Option<String>,
  22. }
  23. impl QueryWorkspaceParams {
  24. pub fn new() -> Self {
  25. Self {
  26. workspace_id: None,
  27. ..Default::default()
  28. }
  29. }
  30. pub fn workspace_id(mut self, workspace_id: &str) -> Self {
  31. self.workspace_id = Some(workspace_id.to_string());
  32. self
  33. }
  34. }
  35. impl TryInto<QueryWorkspaceParams> for QueryWorkspaceRequest {
  36. type Error = WorkspaceError;
  37. fn try_into(self) -> Result<QueryWorkspaceParams, Self::Error> {
  38. let workspace_id = match self.workspace_id {
  39. None => None,
  40. Some(workspace_id) => Some(
  41. WorkspaceId::parse(workspace_id)
  42. .map_err(|e| ErrorBuilder::new(ErrorCode::WorkspaceIdInvalid).msg(e).build())?
  43. .0,
  44. ),
  45. };
  46. Ok(QueryWorkspaceParams { workspace_id })
  47. }
  48. }