feat: Ensure consistent ordering of games, users, and user opinions, and add DerefMut for User.
This commit is contained in:
+17
-4
@@ -17,6 +17,12 @@ impl std::ops::Deref for User {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for User {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.person
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct PersistentState {
|
||||
pub games: Vec<Game>,
|
||||
@@ -26,10 +32,17 @@ pub struct PersistentState {
|
||||
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(),
|
||||
};
|
||||
let mut games = games.to_vec();
|
||||
games.sort_unstable_by(|g1, g2| g1.title.cmp(&g2.title));
|
||||
|
||||
let mut users = users.to_vec();
|
||||
users.sort_unstable_by(|u1, u2| u1.name.cmp(&u2.name));
|
||||
|
||||
users.iter_mut().for_each(|u| {
|
||||
u.opinion.sort_unstable_by(|o1, o2| o1.title.cmp(&o2.title));
|
||||
});
|
||||
|
||||
let state = PersistentState { games, users };
|
||||
if let Ok(file) = File::create(STATE_FILE) {
|
||||
let _ = serde_json::to_writer_pretty(file, &state);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user