Implement User conversions

This commit is contained in:
Ethan Girouard 2024-02-04 17:43:17 -05:00
parent 35eee117d7
commit ee5e869442
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146

View File

@ -44,6 +44,17 @@ pub struct NewUser {
pub password: String,
}
/// Convert a User into a NewUser, omitting the id and created_at fields
impl From<User> for NewUser {
fn from(user: User) -> NewUser {
NewUser {
username: user.username,
email: user.email,
password: user.password,
}
}
}
/// Model for a "Public User", used for returning user data to the client
/// This model omits the password field, so that the hashed password is not sent to the client
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable))]
@ -60,3 +71,15 @@ pub struct PublicUser {
/// The time the user was created
pub created_at: SystemTime,
}
/// Convert a User into a PublicUser, omitting the password field
impl From<User> for PublicUser {
fn from(user: User) -> PublicUser {
PublicUser {
id: user.id,
username: user.username,
email: user.email,
created_at: user.created_at,
}
}
}