user_profile.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. use flowy_derive::ProtoBuf;
  2. use std::convert::TryInto;
  3. use crate::{
  4. errors::ErrorCode,
  5. parser::{UserEmail, UserId, UserName, UserPassword},
  6. };
  7. #[derive(Default, ProtoBuf)]
  8. pub struct UserToken {
  9. #[pb(index = 1)]
  10. pub token: String,
  11. }
  12. #[derive(ProtoBuf, Default, Debug, PartialEq, Eq, Clone)]
  13. pub struct UserProfile {
  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 UpdateUserRequest {
  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 UpdateUserRequest {
  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 UpdateUserParams {
  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 UpdateUserParams {
  66. pub fn new(user_id: &str) -> Self {
  67. Self {
  68. id: user_id.to_owned(),
  69. ..Default::default()
  70. }
  71. }
  72. pub fn name(mut self, name: &str) -> Self {
  73. self.name = Some(name.to_owned());
  74. self
  75. }
  76. pub fn email(mut self, email: &str) -> Self {
  77. self.email = Some(email.to_owned());
  78. self
  79. }
  80. pub fn password(mut self, password: &str) -> Self {
  81. self.password = Some(password.to_owned());
  82. self
  83. }
  84. }
  85. impl TryInto<UpdateUserParams> for UpdateUserRequest {
  86. type Error = ErrorCode;
  87. fn try_into(self) -> Result<UpdateUserParams, Self::Error> {
  88. let id = UserId::parse(self.id)?.0;
  89. let name = match self.name {
  90. None => None,
  91. Some(name) => Some(UserName::parse(name)?.0),
  92. };
  93. let email = match self.email {
  94. None => None,
  95. Some(email) => Some(UserEmail::parse(email)?.0),
  96. };
  97. let password = match self.password {
  98. None => None,
  99. Some(password) => Some(UserPassword::parse(password)?.0),
  100. };
  101. Ok(UpdateUserParams {
  102. id,
  103. name,
  104. email,
  105. password,
  106. })
  107. }
  108. }