use std::sync::Arc;
use crate::{
common::Result,
connection::server::{server_manager::ServerManager, server_routing::ServerRouting},
info::UserInfo,
};
#[derive(Clone, Debug)]
pub struct User {
name: String,
password: Option<String>,
server_manager: Arc<ServerManager>,
}
impl User {
pub(crate) fn new(name: String, password: Option<String>, server_manager: Arc<ServerManager>) -> Self {
Self { name, password, server_manager }
}
pub(crate) fn from_info(user_info: UserInfo, server_manager: Arc<ServerManager>) -> Self {
Self::new(user_info.name, user_info.password, server_manager)
}
pub fn name(&self) -> &str {
self.name.as_str()
}
pub fn password(&self) -> Option<&str> {
self.password.as_ref().map(|value| value.as_str())
}
#[cfg_attr(feature = "sync", doc = "user.update_password(password);")]
#[cfg_attr(not(feature = "sync"), doc = "user.update_password(password).await;")]
#[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
pub async fn update_password(&self, password: impl Into<String>) -> Result<()> {
let password = password.into();
self.server_manager
.execute(ServerRouting::Auto, |server_connection| {
let name = self.name.clone();
let password = password.clone();
async move { server_connection.update_password(name, password).await }
})
.await
}
#[cfg_attr(feature = "sync", doc = "user.delete();")]
#[cfg_attr(not(feature = "sync"), doc = "user.delete().await;")]
#[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
pub async fn delete(self) -> Result {
self.server_manager
.execute(ServerRouting::Auto, |server_connection| {
let name = self.name.clone();
async move { server_connection.delete_user(name).await }
})
.await
}
}