AppFlowy/frontend/rust-lib/flowy-user/src/entities/user_profile.rs

165 lines
3.5 KiB
Rust
Raw Normal View History

2021-09-17 11:03:46 +00:00
use flowy_derive::ProtoBuf;
use std::convert::TryInto;
use crate::{
2022-08-06 14:31:55 +00:00
entities::parser::{UserEmail, UserIcon, UserId, UserName, UserPassword},
errors::ErrorCode,
};
2021-08-31 09:25:08 +00:00
#[derive(Default, ProtoBuf)]
2022-07-19 06:40:56 +00:00
pub struct UserTokenPB {
2021-08-31 09:25:08 +00:00
#[pb(index = 1)]
pub token: String,
}
2021-09-04 07:12:53 +00:00
#[derive(ProtoBuf, Default, Debug, PartialEq, Eq, Clone)]
2022-07-19 06:40:56 +00:00
pub struct UserProfilePB {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub email: String,
#[pb(index = 3)]
pub name: String,
2021-09-08 10:25:32 +00:00
#[pb(index = 4)]
pub token: String,
2022-08-06 14:31:55 +00:00
#[pb(index = 5)]
pub icon: String,
}
2021-08-31 09:25:08 +00:00
#[derive(ProtoBuf, Default)]
2022-07-19 06:40:56 +00:00
pub struct UpdateUserProfilePayloadPB {
2021-08-31 09:25:08 +00:00
#[pb(index = 1)]
pub id: String,
#[pb(index = 2, one_of)]
pub name: Option<String>,
#[pb(index = 3, one_of)]
pub email: Option<String>,
#[pb(index = 4, one_of)]
pub password: Option<String>,
2022-08-06 14:31:55 +00:00
#[pb(index = 5, one_of)]
pub icon: Option<String>,
2021-08-31 09:25:08 +00:00
}
2022-07-19 06:40:56 +00:00
impl UpdateUserProfilePayloadPB {
2021-08-31 09:25:08 +00:00
pub fn new(id: &str) -> Self {
Self {
id: id.to_owned(),
..Default::default()
}
}
2021-08-31 09:25:08 +00:00
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
}
pub fn email(mut self, email: &str) -> Self {
self.email = Some(email.to_owned());
self
}
pub fn password(mut self, password: &str) -> Self {
self.password = Some(password.to_owned());
self
}
2022-08-06 14:31:55 +00:00
pub fn icon(mut self, icon: &str) -> Self {
self.icon = Some(icon.to_owned());
self
}
2021-08-31 09:25:08 +00:00
}
2021-09-06 08:18:34 +00:00
#[derive(ProtoBuf, Default, Clone, Debug)]
pub struct UpdateUserProfileParams {
2021-08-31 09:25:08 +00:00
#[pb(index = 1)]
pub id: String,
#[pb(index = 2, one_of)]
pub name: Option<String>,
#[pb(index = 3, one_of)]
pub email: Option<String>,
#[pb(index = 4, one_of)]
pub password: Option<String>,
2022-08-06 14:31:55 +00:00
#[pb(index = 5, one_of)]
pub icon: Option<String>,
2021-08-31 09:25:08 +00:00
}
impl UpdateUserProfileParams {
2021-09-01 08:08:32 +00:00
pub fn new(user_id: &str) -> Self {
Self {
id: user_id.to_owned(),
2022-07-19 06:40:56 +00:00
name: None,
email: None,
password: None,
2022-08-06 14:31:55 +00:00
icon: None,
2021-09-01 08:08:32 +00:00
}
}
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
}
pub fn email(mut self, email: &str) -> Self {
self.email = Some(email.to_owned());
self
}
pub fn password(mut self, password: &str) -> Self {
self.password = Some(password.to_owned());
self
}
2022-08-06 14:31:55 +00:00
pub fn icon(mut self, icon: &str) -> Self {
self.icon = Some(icon.to_owned());
self
}
2021-09-01 08:08:32 +00:00
}
2022-07-19 06:40:56 +00:00
impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
type Error = ErrorCode;
2021-08-31 09:25:08 +00:00
fn try_into(self) -> Result<UpdateUserProfileParams, Self::Error> {
let id = UserId::parse(self.id)?.0;
2021-08-31 09:25:08 +00:00
let name = match self.name {
None => None,
Some(name) => Some(UserName::parse(name)?.0),
2021-08-31 09:25:08 +00:00
};
let email = match self.email {
None => None,
Some(email) => Some(UserEmail::parse(email)?.0),
2021-08-31 09:25:08 +00:00
};
let password = match self.password {
None => None,
Some(password) => Some(UserPassword::parse(password)?.0),
2021-08-31 09:25:08 +00:00
};
2022-08-06 14:31:55 +00:00
let icon = match self.icon {
None => None,
Some(icon) => Some(UserIcon::parse(icon)?.0),
};
Ok(UpdateUserProfileParams {
id,
name,
email,
password,
2022-08-06 14:31:55 +00:00
icon,
})
2021-08-31 09:25:08 +00:00
}
}