Format code

This commit is contained in:
2022-04-03 15:50:49 +02:00
parent 9236b91f12
commit b965fa6b4f
20 changed files with 149 additions and 106 deletions

View File

@ -37,4 +37,4 @@ impl AppConfig {
pub fn users_file(&self) -> PathBuf {
self.storage_path().join(USERS_LIST_FILE)
}
}
}

View File

@ -8,7 +8,10 @@ pub struct EntityManager<E> {
list: Vec<E>,
}
impl<E> EntityManager<E> where E: serde::Serialize + serde::de::DeserializeOwned + Eq + Clone {
impl<E> EntityManager<E>
where
E: serde::Serialize + serde::de::DeserializeOwned + Eq + Clone,
{
/// Open entity
pub fn open_or_create<A: AsRef<Path>>(path: A) -> Res<Self> {
if !path.as_ref().is_file() {
@ -37,7 +40,10 @@ impl<E> EntityManager<E> where E: serde::Serialize + serde::de::DeserializeOwned
/// Save the list
fn save(&self) -> Res {
Ok(std::fs::write(&self.file_path, serde_json::to_string(&self.list)?)?)
Ok(std::fs::write(
&self.file_path,
serde_json::to_string(&self.list)?,
)?)
}
/// Insert a new element in the list
@ -47,7 +53,10 @@ impl<E> EntityManager<E> where E: serde::Serialize + serde::de::DeserializeOwned
}
/// Replace entries in the list that matches a criteria
pub fn replace_entries<F>(&mut self, filter: F, el: &E) -> Res where F: Fn(&E) -> bool {
pub fn replace_entries<F>(&mut self, filter: F, el: &E) -> Res
where
F: Fn(&E) -> bool,
{
for i in 0..self.list.len() {
if filter(&self.list[i]) {
self.list[i] = el.clone();
@ -61,4 +70,4 @@ impl<E> EntityManager<E> where E: serde::Serialize + serde::de::DeserializeOwned
pub fn iter(&self) -> Iter<'_, E> {
self.list.iter()
}
}
}

View File

@ -1,5 +1,5 @@
pub mod app_config;
pub mod user;
pub mod service;
pub mod entity_manager;
pub mod session_identity;
pub mod service;
pub mod session_identity;
pub mod user;

View File

@ -1,2 +1,2 @@
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct ServiceID(String);
pub struct ServiceID(String);

View File

@ -17,7 +17,6 @@ impl Default for SessionStatus {
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct SessionIdentityData {
pub id: UserID,
@ -33,7 +32,8 @@ impl<'a> SessionIdentity<'a> {
}
pub fn deserialize_session_data(data: Option<String>) -> Option<SessionIdentityData> {
let res: Option<SessionIdentityData> = data.as_deref()
let res: Option<SessionIdentityData> = data
.as_deref()
.map(serde_json::from_str)
.map(|f| match f {
Ok(d) => Some(d),
@ -55,8 +55,7 @@ impl<'a> SessionIdentity<'a> {
}
fn set_session_data(&self, data: &SessionIdentityData) {
let s = serde_json::to_string(data)
.expect("Failed to serialize session data!");
let s = serde_json::to_string(data).expect("Failed to serialize session data!");
self.0.remember(s);
}
@ -88,8 +87,6 @@ impl<'a> SessionIdentity<'a> {
}
pub fn user_id(&self) -> UserID {
self.get_session_data()
.unwrap_or_default()
.id
self.get_session_data().unwrap_or_default().id
}
}
}

View File

@ -80,10 +80,13 @@ impl EntityManager<User> {
}
/// Update user information
fn update_user<F>(&mut self, id: &UserID, update: F) -> bool where F: FnOnce(User) -> User {
fn update_user<F>(&mut self, id: &UserID, update: F) -> bool
where
F: FnOnce(User) -> User,
{
let user = match self.find_by_user_id(id) {
None => return false,
Some(user) => user
Some(user) => user,
};
if let Err(e) = self.replace_entries(|u| u.uid.eq(id), &update(user)) {
@ -96,7 +99,7 @@ impl EntityManager<User> {
pub fn change_user_password(&mut self, id: &UserID, password: &str, temporary: bool) -> bool {
let new_hash = match hash_password(password) {
Ok(h) => { h }
Ok(h) => h,
Err(e) => {
log::error!("Failed to hash user password! {}", e);
return false;
@ -109,4 +112,4 @@ impl EntityManager<User> {
user
})
}
}
}