account.rs (1093B)
1 use crate::data::AccountCreationInput; 2 use crate::validate::ValidationError; 3 use serde::{Deserialize, Serialize}; 4 5 /// An account 6 #[derive(Deserialize, Serialize)] 7 pub struct Account { 8 /// The username of the user, also used as an unique identifier 9 pub username: String, 10 /// The password of the user 11 pub password: String, 12 /// The email address the user 13 pub email: Option<String>, 14 } 15 16 impl Account { 17 /// Create new account 18 pub fn create(payload: AccountCreationInput) -> Result<Self, ValidationError> { 19 payload.is_valid()?; 20 21 let account = Account { 22 username: payload.username, 23 email: payload.email, 24 password: payload.password, 25 }; 26 27 Ok(account) 28 } 29 } 30 31 /// Account Public Data 32 #[derive(Deserialize, Serialize)] 33 pub struct PublicAccount { 34 /// The username of the user, also used as an unique identifier 35 pub username: String, 36 } 37 38 impl From<Account> for PublicAccount { 39 fn from(account: Account) -> Self { 40 PublicAccount { 41 username: account.username, 42 } 43 } 44 }