pub struct User {
pub id: Uuid,
pub is_admin: bool,
pub username: String,
pub full_name: String,
pub email: String,
pub password_hash: String,
pub api_key: Uuid,
}Expand description
Represents a user of the system
Fields§
§id: UuidUser ID
is_admin: boolIs administrator?
username: StringUsername
full_name: StringFull name
email: StringEmail address
password_hash: StringPassword hash (encrypted)
api_key: UuidAPI key
Implementations§
Source§impl User
impl User
Sourcepub fn new_id() -> Uuid
pub fn new_id() -> Uuid
Generates a new user id
§Returns
User id
§Examples
Initialize a user with a new user id and api key and then print it
use data_model::User;
let user = User {
id: User::new_id(),
is_admin: false,
username: "john_smith".to_string(),
full_name: "John Smith".to_string(),
email: "john_smith@company.com".to_string(),
password_hash: "encrypted_hash".to_string(),
api_key: User::new_api_key(),
};
println!("User: {:?}", user);Sourcepub fn new_api_key() -> Uuid
pub fn new_api_key() -> Uuid
Generates a new API key
§Returns
User id
§Examples
Initialize a user with a new user id and api key and then print it
use data_model::User;
let user = User {
id: User::new_id(),
is_admin: false,
username: "john_smith".to_string(),
full_name: "John Smith".to_string(),
email: "john_smith@company.com".to_string(),
password_hash: "encrypted_hash".to_string(),
api_key: User::new_api_key(),
};
println!("User: {:?}", user);Sourcepub fn to_json(&self) -> Result<String, Error>
pub fn to_json(&self) -> Result<String, Error>
Generates the JSON string representation for the user
§Returns
JSON string representing the user
§Examples
Initialize a user, convert it to JSON and print it
use data_model::User;
let user = User {
id: User::new_id(),
is_admin: false,
username: "john_smith".to_string(),
full_name: "John Smith".to_string(),
email: "john_smith@company.com".to_string(),
password_hash: "encrypted_hash".to_string(),
api_key: User::new_api_key(),
};
match user.to_json() {
Ok(json) => {
println!("JSON: {}", json);
}
Err(error) => {
panic!(
"failed to convert user to JSON => {}",
error
);
}
}Sourcepub fn from_json(&mut self, json: &str) -> Result<(), Error>
pub fn from_json(&mut self, json: &str) -> Result<(), Error>
Initializes a user instance by reading the JSON string content provided
§Returns
This function will return an error if the JSON could not be read
§Examples
Create a default user and then reinitialize it from a JSON string definition
use data_model::User;
let mut user = User::default();
let json = r#"{"id":"02345678-1234-5678-1234-567890123456","is_admin":false,"username":"john_smith","full_name":"John Smith","email":"john_smith@company.com","password_hash":"some_password_hash","api_key":"12345678-1234-5678-1234-567890123456"}"#;
match user.from_json(json) {
Ok(()) => {
println!(
"JSON successfully read into User => username = {}",
user.username
);
}
Err(error) => {
panic!(
"failed to read JSON into user => {}",
error
);
}
}Sourcepub fn validate(&self) -> Result<(), Error>
pub fn validate(&self) -> Result<(), Error>
Validates the content of a user object
§Returns
JSON string representing the user
§Examples
Initialize a user with an invalid email address (missing @) and validate it
use data_model::User;
let user = User {
id: User::new_id(),
is_admin: false,
username: "john_smith".to_string(),
full_name: "John Smith".to_string(),
email: "bad_email".to_string(),
password_hash: "encrypted_hash".to_string(),
api_key: User::new_api_key(),
};
match user.validate() {
Ok(_) => {
println!("Validation passed");
}
Err(error) => {
println!(
"Validation failed => {}",
error
);
}
}