feat: Implement user management with a new add_user binary and refactor state handling into a dedicated store module.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
use crate::items::{Game, Person};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub person: Person,
|
||||
pub password_hash: String,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for User {
|
||||
type Target = Person;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.person
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PersistentState {
|
||||
pub games: Vec<Game>,
|
||||
pub users: Vec<User>,
|
||||
}
|
||||
|
||||
pub const STATE_FILE: &str = "state.json";
|
||||
|
||||
pub fn save_state(games: &[Game], users: &[User]) {
|
||||
let state = PersistentState {
|
||||
games: games.to_vec(),
|
||||
users: users.to_vec(),
|
||||
};
|
||||
if let Ok(file) = File::create(STATE_FILE) {
|
||||
let _ = serde_json::to_writer_pretty(file, &state);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_state() -> Option<(Vec<Game>, Vec<User>)> {
|
||||
if let Ok(file) = File::open(STATE_FILE) {
|
||||
let reader = BufReader::new(file);
|
||||
if let Ok(state) = serde_json::from_reader::<_, PersistentState>(reader) {
|
||||
return Some((state.games, state.users));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
Reference in New Issue
Block a user