use std::convert::TryFrom;
use std::str::FromStr;
use email_address::EmailAddress;
use phonenumber::PhoneNumber;
use uuid::Uuid;
use crate::domain::entity::{Role, User};
use crate::infrastructure::schema::users;
#[derive(Queryable, Insertable, AsChangeset)]
#[table_name = "users"]
pub struct UserDiesel {
id: String,
email: String,
phone: String,
role: String,
password: String,
}
impl Into<User> for UserDiesel {
fn into(self) -> User {
User {
id: Uuid::parse_str(&*self.id).unwrap(),
email: EmailAddress::from_str(&*self.email).unwrap(),
phone: PhoneNumber::from_str(&*self.phone).unwrap(),
role: Role::try_from(self.role).unwrap(),
password: self.password,
}
}
}
impl From<User> for UserDiesel {
fn from(user: User) -> Self {
Self {
id: user.id.to_string(),
email: user.email.to_string(),
phone: user.phone.to_string(),
role: user.role.to_string(),
password: user.password,
}
}
}