user_profile.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use flowy_derive::ProtoBuf;
  2. use std::convert::TryInto;
  3. use crate::{
  4. entities::parser::{UserEmail, UserIcon, 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. #[pb(index = 5)]
  23. pub icon_url: String,
  24. }
  25. #[derive(ProtoBuf, Default)]
  26. pub struct UpdateUserProfilePayloadPB {
  27. #[pb(index = 1)]
  28. pub id: String,
  29. #[pb(index = 2, one_of)]
  30. pub name: Option<String>,
  31. #[pb(index = 3, one_of)]
  32. pub email: Option<String>,
  33. #[pb(index = 4, one_of)]
  34. pub password: Option<String>,
  35. #[pb(index = 5, one_of)]
  36. pub icon_url: Option<String>,
  37. }
  38. impl UpdateUserProfilePayloadPB {
  39. pub fn new(id: &str) -> Self {
  40. Self {
  41. id: id.to_owned(),
  42. ..Default::default()
  43. }
  44. }
  45. pub fn name(mut self, name: &str) -> Self {
  46. self.name = Some(name.to_owned());
  47. self
  48. }
  49. pub fn email(mut self, email: &str) -> Self {
  50. self.email = Some(email.to_owned());
  51. self
  52. }
  53. pub fn password(mut self, password: &str) -> Self {
  54. self.password = Some(password.to_owned());
  55. self
  56. }
  57. pub fn icon_url(mut self, icon_url: &str) -> Self {
  58. self.icon_url = Some(icon_url.to_owned());
  59. self
  60. }
  61. }
  62. #[derive(ProtoBuf, Default, Clone, Debug)]
  63. pub struct UpdateUserProfileParams {
  64. #[pb(index = 1)]
  65. pub id: String,
  66. #[pb(index = 2, one_of)]
  67. pub name: Option<String>,
  68. #[pb(index = 3, one_of)]
  69. pub email: Option<String>,
  70. #[pb(index = 4, one_of)]
  71. pub password: Option<String>,
  72. #[pb(index = 5, one_of)]
  73. pub icon_url: Option<String>,
  74. }
  75. impl UpdateUserProfileParams {
  76. pub fn new(user_id: &str) -> Self {
  77. Self {
  78. id: user_id.to_owned(),
  79. name: None,
  80. email: None,
  81. password: None,
  82. icon_url: None,
  83. }
  84. }
  85. pub fn name(mut self, name: &str) -> Self {
  86. self.name = Some(name.to_owned());
  87. self
  88. }
  89. pub fn email(mut self, email: &str) -> Self {
  90. self.email = Some(email.to_owned());
  91. self
  92. }
  93. pub fn password(mut self, password: &str) -> Self {
  94. self.password = Some(password.to_owned());
  95. self
  96. }
  97. pub fn icon_url(mut self, icon_url: &str) -> Self {
  98. self.icon_url = Some(icon_url.to_owned());
  99. self
  100. }
  101. }
  102. impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
  103. type Error = ErrorCode;
  104. fn try_into(self) -> Result<UpdateUserProfileParams, Self::Error> {
  105. let id = UserId::parse(self.id)?.0;
  106. let name = match self.name {
  107. None => None,
  108. Some(name) => Some(UserName::parse(name)?.0),
  109. };
  110. let email = match self.email {
  111. None => None,
  112. Some(email) => Some(UserEmail::parse(email)?.0),
  113. };
  114. let password = match self.password {
  115. None => None,
  116. Some(password) => Some(UserPassword::parse(password)?.0),
  117. };
  118. let icon_url = match self.icon_url {
  119. None => None,
  120. Some(icon_url) => Some(UserIcon::parse(icon_url)?.0),
  121. };
  122. Ok(UpdateUserProfileParams {
  123. id,
  124. name,
  125. email,
  126. password,
  127. icon_url,
  128. })
  129. }
  130. }