user_profile.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use flowy_derive::ProtoBuf;
  2. use std::convert::TryInto;
  3. use crate::{
  4. entities::parser::{UserEmail, UserId, UserName, UserPassword},
  5. errors::ErrorCode,
  6. };
  7. #[derive(Default, ProtoBuf)]
  8. pub struct UserTokenPB {
  9. #[pb(index = 1)]
  10. pub token: String,
  11. }
  12. #[derive(ProtoBuf, Default, Debug, PartialEq, Eq, Clone)]
  13. pub struct UserProfilePB {
  14. #[pb(index = 1)]
  15. pub id: String,
  16. #[pb(index = 2)]
  17. pub email: String,
  18. #[pb(index = 3)]
  19. pub name: String,
  20. #[pb(index = 4)]
  21. pub token: String,
  22. }
  23. #[derive(ProtoBuf, Default)]
  24. pub struct UpdateUserProfilePayloadPB {
  25. #[pb(index = 1)]
  26. pub id: String,
  27. #[pb(index = 2, one_of)]
  28. pub name: Option<String>,
  29. #[pb(index = 3, one_of)]
  30. pub email: Option<String>,
  31. #[pb(index = 4, one_of)]
  32. pub password: Option<String>,
  33. }
  34. impl UpdateUserProfilePayloadPB {
  35. pub fn new(id: &str) -> Self {
  36. Self {
  37. id: id.to_owned(),
  38. ..Default::default()
  39. }
  40. }
  41. pub fn name(mut self, name: &str) -> Self {
  42. self.name = Some(name.to_owned());
  43. self
  44. }
  45. pub fn email(mut self, email: &str) -> Self {
  46. self.email = Some(email.to_owned());
  47. self
  48. }
  49. pub fn password(mut self, password: &str) -> Self {
  50. self.password = Some(password.to_owned());
  51. self
  52. }
  53. }
  54. #[derive(ProtoBuf, Default, Clone, Debug)]
  55. pub struct UpdateUserProfileParams {
  56. #[pb(index = 1)]
  57. pub id: String,
  58. #[pb(index = 2, one_of)]
  59. pub name: Option<String>,
  60. #[pb(index = 3, one_of)]
  61. pub email: Option<String>,
  62. #[pb(index = 4, one_of)]
  63. pub password: Option<String>,
  64. }
  65. impl UpdateUserProfileParams {
  66. pub fn new(user_id: &str) -> Self {
  67. Self {
  68. id: user_id.to_owned(),
  69. name: None,
  70. email: None,
  71. password: None,
  72. }
  73. }
  74. pub fn name(mut self, name: &str) -> Self {
  75. self.name = Some(name.to_owned());
  76. self
  77. }
  78. pub fn email(mut self, email: &str) -> Self {
  79. self.email = Some(email.to_owned());
  80. self
  81. }
  82. pub fn password(mut self, password: &str) -> Self {
  83. self.password = Some(password.to_owned());
  84. self
  85. }
  86. }
  87. impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
  88. type Error = ErrorCode;
  89. fn try_into(self) -> Result<UpdateUserProfileParams, Self::Error> {
  90. let id = UserId::parse(self.id)?.0;
  91. let name = match self.name {
  92. None => None,
  93. Some(name) => Some(UserName::parse(name)?.0),
  94. };
  95. let email = match self.email {
  96. None => None,
  97. Some(email) => Some(UserEmail::parse(email)?.0),
  98. };
  99. let password = match self.password {
  100. None => None,
  101. Some(password) => Some(UserPassword::parse(password)?.0),
  102. };
  103. Ok(UpdateUserProfileParams {
  104. id,
  105. name,
  106. email,
  107. password,
  108. })
  109. }
  110. }